Skip to content

Commit

Permalink
added default value for interactive text input (#577)
Browse files Browse the repository at this point in the history
Co-authored-by: Karolos Lykos <Karolos.Lykos@powerfactors.com>
  • Loading branch information
KarolosLykos and Karolos Lykos committed Oct 18, 2023
1 parent bfe641f commit 49ccf92
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 1 deletion.
13 changes: 13 additions & 0 deletions interactive_textinput_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var DefaultInteractiveTextInput = InteractiveTextInputPrinter{
type InteractiveTextInputPrinter struct {
TextStyle *Style
DefaultText string
DefaultValue string
Delimiter string
MultiLine bool
Mask string
Expand All @@ -39,6 +40,12 @@ func (p InteractiveTextInputPrinter) WithDefaultText(text string) *InteractiveTe
return &p
}

// WithDefaultValue sets the default value.
func (p InteractiveTextInputPrinter) WithDefaultValue(value string) *InteractiveTextInputPrinter {
p.DefaultValue = value
return &p
}

// WithTextStyle sets the text style.
func (p InteractiveTextInputPrinter) WithTextStyle(style *Style) *InteractiveTextInputPrinter {
p.TextStyle = style
Expand Down Expand Up @@ -87,6 +94,7 @@ func (p InteractiveTextInputPrinter) Show(text ...string) (string, error) {
} else {
areaText = p.TextStyle.Sprintf("%s%s", text[0], p.Delimiter)
}

p.text = areaText
area := cursor.NewArea()
area.Update(areaText)
Expand All @@ -96,6 +104,11 @@ func (p InteractiveTextInputPrinter) Show(text ...string) (string, error) {
cursor.Right(len(RemoveColorFromString(areaText)))
}

if p.DefaultValue != "" {
p.input = append(p.input, p.DefaultValue)
p.updateArea(&area)
}

err := keyboard.Listen(func(key keys.Key) (stop bool, err error) {
if !p.MultiLine {
p.cursorYPos = 0
Expand Down
194 changes: 193 additions & 1 deletion interactive_textinput_printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pterm_test
import (
"reflect"
"testing"
"time"

"atomicgo.dev/keyboard"
"atomicgo.dev/keyboard/keys"
Expand All @@ -16,6 +17,11 @@ func TestInteractiveTextInputPrinter_WithDefaultText(t *testing.T) {
testza.AssertEqual(t, p.DefaultText, "default")
}

func TestInteractiveTextInputPrinter_WithDefaultValue(t *testing.T) {
p := pterm.DefaultInteractiveTextInput.WithDefaultValue("default")
testza.AssertEqual(t, p.DefaultValue, "default")
}

func TestInteractiveTextInputPrinter_WithDelimiter(t *testing.T) {
p := pterm.DefaultInteractiveTextInput.WithDelimiter(">>")
testza.AssertEqual(t, p.Delimiter, ">>")
Expand All @@ -39,13 +45,199 @@ func TestInteractiveTextInputPrinter_WithTextStyle(t *testing.T) {

func TestInteractiveTextInputPrinter_WithMask(t *testing.T) {
go func() {
time.Sleep(1 * time.Millisecond)
keyboard.SimulateKeyPress('a')
keyboard.SimulateKeyPress('b')
keyboard.SimulateKeyPress('c')
keyboard.SimulateKeyPress(keys.Enter)
}()
result, _ := pterm.DefaultInteractiveTextInput.WithMask("*").Show()
testza.AssertEqual(t, result, "abc")
testza.AssertEqual(t, "abc", result)
}

func TestInteractiveTextInputPrinter_WithCancel(t *testing.T) {
go func() {
time.Sleep(1 * time.Millisecond)
keyboard.SimulateKeyPress(keys.CtrlC)
}()
result, _ := pterm.DefaultInteractiveTextInput.WithMask("*").Show()
testza.AssertEqual(t, "", result)
}

func TestInteractiveTextInputPrinter_OnEnter(t *testing.T) {
go func() {
keyboard.SimulateKeyPress(keys.Enter)
}()
result, _ := pterm.DefaultInteractiveTextInput.WithDefaultValue("default").Show()
testza.AssertEqual(t, "default", result)
}

func TestInteractiveTextInputPrinter_WithMultiLineOnTab(t *testing.T) {
go func() {
keyboard.SimulateKeyPress(keys.Tab)
}()
result, _ := pterm.DefaultInteractiveTextInput.
WithMultiLine(true).
WithDefaultValue("default").Show()
testza.AssertEqual(t, "default", result)
}

func TestInteractiveTextInputPrinter_WithMultiLineOnUp(t *testing.T) {
go func() {
time.Sleep(1 * time.Millisecond)
keyboard.SimulateKeyPress(keys.Enter)
keyboard.SimulateKeyPress("second line")
keyboard.SimulateKeyPress(keys.Up)
keyboard.SimulateKeyPress("first line")
keyboard.SimulateKeyPress(keys.Tab)
}()
result, _ := pterm.DefaultInteractiveTextInput.
WithMultiLine(true).Show()
testza.AssertEqual(t, "first line\nsecond line", result)
}

func TestInteractiveTextInputPrinter_WithMultiLineOnDown(t *testing.T) {
go func() {
time.Sleep(1 * time.Millisecond)
keyboard.SimulateKeyPress("a")
keyboard.SimulateKeyPress(keys.Enter)
keyboard.SimulateKeyPress(keys.Enter)
keyboard.SimulateKeyPress(keys.Up)
keyboard.SimulateKeyPress("b")
keyboard.SimulateKeyPress(keys.Down)
keyboard.SimulateKeyPress("c")
keyboard.SimulateKeyPress(keys.Tab)
}()
result, _ := pterm.DefaultInteractiveTextInput.
WithMultiLine(true).Show()
testza.AssertEqual(t, "a\nb\nc", result)
}

func TestInteractiveTextInputPrinter_WithMultiLineOnLeft(t *testing.T) {
go func() {
time.Sleep(1 * time.Millisecond)
keyboard.SimulateKeyPress(keys.Enter)
keyboard.SimulateKeyPress("a")
keyboard.SimulateKeyPress(keys.Left)
keyboard.SimulateKeyPress(keys.Left)
keyboard.SimulateKeyPress("b")
keyboard.SimulateKeyPress(keys.Tab)
}()
result, _ := pterm.DefaultInteractiveTextInput.
WithMultiLine(true).Show()
testza.AssertEqual(t, "b\na", result)
}

func TestInteractiveTextInputPrinter_WithMultiLineOnRight(t *testing.T) {
go func() {
time.Sleep(1 * time.Millisecond)
keyboard.SimulateKeyPress('a')
keyboard.SimulateKeyPress(keys.Enter)
keyboard.SimulateKeyPress(keys.Up)
keyboard.SimulateKeyPress(keys.Right)
keyboard.SimulateKeyPress(keys.Right)
keyboard.SimulateKeyPress("b")
keyboard.SimulateKeyPress(keys.Tab)
}()
result, _ := pterm.DefaultInteractiveTextInput.
WithMultiLine(true).Show()
testza.AssertEqual(t, "a\nb", result)
}

func TestInteractiveTextInputPrinter_OnSpace(t *testing.T) {
go func() {
keyboard.SimulateKeyPress(keys.Space)
keyboard.SimulateKeyPress(keys.Tab)
}()
result, _ := pterm.DefaultInteractiveTextInput.
WithMultiLine(true).
WithDefaultValue("default").Show()
testza.AssertEqual(t, "default ", result)
}

func TestInteractiveTextInputPrinter_OnDelete(t *testing.T) {
go func() {
keyboard.SimulateKeyPress(keys.Left)
keyboard.SimulateKeyPress(keys.Delete)
keyboard.SimulateKeyPress(keys.Enter)
}()
result, _ := pterm.DefaultInteractiveTextInput.
WithDefaultValue("a").Show()
testza.AssertEqual(t, "", result)
}

func TestInteractiveTextInputPrinter_OnBackspace(t *testing.T) {
go func() {
keyboard.SimulateKeyPress(keys.Backspace)
keyboard.SimulateKeyPress(keys.Enter)
}()
result, _ := pterm.DefaultInteractiveTextInput.
WithDefaultValue("a").Show()
testza.AssertEqual(t, "", result)
}

func TestInteractiveTextInputPrinter_WithMultiLineOnDelete(t *testing.T) {
go func() {
keyboard.SimulateKeyPress(keys.Enter)
keyboard.SimulateKeyPress('a')
keyboard.SimulateKeyPress(keys.Up)
keyboard.SimulateKeyPress(keys.Delete)
keyboard.SimulateKeyPress(keys.Tab)
}()
result, _ := pterm.DefaultInteractiveTextInput.
WithMultiLine(true).
WithDefaultValue("a").Show()
testza.AssertEqual(t, "aa", result)
}

func TestInteractiveTextInputPrinter_WithMultiLineOnBackspace(t *testing.T) {
go func() {
keyboard.SimulateKeyPress(keys.Enter)
keyboard.SimulateKeyPress(keys.Backspace)
keyboard.SimulateKeyPress(keys.Tab)
}()
result, _ := pterm.DefaultInteractiveTextInput.
WithMultiLine(true).
WithDefaultValue("a").Show()
testza.AssertEqual(t, "a", result)
}

func TestInteractiveTextInputPrinter_WithMultiLineAltPressed(t *testing.T) {
go func() {
keyboard.SimulateKeyPress(keys.Key{AltPressed: true, Code: keys.Enter})
keyboard.SimulateKeyPress(keys.Tab)
}()
result, _ := pterm.DefaultInteractiveTextInput.
WithMultiLine(true).
WithDefaultValue("a").Show()
testza.AssertEqual(t, "a\n", result)
}

func TestInteractiveTextInputPrinter_OnLeftRight(t *testing.T) {
go func() {
keyboard.SimulateKeyPress(keys.Left)
keyboard.SimulateKeyPress("left")
keyboard.SimulateKeyPress(keys.Right)
keyboard.SimulateKeyPress("right")
keyboard.SimulateKeyPress(keys.Enter)
}()
result, _ := pterm.DefaultInteractiveTextInput.
WithDefaultValue("default").Show()
testza.AssertEqual(t, "defaullefttright", result)
}

func TestInteractiveTextInputPrinter_WithMultiLineOnLeftRight(t *testing.T) {
go func() {
time.Sleep(1 * time.Millisecond)
keyboard.SimulateKeyPress("a")
keyboard.SimulateKeyPress(keys.Enter)
keyboard.SimulateKeyPress("b")
keyboard.SimulateKeyPress(keys.Tab)
}()
result, _ := pterm.DefaultInteractiveTextInput.
WithMultiLine(true).
Show("Enter")
testza.AssertEqual(t, "a\nb", result)
}

func TestInteractiveTextInputPrinter_WithOnInterruptFunc(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions slog_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pterm

import (
"context"

"log/slog"
)

Expand Down

0 comments on commit 49ccf92

Please sign in to comment.