|
| 1 | +package cmdio |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/manifoldco/promptui" |
| 10 | +) |
| 11 | + |
| 12 | +/* |
| 13 | +Temporary compatibility layer for the progress logger interfaces. |
| 14 | +*/ |
| 15 | + |
| 16 | +// Log is a compatibility layer for the progress logger interfaces. |
| 17 | +// It writes the string representation of the stringer to the error writer. |
| 18 | +func Log(ctx context.Context, str fmt.Stringer) { |
| 19 | + LogString(ctx, str.String()) |
| 20 | +} |
| 21 | + |
| 22 | +// LogString is a compatibility layer for the progress logger interfaces. |
| 23 | +// It writes the string to the error writer. |
| 24 | +func LogString(ctx context.Context, str string) { |
| 25 | + c := fromContext(ctx) |
| 26 | + _, _ = io.WriteString(c.err, str) |
| 27 | + _, _ = io.WriteString(c.err, "\n") |
| 28 | +} |
| 29 | + |
| 30 | +// readLine reads a line from the reader and returns it without the trailing newline characters. |
| 31 | +// It is unbuffered because cmdio's stdin is also unbuffered. |
| 32 | +// If we were to add a [bufio.Reader] to the mix, we would need to update the other uses of the reader. |
| 33 | +// Once cmdio's stdio is made to be buffered, this function can be removed. |
| 34 | +func readLine(r io.Reader) (string, error) { |
| 35 | + var b strings.Builder |
| 36 | + buf := make([]byte, 1) |
| 37 | + for { |
| 38 | + n, err := r.Read(buf) |
| 39 | + if n > 0 { |
| 40 | + if buf[0] == '\n' { |
| 41 | + break |
| 42 | + } |
| 43 | + if buf[0] != '\r' { |
| 44 | + b.WriteByte(buf[0]) |
| 45 | + } |
| 46 | + } |
| 47 | + if err != nil { |
| 48 | + if b.Len() == 0 { |
| 49 | + return "", err |
| 50 | + } |
| 51 | + break |
| 52 | + } |
| 53 | + } |
| 54 | + return b.String(), nil |
| 55 | +} |
| 56 | + |
| 57 | +// Ask is a compatibility layer for the progress logger interfaces. |
| 58 | +// It prompts the user with a question and returns the answer. |
| 59 | +func Ask(ctx context.Context, question, defaultVal string) (string, error) { |
| 60 | + c := fromContext(ctx) |
| 61 | + |
| 62 | + // Add default value to question prompt. |
| 63 | + if defaultVal != "" { |
| 64 | + question += fmt.Sprintf(` [%s]`, defaultVal) |
| 65 | + } |
| 66 | + question += `: ` |
| 67 | + |
| 68 | + // Print prompt. |
| 69 | + _, err := io.WriteString(c.err, question) |
| 70 | + if err != nil { |
| 71 | + return "", err |
| 72 | + } |
| 73 | + |
| 74 | + // Read user input. Trim new line characters. |
| 75 | + ans, err := readLine(c.in) |
| 76 | + if err != nil { |
| 77 | + return "", err |
| 78 | + } |
| 79 | + |
| 80 | + // Return default value if user just presses enter. |
| 81 | + if ans == "" { |
| 82 | + return defaultVal, nil |
| 83 | + } |
| 84 | + |
| 85 | + return ans, nil |
| 86 | +} |
| 87 | + |
| 88 | +// AskYesOrNo is a compatibility layer for the progress logger interfaces. |
| 89 | +// It prompts the user with a question and returns the answer. |
| 90 | +func AskYesOrNo(ctx context.Context, question string) (bool, error) { |
| 91 | + ans, err := Ask(ctx, question+" [y/n]", "") |
| 92 | + if err != nil { |
| 93 | + return false, err |
| 94 | + } |
| 95 | + return ans == "y", nil |
| 96 | +} |
| 97 | + |
| 98 | +func splitAtLastNewLine(s string) (string, string) { |
| 99 | + // Split at the newline character |
| 100 | + if i := strings.LastIndex(s, "\n"); i != -1 { |
| 101 | + return s[:i+1], s[i+1:] |
| 102 | + } |
| 103 | + // Return the original string if no newline found |
| 104 | + return "", s |
| 105 | +} |
| 106 | + |
| 107 | +// AskSelect is a compatibility layer for the progress logger interfaces. |
| 108 | +// It prompts the user with a question and returns the answer. |
| 109 | +func AskSelect(ctx context.Context, question string, choices []string) (string, error) { |
| 110 | + c := fromContext(ctx) |
| 111 | + |
| 112 | + // Promptui does not support multiline prompts. So we split the question. |
| 113 | + first, last := splitAtLastNewLine(question) |
| 114 | + _, err := io.WriteString(c.err, first) |
| 115 | + if err != nil { |
| 116 | + return "", err |
| 117 | + } |
| 118 | + |
| 119 | + // Note: by default this prompt uses os.Stdin and os.Stdout. |
| 120 | + // This is contrary to the rest of the original progress logger |
| 121 | + // functions that write to stderr. |
| 122 | + prompt := promptui.Select{ |
| 123 | + Label: last, |
| 124 | + Items: choices, |
| 125 | + HideHelp: true, |
| 126 | + Templates: &promptui.SelectTemplates{ |
| 127 | + Label: "{{.}}: ", |
| 128 | + Selected: last + ": {{.}}", |
| 129 | + }, |
| 130 | + } |
| 131 | + |
| 132 | + _, ans, err := prompt.Run() |
| 133 | + if err != nil { |
| 134 | + return "", err |
| 135 | + } |
| 136 | + return ans, nil |
| 137 | +} |
0 commit comments