diff --git a/go.mod b/go.mod index 79da2b4a..5eea382b 100644 --- a/go.mod +++ b/go.mod @@ -69,7 +69,7 @@ require ( github.com/muesli/reflow v0.3.0 // indirect github.com/muesli/termenv v0.15.2 // indirect github.com/pkg/errors v0.9.1 - github.com/pterm/pterm v0.12.74 + github.com/pterm/pterm v0.12.79 github.com/rivo/uniseg v0.4.4 // indirect github.com/spf13/pflag v1.0.5 github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect diff --git a/go.sum b/go.sum index e0a6b1f4..634c7231 100644 --- a/go.sum +++ b/go.sum @@ -149,8 +149,8 @@ github.com/pterm/pterm v0.12.31/go.mod h1:32ZAWZVXD7ZfG0s8qqHXePte42kdz8ECtRyEej github.com/pterm/pterm v0.12.33/go.mod h1:x+h2uL+n7CP/rel9+bImHD5lF3nM9vJj80k9ybiiTTE= github.com/pterm/pterm v0.12.36/go.mod h1:NjiL09hFhT/vWjQHSj1athJpx6H8cjpHXNAK5bUw8T8= github.com/pterm/pterm v0.12.40/go.mod h1:ffwPLwlbXxP+rxT0GsgDTzS3y3rmpAO1NMjUkGTYf8s= -github.com/pterm/pterm v0.12.74 h1:fPsds9KisCyJh4NyY6bv8QJt3FLHceb5DxI6W0An9cc= -github.com/pterm/pterm v0.12.74/go.mod h1:+M33aZWQVpmLmLbvjykyGZ4gAfeebznRo8JMbabaxQU= +github.com/pterm/pterm v0.12.79 h1:lH3yrYMhdpeqX9y5Ep1u7DejyHy7NSQg9qrBjF9dFT4= +github.com/pterm/pterm v0.12.79/go.mod h1:1v/gzOF1N0FsjbgTHZ1wVycRkKiatFvJSJC4IGaQAAo= github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis= diff --git a/vendor/github.com/pterm/pterm/README.md b/vendor/github.com/pterm/pterm/README.md index c7a37292..6a9cd784 100644 --- a/vendor/github.com/pterm/pterm/README.md +++ b/vendor/github.com/pterm/pterm/README.md @@ -2388,11 +2388,38 @@ import ( ) func main() { - // Create an interactive text input with single line input mode - textInput := pterm.DefaultInteractiveTextInput.WithMultiLine(false) + // Create an interactive text input with single line input mode and show it + result, _ := pterm.DefaultInteractiveTextInput.Show() - // Show the text input and get the result - result, _ := textInput.Show() + // Print a blank line for better readability + pterm.Println() + + // Print the user's answer with an info prefix + pterm.Info.Printfln("You answered: %s", result) +} + +``` + + + +### interactive_textinput/default-value + +![Animation](https://raw.githubusercontent.com/pterm/pterm/master/_examples/interactive_textinput/default-value/animation.svg) + +
+ +SHOW SOURCE + +```go +package main + +import ( + "github.com/pterm/pterm" +) + +func main() { + // Create an interactive text input with single line input mode and show it + result, _ := pterm.DefaultInteractiveTextInput.WithDefaultValue("Some default value").Show() // Print a blank line for better readability pterm.Println() diff --git a/vendor/github.com/pterm/pterm/errors.go b/vendor/github.com/pterm/pterm/errors.go index a7d6fcc7..9260ab64 100644 --- a/vendor/github.com/pterm/pterm/errors.go +++ b/vendor/github.com/pterm/pterm/errors.go @@ -8,4 +8,7 @@ var ( // ErrHexCodeIsInvalid - the given HEX code is invalid. ErrHexCodeIsInvalid = errors.New("hex code is not valid") + + // ErrKeyWithoutValue - an odd number of arguments was passed to a pterm Logger's Args method. + ErrKeyWithoutValue = "ERROR: key_without_value" ) diff --git a/vendor/github.com/pterm/pterm/interactive_multiselect_printer.go b/vendor/github.com/pterm/pterm/interactive_multiselect_printer.go index 754ba98a..9dce03fb 100644 --- a/vendor/github.com/pterm/pterm/interactive_multiselect_printer.go +++ b/vendor/github.com/pterm/pterm/interactive_multiselect_printer.go @@ -206,7 +206,7 @@ func (p *InteractiveMultiselectPrinter) Show(text ...string) ([]string, error) { } case keys.Backspace: // Remove last character from fuzzy search string - if len(p.fuzzySearchString) > 0 { + if p.fuzzySearchString != "" { // Handle UTF-8 characters p.fuzzySearchString = string([]rune(p.fuzzySearchString)[:len([]rune(p.fuzzySearchString))-1]) } diff --git a/vendor/github.com/pterm/pterm/interactive_select_printer.go b/vendor/github.com/pterm/pterm/interactive_select_printer.go index 0c4bc995..d62f5dad 100644 --- a/vendor/github.com/pterm/pterm/interactive_select_printer.go +++ b/vendor/github.com/pterm/pterm/interactive_select_printer.go @@ -173,7 +173,7 @@ func (p *InteractiveSelectPrinter) Show(text ...string) (string, error) { area.Update(p.renderSelectMenu()) case keys.Backspace: // Remove last character from fuzzy search string - if len(p.fuzzySearchString) > 0 { + if p.fuzzySearchString != "" { // Handle UTF-8 characters p.fuzzySearchString = string([]rune(p.fuzzySearchString)[:len([]rune(p.fuzzySearchString))-1]) } diff --git a/vendor/github.com/pterm/pterm/interactive_textinput_printer.go b/vendor/github.com/pterm/pterm/interactive_textinput_printer.go index 88535c51..6926a4be 100644 --- a/vendor/github.com/pterm/pterm/interactive_textinput_printer.go +++ b/vendor/github.com/pterm/pterm/interactive_textinput_printer.go @@ -6,6 +6,7 @@ import ( "atomicgo.dev/cursor" "atomicgo.dev/keyboard" "atomicgo.dev/keyboard/keys" + "github.com/mattn/go-runewidth" "github.com/pterm/pterm/internal" ) @@ -28,10 +29,12 @@ type InteractiveTextInputPrinter struct { Mask string OnInterruptFunc func() - input []string - cursorXPos int - cursorYPos int - text string + input []string + cursorXPos int + cursorYPos int + text string + startedTyping bool + valueStyle *Style } // WithDefaultText sets the default text. @@ -85,7 +88,7 @@ func (p InteractiveTextInputPrinter) Show(text ...string) (string, error) { var areaText string - if len(text) == 0 || Sprint(text[0]) == "" { + if len(text) == 0 || text[0] == "" { text = []string{p.DefaultText} } @@ -101,11 +104,11 @@ func (p InteractiveTextInputPrinter) Show(text ...string) (string, error) { area.StartOfLine() if !p.MultiLine { - cursor.Right(len(RemoveColorFromString(areaText))) + cursor.Right(runewidth.StringWidth(RemoveColorFromString(areaText))) } if p.DefaultValue != "" { - p.input = append(p.input, p.DefaultValue) + p.input = append(p.input, Gray(p.DefaultValue)) p.updateArea(&area) } @@ -124,6 +127,17 @@ func (p InteractiveTextInputPrinter) Show(text ...string) (string, error) { return true, nil } case keys.Enter: + if p.DefaultValue != "" && !p.startedTyping { + for i := range p.input { + p.input[i] = RemoveColorFromString(p.input[i]) + } + + if p.MultiLine { + area.Bottom() + } + return true, nil + } + if p.MultiLine { if key.AltPressed { p.cursorXPos = 0 @@ -140,10 +154,22 @@ func (p InteractiveTextInputPrinter) Show(text ...string) (string, error) { return true, nil } case keys.RuneKey: + if !p.startedTyping { + p.input = []string{""} + p.startedTyping = true + } p.input[p.cursorYPos] = string(append([]rune(p.input[p.cursorYPos])[:len([]rune(p.input[p.cursorYPos]))+p.cursorXPos], append([]rune(key.String()), []rune(p.input[p.cursorYPos])[len([]rune(p.input[p.cursorYPos]))+p.cursorXPos:]...)...)) case keys.Space: + if !p.startedTyping { + p.input = []string{" "} + p.startedTyping = true + } p.input[p.cursorYPos] = string(append([]rune(p.input[p.cursorYPos])[:len([]rune(p.input[p.cursorYPos]))+p.cursorXPos], append([]rune(" "), []rune(p.input[p.cursorYPos])[len([]rune(p.input[p.cursorYPos]))+p.cursorXPos:]...)...)) case keys.Backspace: + if !p.startedTyping { + p.input = []string{""} + p.startedTyping = true + } if len([]rune(p.input[p.cursorYPos]))+p.cursorXPos > 0 { p.input[p.cursorYPos] = string(append([]rune(p.input[p.cursorYPos])[:len([]rune(p.input[p.cursorYPos]))-1+p.cursorXPos], []rune(p.input[p.cursorYPos])[len([]rune(p.input[p.cursorYPos]))+p.cursorXPos:]...)) } else if p.cursorYPos > 0 { @@ -154,6 +180,11 @@ func (p InteractiveTextInputPrinter) Show(text ...string) (string, error) { p.cursorYPos-- } case keys.Delete: + if !p.startedTyping { + p.input = []string{""} + p.startedTyping = true + return false, nil + } if len([]rune(p.input[p.cursorYPos]))+p.cursorXPos < len([]rune(p.input[p.cursorYPos])) { p.input[p.cursorYPos] = string(append([]rune(p.input[p.cursorYPos])[:len([]rune(p.input[p.cursorYPos]))+p.cursorXPos], []rune(p.input[p.cursorYPos])[len([]rune(p.input[p.cursorYPos]))+p.cursorXPos+1:]...)) p.cursorXPos++ @@ -167,6 +198,10 @@ func (p InteractiveTextInputPrinter) Show(text ...string) (string, error) { cancel() return true, nil case keys.Down: + if !p.startedTyping { + p.input = []string{""} + p.startedTyping = true + } if p.cursorYPos+1 < len(p.input) { p.cursorXPos = (internal.GetStringMaxWidth(p.input[p.cursorYPos]) + p.cursorXPos) - internal.GetStringMaxWidth(p.input[p.cursorYPos+1]) if p.cursorXPos > 0 { @@ -175,6 +210,10 @@ func (p InteractiveTextInputPrinter) Show(text ...string) (string, error) { p.cursorYPos++ } case keys.Up: + if !p.startedTyping { + p.input = []string{""} + p.startedTyping = true + } if p.cursorYPos > 0 { p.cursorXPos = (internal.GetStringMaxWidth(p.input[p.cursorYPos]) + p.cursorXPos) - internal.GetStringMaxWidth(p.input[p.cursorYPos-1]) if p.cursorXPos > 0 { @@ -222,6 +261,10 @@ func (p InteractiveTextInputPrinter) Show(text ...string) (string, error) { } } + if !p.startedTyping { + return p.DefaultValue, nil + } + return strings.ReplaceAll(areaText, p.text, ""), nil } diff --git a/vendor/github.com/pterm/pterm/logger.go b/vendor/github.com/pterm/pterm/logger.go index 26eea5b0..407f4d50 100644 --- a/vendor/github.com/pterm/pterm/logger.go +++ b/vendor/github.com/pterm/pterm/logger.go @@ -211,6 +211,8 @@ func (l Logger) Args(args ...any) []LoggerArgument { var loggerArgs []LoggerArgument // args are in the format of: key, value, key, value, key, value, ... + args = l.sanitizeArgs(args) + for i := 0; i < len(args); i += 2 { key := Sprint(args[i]) value := args[i+1] @@ -238,6 +240,20 @@ func (l Logger) ArgsFromMap(m map[string]any) []LoggerArgument { return loggerArgs } +// sanitizeArgs inserts an error message into an args slice if an odd number of arguments is provided. +func (l Logger) sanitizeArgs(args []any) []any { + numArgs := len(args) + if numArgs > 0 && numArgs%2 != 0 { + if numArgs > 1 { + lastArg := args[numArgs-1] + args = append(args[:numArgs-1], []any{ErrKeyWithoutValue, lastArg}...) + } else { + args = []any{ErrKeyWithoutValue, args[0]} + } + } + return args +} + func (l Logger) getCallerInfo() (path string, line int) { if !l.ShowCaller { return @@ -280,7 +296,7 @@ func (l Logger) print(level LogLevel, msg string, args []LoggerArgument) { loggerMutex.Lock() defer loggerMutex.Unlock() - _, _ = l.Writer.Write([]byte(line + "\n")) + Fprintln(l.Writer, line) } func (l Logger) renderColorful(level LogLevel, msg string, args []LoggerArgument) (result string) { diff --git a/vendor/github.com/pterm/pterm/panel_printer.go b/vendor/github.com/pterm/pterm/panel_printer.go index 709f7c80..090498e7 100644 --- a/vendor/github.com/pterm/pterm/panel_printer.go +++ b/vendor/github.com/pterm/pterm/panel_printer.go @@ -183,7 +183,7 @@ func (p PanelPrinter) Srender() (string, error) { // Render prints the Template to the terminal. func (p PanelPrinter) Render() error { s, _ := p.Srender() - Println(s) + Fprintln(p.Writer, s) return nil } diff --git a/vendor/github.com/pterm/pterm/print.go b/vendor/github.com/pterm/pterm/print.go index a58e5fa6..67b49c02 100644 --- a/vendor/github.com/pterm/pterm/print.go +++ b/vendor/github.com/pterm/pterm/print.go @@ -3,13 +3,17 @@ package pterm import ( "fmt" "io" + "os" "strings" "github.com/gookit/color" ) +var defaultWriter io.Writer = os.Stdout + // SetDefaultOutput sets the default output of pterm. func SetDefaultOutput(w io.Writer) { + defaultWriter = w color.SetOutput(w) } @@ -45,7 +49,7 @@ func Sprinto(a ...interface{}) string { // Spaces are added between operands when neither is a string. // It returns the number of bytes written and any write error encountered. func Print(a ...interface{}) { - Fprint(nil, a...) + Fprint(defaultWriter, a...) } // Println formats using the default formats for its operands and writes to standard output. diff --git a/vendor/github.com/pterm/pterm/progressbar_printer.go b/vendor/github.com/pterm/pterm/progressbar_printer.go index 1cf4a0c2..d2bb35c1 100644 --- a/vendor/github.com/pterm/pterm/progressbar_printer.go +++ b/vendor/github.com/pterm/pterm/progressbar_printer.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "math" + "os" "strings" "time" @@ -32,6 +33,7 @@ var DefaultProgressbar = ProgressbarPrinter{ ShowElapsedTime: true, BarFiller: Gray("█"), MaxWidth: 80, + Writer: os.Stdout, } // ProgressbarPrinter shows a progress animation in the terminal. diff --git a/vendor/github.com/pterm/pterm/rgb.go b/vendor/github.com/pterm/pterm/rgb.go index a35dd1a0..7c00835a 100644 --- a/vendor/github.com/pterm/pterm/rgb.go +++ b/vendor/github.com/pterm/pterm/rgb.go @@ -136,13 +136,13 @@ func (p RGBStyle) Sprintln(a ...interface{}) string { // Sprintf formats according to a format specifier and returns the resulting string. func (p RGBStyle) Sprintf(format string, a ...interface{}) string { - return fmt.Sprintf(format, p.Sprint(a...)) + return p.Sprint(Sprintf(format, a...)) } // Sprintfln formats according to a format specifier and returns the resulting string. // Spaces are always added between operands and a newline is appended. func (p RGBStyle) Sprintfln(format string, a ...interface{}) string { - return fmt.Sprintf(format, p.Sprint(a...)) + "\n" + return p.Sprintf(format, a...) + "\n" } // GetValues returns the RGB values separately. diff --git a/vendor/modules.txt b/vendor/modules.txt index c76be9f9..f4effbc8 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -238,7 +238,7 @@ github.com/pjbgf/sha1cd/ubc # github.com/pkg/errors v0.9.1 ## explicit github.com/pkg/errors -# github.com/pterm/pterm v0.12.74 +# github.com/pterm/pterm v0.12.79 ## explicit; go 1.21 github.com/pterm/pterm github.com/pterm/pterm/internal