Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Commit

Permalink
Fix prompt reset on some terminals - fixes Konsole (#302)
Browse files Browse the repository at this point in the history
* Fix prompt reset on some terminals #300

* Remove PreviousLine and NextLine and replace them with Up and Down

* Put pack PreviousLine and NextLine

To avoid breaking change. Add deprecated notice
  • Loading branch information
System-Glitch committed Oct 23, 2020
1 parent dbdb3e5 commit 8780050
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 18 deletions.
2 changes: 1 addition & 1 deletion confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (c *Confirm) getBool(showHelp bool, config *PromptConfig) (bool, error) {
return false, err
}
// move back up a line to compensate for the \n echoed from terminal
cursor.PreviousLine(1)
cursor.Up(1)
val := string(line)

// get the answer that matches the
Expand Down
2 changes: 1 addition & 1 deletion input.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (i *Input) Prompt(config *PromptConfig) (interface{}, error) {
return string(line), err
}
// terminal will echo the \n so we need to jump back up one row
cursor.PreviousLine(1)
cursor.Up(1)

if string(line) == config.HelpInput && i.Help != "" {
err = i.Render(
Expand Down
6 changes: 3 additions & 3 deletions multiline.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ func (i *Multiline) Prompt(config *PromptConfig) (interface{}, error) {
if string(line) == "" {
if emptyOnce {
numLines := len(multiline) + 2
cursor.PreviousLine(numLines)
cursor.Up(numLines)
for j := 0; j < numLines; j++ {
terminal.EraseLine(i.Stdio().Out, terminal.ERASE_LINE_ALL)
cursor.NextLine(1)
cursor.Down(1)
}
cursor.PreviousLine(numLines)
cursor.Up(numLines)
break
}
emptyOnce = true
Expand Down
2 changes: 1 addition & 1 deletion password.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (p *Password) Prompt(config *PromptConfig) (interface{}, error) {

if string(line) == config.HelpInput {
// terminal will echo the \n so we need to jump back up one row
cursor.PreviousLine(1)
cursor.Up(1)

err = p.Render(
PasswordQuestionTemplate,
Expand Down
2 changes: 1 addition & 1 deletion renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (r *Renderer) resetPrompt(lines int) {
terminal.EraseLine(r.stdio.Out, terminal.ERASE_LINE_ALL)
// clean up what we left behind last time
for i := 0; i < lines; i++ {
cursor.PreviousLine(1)
cursor.Up(1)
terminal.EraseLine(r.stdio.Out, terminal.ERASE_LINE_ALL)
}
}
Expand Down
4 changes: 3 additions & 1 deletion terminal/cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ func (c *Cursor) Back(n int) {
}

// NextLine moves cursor to beginning of the line n lines down.
// DEPRECATED: use Down() instead
func (c *Cursor) NextLine(n int) {
fmt.Fprintf(c.Out, "\x1b[%dE", n)
}

// PreviousLine moves cursor to beginning of the line n lines up.
// DEPRECATED: use Up() instead
func (c *Cursor) PreviousLine(n int) {
fmt.Fprintf(c.Out, "\x1b[%dF", n)
}
Expand Down Expand Up @@ -86,7 +88,7 @@ func (c *Cursor) MoveNextLine(cur *Coord, terminalSize *Coord) {
if cur.Y == terminalSize.Y {
fmt.Fprintln(c.Out)
}
c.NextLine(1)
c.Down(1)
}

// Location returns the current location of the cursor in the terminal
Expand Down
20 changes: 10 additions & 10 deletions terminal/runereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (rr *RuneReader) ReadLine(mask rune) ([]rune, error) {
for index > 0 {
if cursorCurrent.CursorIsAtLineBegin() {
EraseLine(rr.stdio.Out, ERASE_LINE_END)
cursor.PreviousLine(1)
cursor.Up(1)
cursor.Forward(int(terminalSize.X))
cursorCurrent.X = terminalSize.X
cursorCurrent.Y--
Expand Down Expand Up @@ -99,7 +99,7 @@ func (rr *RuneReader) ReadLine(mask rune) ([]rune, error) {
line = line[:len(line)-1]
// go back one
if cursorCurrent.X == 1 {
cursor.PreviousLine(1)
cursor.Up(1)
cursor.Forward(int(terminalSize.X))
} else {
cursor.Back(cells)
Expand Down Expand Up @@ -133,13 +133,13 @@ func (rr *RuneReader) ReadLine(mask rune) ([]rune, error) {
}
// erase what's left over from last print
if cursorCurrent.Y < terminalSize.Y {
cursor.NextLine(1)
cursor.Down(1)
EraseLine(rr.stdio.Out, ERASE_LINE_END)
}
// restore cursor
cursor.Restore()
if cursorCurrent.CursorIsAtLineBegin() {
cursor.PreviousLine(1)
cursor.Up(1)
cursor.Forward(int(terminalSize.X))
} else {
cursor.Back(cells)
Expand All @@ -163,7 +163,7 @@ func (rr *RuneReader) ReadLine(mask rune) ([]rune, error) {
if index > 0 {
//move the cursor to the prev line if necessary
if cursorCurrent.CursorIsAtLineBegin() {
cursor.PreviousLine(1)
cursor.Up(1)
cursor.Forward(int(terminalSize.X))
} else {
cursor.Back(runeWidth(line[index-1]))
Expand All @@ -187,7 +187,7 @@ func (rr *RuneReader) ReadLine(mask rune) ([]rune, error) {
if index < len(line) {
// move the cursor to the next line if necessary
if cursorCurrent.CursorIsAtLineEnd(terminalSize) {
cursor.NextLine(1)
cursor.Down(1)
} else {
cursor.Forward(runeWidth(line[index]))
}
Expand All @@ -206,7 +206,7 @@ func (rr *RuneReader) ReadLine(mask rune) ([]rune, error) {
if r == SpecialKeyHome {
for index > 0 {
if cursorCurrent.CursorIsAtLineBegin() {
cursor.PreviousLine(1)
cursor.Up(1)
cursor.Forward(int(terminalSize.X))
cursorCurrent.X = terminalSize.X
cursorCurrent.Y--
Expand All @@ -222,7 +222,7 @@ func (rr *RuneReader) ReadLine(mask rune) ([]rune, error) {
} else if r == SpecialKeyEnd {
for index != len(line) {
if cursorCurrent.CursorIsAtLineEnd(terminalSize) {
cursor.NextLine(1)
cursor.Down(1)
cursorCurrent.X = COORDINATE_SYSTEM_BEGIN
cursorCurrent.Y++

Expand Down Expand Up @@ -251,7 +251,7 @@ func (rr *RuneReader) ReadLine(mask rune) ([]rune, error) {
}
// erase what's left on last line
if cursorCurrent.Y < terminalSize.Y {
cursor.NextLine(1)
cursor.Down(1)
EraseLine(rr.stdio.Out, ERASE_LINE_END)
}
// restore cursor
Expand Down Expand Up @@ -310,7 +310,7 @@ func (rr *RuneReader) ReadLine(mask rune) ([]rune, error) {
// check if cursor needs to move to next line
cursorCurrent, _ = cursor.Location(rr.Buffer())
if cursorCurrent.CursorIsAtLineEnd(terminalSize) {
cursor.NextLine(1)
cursor.Down(1)
} else {
cursor.Forward(runeWidth(r))
}
Expand Down

0 comments on commit 8780050

Please sign in to comment.