diff --git a/README.md b/README.md index faa3bc6..09d2532 100644 --- a/README.md +++ b/README.md @@ -158,7 +158,7 @@ Notice: `Meta`+`B` is equals with `Alt`+`B` in windows. | `Ctrl`+`I` / `Tab` | Command line completion | | `Ctrl`+`J` | Line feed | | `Ctrl`+`K` | Cut text to the end of line | -| `Ctrl`+`L` | Clean screen (TODO) | +| `Ctrl`+`L` | Clear screen | | `Ctrl`+`M` | Same as Enter key | | `Ctrl`+`N` / `↓` | Next line (in history) | | `Ctrl`+`P` / `↑` | Prev line (in history) | diff --git a/char.go b/char.go index 3e23865..e269640 100644 --- a/char.go +++ b/char.go @@ -12,6 +12,7 @@ const ( CharTab = 9 CharCtrlJ = 10 CharKill = 11 + CharCtrlL = 12 CharEnter = 13 CharNext = 14 CharPrev = 16 diff --git a/operation.go b/operation.go index 135928c..32289b3 100644 --- a/operation.go +++ b/operation.go @@ -190,6 +190,9 @@ func (o *Operation) ioloop() { o.buf.Clean() o.t.SleepToResume() o.Refresh() + case CharCtrlL: + ClearScreen(o.w) + o.Refresh() case MetaBackspace, CharCtrlW: o.buf.BackEscapeWord() case CharEnter, CharCtrlJ: diff --git a/utils_unix.go b/utils_unix.go index 2c9bc8e..addc5ba 100644 --- a/utils_unix.go +++ b/utils_unix.go @@ -3,6 +3,7 @@ package readline import ( + "io" "os" "os/signal" "sync" @@ -46,6 +47,11 @@ func GetScreenWidth() int { return getWidth(syscall.Stdout) } +// ClearScreen clears the console screen +func ClearScreen(w io.Writer) (int, error) { + return w.Write([]byte("\033[H")) +} + func DefaultIsTerminal() bool { return IsTerminal(syscall.Stdin) && IsTerminal(syscall.Stdout) } diff --git a/utils_windows.go b/utils_windows.go index d82d577..5bfa55d 100644 --- a/utils_windows.go +++ b/utils_windows.go @@ -2,7 +2,10 @@ package readline -import "syscall" +import ( + "io" + "syscall" +) func SuspendMe() { } @@ -24,6 +27,11 @@ func GetScreenWidth() int { return int(info.dwSize.x) } +// ClearScreen clears the console screen +func ClearScreen(_ io.Writer) error { + return SetConsoleCursorPosition(&_COORD{0, 0}) +} + func DefaultIsTerminal() bool { return true }