Repository: pterm/pterm (go)

Issue:
Default Value for InteractiveTextInput
I would like a default value set for InteractiveTextInput, I have some cases where i would like to prefill the input for easier editing

Pull request:
Feature: Default value for interactive text input
### Description
Add a default value option for interactive text input.
Also, introduced `WithDefaultValue` method and included additional printer test cases.

### Scope
`InteractiveTextInput`

- [ ] Bug Fix
- [x] New Feature
- [x] Documentation
- [ ] Other

### Related Issue
Fixes #576 


### To-Do Checklist
- [x] I tested my changes
- [x] I have commented every method that I created/changed
- [x] I updated the examples to fit with my changes
- [x] I have added tests for my newly created methods


Patch:
diff --git a/interactive_textinput_printer.go b/interactive_textinput_printer.go
index 2514ab12..88535c51 100644
--- a/interactive_textinput_printer.go
+++ b/interactive_textinput_printer.go
@@ -22,6 +22,7 @@ var DefaultInteractiveTextInput = InteractiveTextInputPrinter{
 type InteractiveTextInputPrinter struct {
 	TextStyle       *Style
 	DefaultText     string
+	DefaultValue    string
 	Delimiter       string
 	MultiLine       bool
 	Mask            string
@@ -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
@@ -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)
@@ -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
diff --git a/slog_handler.go b/slog_handler.go
index db35617e..d0854d04 100644
--- a/slog_handler.go
+++ b/slog_handler.go
@@ -2,6 +2,7 @@ package pterm
 
 import (
 	"context"
+
 	"log/slog"
 )
 


Test patch:
diff --git a/interactive_textinput_printer_test.go b/interactive_textinput_printer_test.go
index c86506e0..2f925a02 100644
--- a/interactive_textinput_printer_test.go
+++ b/interactive_textinput_printer_test.go
@@ -3,6 +3,7 @@ package pterm_test
 import (
 	"reflect"
 	"testing"
+	"time"
 
 	"atomicgo.dev/keyboard"
 	"atomicgo.dev/keyboard/keys"
@@ -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, ">>")
@@ -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) {