From bb91560e16d85df7ebea660bc105e57df205ac0d Mon Sep 17 00:00:00 2001 From: Cory Bennett Date: Mon, 17 Apr 2017 23:38:56 -0700 Subject: [PATCH 1/3] fix incompat issues between posix and windows EraseInLine arguments --- confirm.go | 2 +- input.go | 2 +- multiselect.go | 6 +++--- select.go | 6 +++--- terminal/display.go | 14 ++++++-------- terminal/display_posix.go | 11 +++++++++++ terminal/display_windows.go | 8 ++++---- 7 files changed, 29 insertions(+), 20 deletions(-) create mode 100644 terminal/display_posix.go diff --git a/confirm.go b/confirm.go index 786b4898..ad159c86 100644 --- a/confirm.go +++ b/confirm.go @@ -118,7 +118,7 @@ func (c *Confirm) Cleanup(rl *readline.Instance, val interface{}) error { // go up one line terminal.CursorPreviousLine(1) // clear the line - terminal.EraseInLine(0) + terminal.EraseInLine(terminal.ERASE_LINE_ALL) // the string version of the answer ans := "" diff --git a/input.go b/input.go index 3ca884db..8deae056 100644 --- a/input.go +++ b/input.go @@ -52,7 +52,7 @@ func (i *Input) Cleanup(rl *readline.Instance, val interface{}) error { // go up one line terminal.CursorPreviousLine(1) // clear the line - terminal.EraseInLine(0) + terminal.EraseInLine(terminal.ERASE_LINE_ALL) // render the template out, err := core.RunTemplate( diff --git a/multiselect.go b/multiselect.go index 7a0ff0e4..9f0b0271 100644 --- a/multiselect.go +++ b/multiselect.go @@ -75,7 +75,7 @@ func (m *MultiSelect) render() error { // clean up what we left behind last time for range m.Options { terminal.CursorPreviousLine(1) - terminal.EraseInLine(0) + terminal.EraseInLine(terminal.ERASE_LINE_ALL) } // render the template summarizing the current state @@ -170,10 +170,10 @@ func (m *MultiSelect) Prompt(rl *readline.Instance) (interface{}, error) { // Cleanup removes the options section, and renders the ask like a normal question. func (m *MultiSelect) Cleanup(rl *readline.Instance, val interface{}) error { terminal.CursorPreviousLine(1) - terminal.EraseInLine(0) + terminal.EraseInLine(terminal.ERASE_LINE_ALL) for range m.Options { terminal.CursorPreviousLine(1) - terminal.EraseInLine(0) + terminal.EraseInLine(terminal.ERASE_LINE_ALL) } // execute the output summary template with the answer diff --git a/select.go b/select.go index 109a9df7..ea2ec71e 100644 --- a/select.go +++ b/select.go @@ -65,7 +65,7 @@ func (s *Select) OnChange(line []rune, pos int, key rune) (newLine []rune, newPo func (s *Select) render() error { for range s.Options { terminal.CursorPreviousLine(1) - terminal.EraseInLine(0) + terminal.EraseInLine(terminal.ERASE_LINE_ALL) } // the formatted response @@ -153,10 +153,10 @@ func (s *Select) Prompt(rl *readline.Instance) (interface{}, error) { func (s *Select) Cleanup(rl *readline.Instance, val interface{}) error { terminal.CursorPreviousLine(1) - terminal.EraseInLine(0) + terminal.EraseInLine(terminal.ERASE_LINE_ALL) for range s.Options { terminal.CursorPreviousLine(1) - terminal.EraseInLine(0) + terminal.EraseInLine(terminal.ERASE_LINE_ALL) } // execute the output summary template with the answer diff --git a/terminal/display.go b/terminal/display.go index 53199f3e..894da162 100644 --- a/terminal/display.go +++ b/terminal/display.go @@ -1,11 +1,9 @@ -// +build !windows - package terminal -import ( - "fmt" -) +type EraseLineMode int -func EraseInLine(mode int) { - fmt.Printf("\x1b[%dK", mode) -} +const ( + ERASE_LINE_END EraseLineMode = iota + ERASE_LINE_START EraseLineMode = iota + ERASE_LINE_ALL EraseLineMode = iota +) diff --git a/terminal/display_posix.go b/terminal/display_posix.go new file mode 100644 index 00000000..06819dee --- /dev/null +++ b/terminal/display_posix.go @@ -0,0 +1,11 @@ +// +build !windows + +package terminal + +import ( + "fmt" +) + +func EraseInLine(mode EraseLineMode) { + fmt.Printf("\x1b[%dK", mode) +} diff --git a/terminal/display_windows.go b/terminal/display_windows.go index fea3313d..2d36e5df 100644 --- a/terminal/display_windows.go +++ b/terminal/display_windows.go @@ -6,7 +6,7 @@ import ( "unsafe" ) -func EraseInLine(mode int) { +func EraseInLine(mode EraseLineMode) { handle := syscall.Handle(os.Stdout.Fd()) var csbi consoleScreenBufferInfo @@ -16,11 +16,11 @@ func EraseInLine(mode int) { var x short cursor := csbi.cursorPosition switch mode { - case 1: + case ERASE_LINE_END: x = csbi.size.x - case 2: + case ERASE_LINE_START: x = 0 - case 3: + case ERASE_LINE_ALL: cursor.x = 0 x = csbi.size.x } From 9358f198a1dd17637bd6b02d494318be5e042894 Mon Sep 17 00:00:00 2001 From: Cory Bennett Date: Tue, 18 Apr 2017 00:19:23 -0700 Subject: [PATCH 2/3] remove redundant declarations --- terminal/display.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/terminal/display.go b/terminal/display.go index 894da162..0f014b13 100644 --- a/terminal/display.go +++ b/terminal/display.go @@ -3,7 +3,7 @@ package terminal type EraseLineMode int const ( - ERASE_LINE_END EraseLineMode = iota - ERASE_LINE_START EraseLineMode = iota - ERASE_LINE_ALL EraseLineMode = iota + ERASE_LINE_END EraseLineMode = iota + ERASE_LINE_START + ERASE_LINE_ALL ) From 6a1b406966f1059e889eddd915611646ddd709d8 Mon Sep 17 00:00:00 2001 From: Cory Bennett Date: Tue, 18 Apr 2017 00:20:46 -0700 Subject: [PATCH 3/3] rename EraseInLine to EraseLine --- confirm.go | 2 +- input.go | 2 +- multiselect.go | 6 +++--- select.go | 6 +++--- terminal/display_posix.go | 2 +- terminal/display_windows.go | 2 +- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/confirm.go b/confirm.go index ad159c86..25f735bc 100644 --- a/confirm.go +++ b/confirm.go @@ -118,7 +118,7 @@ func (c *Confirm) Cleanup(rl *readline.Instance, val interface{}) error { // go up one line terminal.CursorPreviousLine(1) // clear the line - terminal.EraseInLine(terminal.ERASE_LINE_ALL) + terminal.EraseLine(terminal.ERASE_LINE_ALL) // the string version of the answer ans := "" diff --git a/input.go b/input.go index 8deae056..4b27ac0c 100644 --- a/input.go +++ b/input.go @@ -52,7 +52,7 @@ func (i *Input) Cleanup(rl *readline.Instance, val interface{}) error { // go up one line terminal.CursorPreviousLine(1) // clear the line - terminal.EraseInLine(terminal.ERASE_LINE_ALL) + terminal.EraseLine(terminal.ERASE_LINE_ALL) // render the template out, err := core.RunTemplate( diff --git a/multiselect.go b/multiselect.go index 9f0b0271..bcf10114 100644 --- a/multiselect.go +++ b/multiselect.go @@ -75,7 +75,7 @@ func (m *MultiSelect) render() error { // clean up what we left behind last time for range m.Options { terminal.CursorPreviousLine(1) - terminal.EraseInLine(terminal.ERASE_LINE_ALL) + terminal.EraseLine(terminal.ERASE_LINE_ALL) } // render the template summarizing the current state @@ -170,10 +170,10 @@ func (m *MultiSelect) Prompt(rl *readline.Instance) (interface{}, error) { // Cleanup removes the options section, and renders the ask like a normal question. func (m *MultiSelect) Cleanup(rl *readline.Instance, val interface{}) error { terminal.CursorPreviousLine(1) - terminal.EraseInLine(terminal.ERASE_LINE_ALL) + terminal.EraseLine(terminal.ERASE_LINE_ALL) for range m.Options { terminal.CursorPreviousLine(1) - terminal.EraseInLine(terminal.ERASE_LINE_ALL) + terminal.EraseLine(terminal.ERASE_LINE_ALL) } // execute the output summary template with the answer diff --git a/select.go b/select.go index ea2ec71e..7f8ba7e0 100644 --- a/select.go +++ b/select.go @@ -65,7 +65,7 @@ func (s *Select) OnChange(line []rune, pos int, key rune) (newLine []rune, newPo func (s *Select) render() error { for range s.Options { terminal.CursorPreviousLine(1) - terminal.EraseInLine(terminal.ERASE_LINE_ALL) + terminal.EraseLine(terminal.ERASE_LINE_ALL) } // the formatted response @@ -153,10 +153,10 @@ func (s *Select) Prompt(rl *readline.Instance) (interface{}, error) { func (s *Select) Cleanup(rl *readline.Instance, val interface{}) error { terminal.CursorPreviousLine(1) - terminal.EraseInLine(terminal.ERASE_LINE_ALL) + terminal.EraseLine(terminal.ERASE_LINE_ALL) for range s.Options { terminal.CursorPreviousLine(1) - terminal.EraseInLine(terminal.ERASE_LINE_ALL) + terminal.EraseLine(terminal.ERASE_LINE_ALL) } // execute the output summary template with the answer diff --git a/terminal/display_posix.go b/terminal/display_posix.go index 06819dee..47a765c7 100644 --- a/terminal/display_posix.go +++ b/terminal/display_posix.go @@ -6,6 +6,6 @@ import ( "fmt" ) -func EraseInLine(mode EraseLineMode) { +func EraseLine(mode EraseLineMode) { fmt.Printf("\x1b[%dK", mode) } diff --git a/terminal/display_windows.go b/terminal/display_windows.go index 2d36e5df..31c03bf2 100644 --- a/terminal/display_windows.go +++ b/terminal/display_windows.go @@ -6,7 +6,7 @@ import ( "unsafe" ) -func EraseInLine(mode EraseLineMode) { +func EraseLine(mode EraseLineMode) { handle := syscall.Handle(os.Stdout.Fd()) var csbi consoleScreenBufferInfo