-
Notifications
You must be signed in to change notification settings - Fork 174
ROX-28263: Custom roxctl command usage formatting
#14376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
e29de33
format utils
parametalol 03a4e64
Remove default boolean patch
parametalol cb6d328
format command groups
parametalol 8f08601
rewritten bats tests
parametalol f559ddf
ignore trailing whitespaces in roxctl output
parametalol 29b741d
fixed some bats tests
parametalol 90e9d90
ignore main command help captialization check
parametalol 98b61a9
comment about kubectl
parametalol 0af78eb
fix bats tests
parametalol 05dfded
new commands tree
parametalol 00adcbc
refactoring
parametalol fe04dfc
fixes
parametalol ec47cdf
test formatting of flags from a flagset
parametalol eca12e6
fix tabs
parametalol 3f6cc05
formatting_writer refactoring
parametalol 1c07ace
reset command_tree_debug.yaml
parametalol b17ddfa
fix bats tests
parametalol 5c55879
format deprecated commands
parametalol 521c3c1
comments, minor refactorings
parametalol 28ac1ee
CHANGELOG.md
parametalol 5706678
get terminal width
parametalol ab3dcbd
wordsAndDelimeters bufio.SplitFunc
parametalol 84f41cc
revert bats' helpers.bash
parametalol 3164dcd
make tabWidth formattingWriter property
parametalol d603513
renamed pop -> popNotLast
parametalol 4e49f50
fix double ln
parametalol 336d0ec
don't pad before ln
parametalol 5364e28
refactoring
parametalol 5558939
test write errors
parametalol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| package utils | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "io" | ||
| "strings" | ||
| ) | ||
|
|
||
| const defaultTabWidth = 8 | ||
|
|
||
| // formattingWriter implements StringWriter interface. | ||
| // It writes strings to the underlying writer indenting and wrapping the text. | ||
| type formattingWriter struct { | ||
| raw io.StringWriter | ||
| width int | ||
| indent indents | ||
| tabWidth int | ||
|
|
||
| currentLine int | ||
| written int | ||
| indentReset bool | ||
| } | ||
|
|
||
| var _ io.StringWriter = (*formattingWriter)(nil) | ||
|
|
||
| func makeFormattingWriter(w io.StringWriter, width int, tabWidth int, indent ...int) *formattingWriter { | ||
| return &formattingWriter{raw: w, width: width, indent: indent, tabWidth: tabWidth} | ||
| } | ||
|
|
||
| // write is an internal method that writes the string to the underlying writer. | ||
| func (w *formattingWriter) write(s string) error { | ||
| n, err := w.raw.WriteString(s) | ||
| w.currentLine += n | ||
| w.written += n | ||
| return err //nolint:wrapcheck | ||
| } | ||
|
|
||
| // writePadding is an internal method, that takes the next indent value and the | ||
| // writes the according number of spaces. If the indent value is negative, it is | ||
| // calculated as tabulation offset, i.e. the spaces are added to reach the | ||
| // required offset. Returns true if a new line has been written. | ||
| func (w *formattingWriter) writePadding() (bool, error) { | ||
| w.indentReset = false | ||
| padding := w.indent.popNotLast() | ||
| if padding < 0 { | ||
| // Indent to tabulation. | ||
| padding = -padding | ||
| if padding > w.currentLine { | ||
| padding -= w.currentLine | ||
| } else { | ||
| return true, nil | ||
| } | ||
| } else if w.currentLine+padding > w.width { | ||
| return true, nil | ||
| } | ||
| return false, w.write(strings.Repeat(" ", padding)) | ||
| } | ||
|
|
||
| // ln is an internal method that writes a new line to the underlying writer. | ||
| func (w *formattingWriter) ln() error { | ||
| _, err := w.raw.WriteString("\n") | ||
| w.currentLine = 0 | ||
| return err //nolint:wrapcheck | ||
| } | ||
|
|
||
| // SetIndent updates the indentation for the following writings. | ||
| func (w *formattingWriter) SetIndent(indent ...int) { | ||
| w.indentReset = true | ||
| w.indent = indent | ||
| } | ||
|
|
||
| // WriteString writes the string to the underlying writer, with the configured | ||
| // indentation and wrapping. | ||
| // Implements the StringWriter interface. | ||
| func (w *formattingWriter) WriteString(s string) (int, error) { | ||
| w.written = 0 | ||
| tokenScanner := bufio.NewScanner(strings.NewReader(s)) | ||
| tokenScanner.Split(wordsAndDelimeters) | ||
| var err error | ||
| for err == nil && tokenScanner.Scan() { | ||
| token := tokenScanner.Text() | ||
| length := len(token) | ||
| switch token { | ||
| case "\t": | ||
| length = defaultTabWidth | ||
| case "\n": | ||
| if w.currentLine == 0 { | ||
| w.indent.popNotLast() | ||
| w.indentReset = false | ||
| } | ||
| err = w.ln() | ||
| continue | ||
| } | ||
| ln := false | ||
| if w.currentLine == 0 || w.indentReset { | ||
| if ln, err = w.writePadding(); err != nil { | ||
| break | ||
| } | ||
| } | ||
| // Write a new line and a padding in case of line overflow: | ||
| if ln || w.currentLine+length > w.width { | ||
| if err = w.ln(); err != nil { | ||
| break | ||
| } | ||
| if token == " " { | ||
| // Do not write space token that caused line break. | ||
| continue | ||
| } | ||
| if _, err = w.writePadding(); err != nil { | ||
| break | ||
| } | ||
| } | ||
| err = w.write(token) | ||
| } | ||
| return w.written, err | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.