-
Notifications
You must be signed in to change notification settings - Fork 358
Clear to end of screen before redrawing prompt #476
base: master
Are you sure you want to change the base?
Changes from 6 commits
338c156
9decb76
363d3eb
cf89954
3090f93
38e598c
9e539c3
b96ec7a
d29d6fa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,18 +47,24 @@ func (c *Cursor) Back(n int) error { | |
|
||
// NextLine moves cursor to beginning of the line n lines down. | ||
func (c *Cursor) NextLine(n int) error { | ||
if err := c.Down(1); err != nil { | ||
if err := c.HorizontalAbsolute(0); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why first move horizontal and then try to move between lines? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There seems to be weird behavior when going It might be more appropriate to move this check for |
||
return err | ||
} | ||
return c.HorizontalAbsolute(0) | ||
if n == 0 { | ||
return nil | ||
} | ||
return c.Down(n) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice catch that this didn't use to forward the |
||
} | ||
|
||
// PreviousLine moves cursor to beginning of the line n lines up. | ||
func (c *Cursor) PreviousLine(n int) error { | ||
if err := c.Up(1); err != nil { | ||
if err := c.HorizontalAbsolute(0); err != nil { | ||
return err | ||
} | ||
return c.HorizontalAbsolute(0) | ||
if n == 0 { | ||
return nil | ||
} | ||
return c.Up(n) | ||
} | ||
|
||
// HorizontalAbsolute moves cursor horizontally to x. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,25 @@ | ||
package terminal | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
type EraseLineMode int | ||
type EraseScreenMode int | ||
|
||
const ( | ||
ERASE_LINE_END EraseLineMode = iota | ||
ERASE_LINE_START | ||
ERASE_LINE_ALL | ||
) | ||
|
||
const ( | ||
ERASE_SCREEN_END EraseScreenMode = iota | ||
ERASE_SCREEN_START | ||
ERASE_SCREEN_ALL | ||
) | ||
|
||
func EraseScreen(out FileWriter, mode EraseScreenMode) error { | ||
_, err := fmt.Fprintf(out, "\x1b[%dJ", mode) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. on Windows we can't just assume that printing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Completely agree! I'd be interested in knowing if a modified version of this example could be used to clear only from the current cursor position to the end of the screen? I have hope that the |
||
return err | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make this simpler to read at the cost of tiny repetition:
I'm curious about where these constants
2
and3
come from. From the old code, it looks like the number of lines to clear was calculated by taking the length ofmultiline
and adding 2 to that (I guess to account for two blank lines). Now, it either deletes 2 or 3 lines up and not more than that. Is the size ofmultiline
taken into account somewhere when redrawing?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those magic numbers were a hacky way to handle the empty case and confused me quite a bit... There was something odd going on when using a modified input value while rendering the template1. Removing the newline from the answer but using the unmodified value in the template seems to clear this logic up some!
Also wanted to share that
Multiline
only moves the cursor location to account for the two blank lines and letsresetPrompt
move the remainingn
lines before clearing the screen. Really good call about checking into howmultiline
is redrawn!Footnotes
Removing the trailing newline from
multiline
before appending torenderTemplate
caused this template to differ from what was actually being displayed for the empty input. The missing newline in therenderTemplate
meant thatcountLines
undercounted what was actually rendered and socursor.PreviousLine(3)
over adjusted, how odd! ↩