From f9d0a5b297c0663c49778c0e14109dc24a35f0b3 Mon Sep 17 00:00:00 2001 From: Shivaram Lingamneni Date: Sun, 5 Nov 2023 02:05:54 -0500 Subject: [PATCH 1/2] Expose a ClearScreen method For parity with the original library; fixes #36 --- example/readline-demo/readline-demo.go | 2 ++ internal/platform/utils_unix.go | 6 ------ internal/platform/utils_windows.go | 6 ------ operation.go | 6 +----- readline.go | 11 +++++++---- utils.go | 6 ++++++ 6 files changed, 16 insertions(+), 21 deletions(-) diff --git a/example/readline-demo/readline-demo.go b/example/readline-demo/readline-demo.go index 09c54ce..f878135 100644 --- a/example/readline-demo/readline-demo.go +++ b/example/readline-demo/readline-demo.go @@ -178,6 +178,8 @@ func main() { } else { log.Println("async writes already started") } + case line == "clear": + l.ClearScreen() case line == "": default: log.Println("you said:", strconv.Quote(line)) diff --git a/internal/platform/utils_unix.go b/internal/platform/utils_unix.go index 51ec230..caed6fa 100644 --- a/internal/platform/utils_unix.go +++ b/internal/platform/utils_unix.go @@ -4,7 +4,6 @@ package platform import ( "context" - "io" "os" "os/signal" "sync" @@ -74,11 +73,6 @@ func GetScreenSize() (width int, height int) { return } -// ClearScreen clears the console screen -func ClearScreen(w io.Writer) (int, error) { - return w.Write([]byte("\033[H")) -} - func DefaultIsTerminal() bool { return term.IsTerminal(syscall.Stdin) && (term.IsTerminal(syscall.Stdout) || term.IsTerminal(syscall.Stderr)) } diff --git a/internal/platform/utils_windows.go b/internal/platform/utils_windows.go index 69f4695..f4a6f52 100644 --- a/internal/platform/utils_windows.go +++ b/internal/platform/utils_windows.go @@ -3,7 +3,6 @@ package platform import ( - "io" "syscall" ) @@ -38,11 +37,6 @@ func GetScreenSize() (width int, height int) { return } -// ClearScreen clears the console screen -func ClearScreen(_ io.Writer) error { - return SetConsoleCursorPosition(&_COORD{0, 0}) -} - func DefaultIsTerminal() bool { return true } diff --git a/operation.go b/operation.go index 8d275b4..717ec57 100644 --- a/operation.go +++ b/operation.go @@ -221,7 +221,7 @@ func (o *operation) readline(deadline chan struct{}) ([]rune, error) { o.Refresh() } case CharCtrlL: - platform.ClearScreen(o.t) + clearScreen(o.t) o.buf.SetOffset(cursorPosition{1, 1}) o.Refresh() case MetaBackspace, CharCtrlW: @@ -547,7 +547,3 @@ func (o *operation) refresh() { o.buf.Refresh(nil) } } - -func (o *operation) Clean() { - o.buf.Clean() -} diff --git a/readline.go b/readline.go index 7d47632..48c2990 100644 --- a/readline.go +++ b/readline.go @@ -267,10 +267,8 @@ func (i *Instance) CaptureExitSignal() { }() } -func (i *Instance) Clean() { - i.operation.Clean() -} - +// Write writes output to the screen, redrawing the prompt and buffer +// as needed. func (i *Instance) Write(b []byte) (int, error) { return i.Stdout().Write(b) } @@ -304,6 +302,11 @@ func (i *Instance) EnableHistory() { i.operation.history.Enable() } +// ClearScreen clears the screen. +func (i *Instance) ClearScreen() { + clearScreen(i.operation.Stdout()) +} + // Painter is a callback type to allow modifying the buffer before it is rendered // on screen, for example, to implement real-time syntax highlighting. type Painter func(line []rune, pos int) []rune diff --git a/utils.go b/utils.go index 9238b67..709d0f2 100644 --- a/utils.go +++ b/utils.go @@ -3,6 +3,7 @@ package readline import ( "container/list" "fmt" + "io" "os" "sync" @@ -74,6 +75,11 @@ func (r *rawModeHandler) Exit() error { return err } +func clearScreen(w io.Writer) error { + _, err := w.Write([]byte("\x1b[H\x1b[J")) + return err +} + // ----------------------------------------------------------------------------- // print a linked list to Debug() From ab816e23020309760f6a1a4b5f40a92d7bbf7d81 Mon Sep 17 00:00:00 2001 From: Shivaram Lingamneni Date: Sun, 5 Nov 2023 08:28:11 -0500 Subject: [PATCH 2/2] add 'async' and 'clear' to demo completer --- example/readline-demo/readline-demo.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/example/readline-demo/readline-demo.go b/example/readline-demo/readline-demo.go index f878135..3529565 100644 --- a/example/readline-demo/readline-demo.go +++ b/example/readline-demo/readline-demo.go @@ -60,6 +60,8 @@ var completer = readline.NewPrefixCompleter( readline.PcItem("test"), ), readline.PcItem("sleep"), + readline.PcItem("async"), + readline.PcItem("clear"), ) func filterInput(r rune) (rune, bool) {