-
Notifications
You must be signed in to change notification settings - Fork 127
Migrate progress logger functions to use cmdio directly #3818
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
7 commits
Select commit
Hold shift + click to select a range
3644963
Remove unused in-place progress logging mode
pietern 8c3f4b7
Add entry to NEXT_CHANGELOG.md
pietern dc02f40
Remove unused JSON progress logging mode
pietern a48589f
Migrate progress logger functions to use cmdio directly
pietern 50939cc
Remove unused mutex field
pietern f6d3f65
Merge remote-tracking branch 'origin/main' into progress-logger-migra…
pietern 3dc0b8f
Merge branch 'main' into progress-logger-migrate-to-cmdio
pietern 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 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| package cmdio | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "io" | ||
| "strings" | ||
|
|
||
| "github.com/manifoldco/promptui" | ||
| ) | ||
|
|
||
| /* | ||
| Temporary compatibility layer for the progress logger interfaces. | ||
| */ | ||
|
|
||
| // Log is a compatibility layer for the progress logger interfaces. | ||
| // It writes the string representation of the stringer to the error writer. | ||
| func Log(ctx context.Context, str fmt.Stringer) { | ||
| LogString(ctx, str.String()) | ||
| } | ||
|
|
||
| // LogString is a compatibility layer for the progress logger interfaces. | ||
| // It writes the string to the error writer. | ||
| func LogString(ctx context.Context, str string) { | ||
| c := fromContext(ctx) | ||
| _, _ = io.WriteString(c.err, str) | ||
| _, _ = io.WriteString(c.err, "\n") | ||
| } | ||
|
|
||
| // readLine reads a line from the reader and returns it without the trailing newline characters. | ||
| // It is unbuffered because cmdio's stdin is also unbuffered. | ||
| // If we were to add a [bufio.Reader] to the mix, we would need to update the other uses of the reader. | ||
| // Once cmdio's stdio is made to be buffered, this function can be removed. | ||
| func readLine(r io.Reader) (string, error) { | ||
| var b strings.Builder | ||
| buf := make([]byte, 1) | ||
| for { | ||
| n, err := r.Read(buf) | ||
| if n > 0 { | ||
| if buf[0] == '\n' { | ||
| break | ||
| } | ||
| if buf[0] != '\r' { | ||
| b.WriteByte(buf[0]) | ||
| } | ||
| } | ||
| if err != nil { | ||
| if b.Len() == 0 { | ||
| return "", err | ||
| } | ||
| break | ||
| } | ||
| } | ||
| return b.String(), nil | ||
| } | ||
|
|
||
| // Ask is a compatibility layer for the progress logger interfaces. | ||
| // It prompts the user with a question and returns the answer. | ||
| func Ask(ctx context.Context, question, defaultVal string) (string, error) { | ||
| c := fromContext(ctx) | ||
|
|
||
| // Add default value to question prompt. | ||
| if defaultVal != "" { | ||
| question += fmt.Sprintf(` [%s]`, defaultVal) | ||
| } | ||
| question += `: ` | ||
|
|
||
| // Print prompt. | ||
| _, err := io.WriteString(c.err, question) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| // Read user input. Trim new line characters. | ||
| ans, err := readLine(c.in) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| // Return default value if user just presses enter. | ||
| if ans == "" { | ||
| return defaultVal, nil | ||
| } | ||
|
|
||
| return ans, nil | ||
| } | ||
|
|
||
| // AskYesOrNo is a compatibility layer for the progress logger interfaces. | ||
| // It prompts the user with a question and returns the answer. | ||
| func AskYesOrNo(ctx context.Context, question string) (bool, error) { | ||
| ans, err := Ask(ctx, question+" [y/n]", "") | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| return ans == "y", nil | ||
| } | ||
|
|
||
| func splitAtLastNewLine(s string) (string, string) { | ||
| // Split at the newline character | ||
| if i := strings.LastIndex(s, "\n"); i != -1 { | ||
| return s[:i+1], s[i+1:] | ||
| } | ||
| // Return the original string if no newline found | ||
| return "", s | ||
| } | ||
|
|
||
| // AskSelect is a compatibility layer for the progress logger interfaces. | ||
| // It prompts the user with a question and returns the answer. | ||
| func AskSelect(ctx context.Context, question string, choices []string) (string, error) { | ||
| c := fromContext(ctx) | ||
|
|
||
| // Promptui does not support multiline prompts. So we split the question. | ||
| first, last := splitAtLastNewLine(question) | ||
| _, err := io.WriteString(c.err, first) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| // Note: by default this prompt uses os.Stdin and os.Stdout. | ||
| // This is contrary to the rest of the original progress logger | ||
| // functions that write to stderr. | ||
| prompt := promptui.Select{ | ||
| Label: last, | ||
| Items: choices, | ||
| HideHelp: true, | ||
| Templates: &promptui.SelectTemplates{ | ||
| Label: "{{.}}: ", | ||
| Selected: last + ": {{.}}", | ||
| }, | ||
| } | ||
|
|
||
| _, ans, err := prompt.Run() | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| return ans, nil | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for a dependency on
cmdiohere.The previous implementation wrote directly to
os.Stderr.