Skip to content
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

fix(textinput): fixed buggy behavior #550

Merged
merged 1 commit into from
Aug 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 12 additions & 14 deletions interactive_textinput_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (p InteractiveTextInputPrinter) WithMask(mask string) *InteractiveTextInput
return &p
}

// OnInterrupt sets the function to execute on exit of the input reader
// WithOnInterruptFunc sets the function to execute on exit of the input reader
func (p InteractiveTextInputPrinter) WithOnInterruptFunc(exitFunc func()) *InteractiveTextInputPrinter {
p.OnInterruptFunc = exitFunc
return &p
Expand Down Expand Up @@ -88,19 +88,15 @@ func (p InteractiveTextInputPrinter) Show(text ...string) (string, error) {
areaText = p.TextStyle.Sprintf("%s%s", text[0], p.Delimiter)
}
p.text = areaText
area, err := DefaultArea.Start(areaText)
defer area.Stop()
if err != nil {
return "", err
}
area := cursor.NewArea()
area.Update(areaText)
area.StartOfLine()

cursor.Up(1)
cursor.StartOfLine()
if !p.MultiLine {
cursor.Right(len(RemoveColorFromString(areaText)))
}

err = keyboard.Listen(func(key keys.Key) (stop bool, err error) {
err := keyboard.Listen(func(key keys.Key) (stop bool, err error) {
if !p.MultiLine {
p.cursorYPos = 0
}
Expand All @@ -111,6 +107,7 @@ func (p InteractiveTextInputPrinter) Show(text ...string) (string, error) {
switch key.Code {
case keys.Tab:
if p.MultiLine {
area.Bottom()
return true, nil
}
case keys.Enter:
Expand Down Expand Up @@ -194,7 +191,7 @@ func (p InteractiveTextInputPrinter) Show(text ...string) (string, error) {
}
}

p.updateArea(area)
p.updateArea(&area)

return false, nil
})
Expand All @@ -216,7 +213,7 @@ func (p InteractiveTextInputPrinter) Show(text ...string) (string, error) {
return strings.ReplaceAll(areaText, p.text, ""), nil
}

func (p InteractiveTextInputPrinter) updateArea(area *AreaPrinter) string {
func (p InteractiveTextInputPrinter) updateArea(area *cursor.Area) string {
if !p.MultiLine {
p.cursorYPos = 0
}
Expand All @@ -238,10 +235,11 @@ func (p InteractiveTextInputPrinter) updateArea(area *AreaPrinter) string {
p.cursorXPos = -internal.GetStringMaxWidth(p.input[p.cursorYPos])
}

cursor.StartOfLine()
area.StartOfLine()
area.Update(areaText)
cursor.Up(len(p.input) - p.cursorYPos)
cursor.StartOfLine()
area.Top()
area.Down(p.cursorYPos + 1)
area.StartOfLine()
if p.MultiLine {
cursor.Right(internal.GetStringMaxWidth(p.input[p.cursorYPos]) + p.cursorXPos)
} else {
Expand Down