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

Expose a ClearScreen method #37

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions example/readline-demo/readline-demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -178,6 +180,8 @@ func main() {
} else {
log.Println("async writes already started")
}
case line == "clear":
slingamn marked this conversation as resolved.
Show resolved Hide resolved
l.ClearScreen()
case line == "":
default:
log.Println("you said:", strconv.Quote(line))
Expand Down
6 changes: 0 additions & 6 deletions internal/platform/utils_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package platform

import (
"context"
"io"
"os"
"os/signal"
"sync"
Expand Down Expand Up @@ -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))
}
Expand Down
6 changes: 0 additions & 6 deletions internal/platform/utils_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package platform

import (
"io"
"syscall"
)

Expand Down Expand Up @@ -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})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was weirdly named?

}

func DefaultIsTerminal() bool {
return true
}
Expand Down
6 changes: 1 addition & 5 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -547,7 +547,3 @@ func (o *operation) refresh() {
o.buf.Refresh(nil)
}
}

func (o *operation) Clean() {
o.buf.Clean()
}
11 changes: 7 additions & 4 deletions readline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -304,6 +302,11 @@ func (i *Instance) EnableHistory() {
i.operation.history.Enable()
}

// ClearScreen clears the screen.
func (i *Instance) ClearScreen() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not very familiar with the internals of this library, but in my experiments I had to call o.Clean() and o.Refresh() as well. Should this be part of the ClearScreen method?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you write to (*Instance).Stdout() or similar, it will refresh automatically via wrapWriter:

func (w *wrapWriter) Write(b []byte) (int, error) {

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
Expand Down
6 changes: 6 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package readline
import (
"container/list"
"fmt"
"io"
"os"
"sync"

Expand Down Expand Up @@ -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"))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe at some point named constant etc for ansi stuff could be nice for readability?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, maybe. What I've found in the past is that the composition rules are tricky. ircdog used to use mgutz/ansi --- I ended up removing it and handcrafting my own ANSI codes because I found that it obscured more than it clarified.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha i see, yeah i actually ran into something similar when trying to general ANSI-aware line wrapper for fq :)

return err
}

// -----------------------------------------------------------------------------

// print a linked list to Debug()
Expand Down