From f382f4db4a4bf5f71ee29ad88c8c83a7b5e436c5 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Fri, 31 Jan 2020 13:47:36 +0100 Subject: [PATCH] Move ansi helpers to termenv --- ansi.go | 64 ------------------------------------- examples/fullscreen/main.go | 6 ++-- examples/go.mod | 1 + examples/go.sum | 9 ++++++ go.mod | 1 + go.sum | 9 ++++++ tea.go | 4 ++- tty_unix.go | 5 +-- tty_windows.go | 6 ++-- 9 files changed, 34 insertions(+), 71 deletions(-) delete mode 100644 ansi.go diff --git a/ansi.go b/ansi.go deleted file mode 100644 index b682405..0000000 --- a/ansi.go +++ /dev/null @@ -1,64 +0,0 @@ -package tea - -import "fmt" - -// Escape sequence -const esc = "\033[" - -// Fullscreen switches to the altscreen and clears the terminal. The former -// view can be restored with ExitFullscreen(). -func Fullscreen() { - fmt.Print(esc + "?1049h" + esc + "H") -} - -// ExitFullscreen exits the altscreen and returns the former terminal view -func ExitFullscreen() { - fmt.Print(esc + "?1049l") -} - -// ClearScreen clears the visible portion of the terminal. Effectively, it -// fills the terminal with blank spaces. -func ClearScreen() { - fmt.Printf(esc + "2J" + esc + "3J" + esc + "1;1H") -} - -// Invert inverts the foreground and background colors of a given string -func Invert(s string) string { - return esc + "7m" + s + esc + "0m" -} - -// Hide the cursor -func hideCursor() { - fmt.Printf(esc + "?25l") -} - -// Show the cursor -func showCursor() { - fmt.Printf(esc + "?25h") -} - -// Move the cursor down a given number of lines and place it at the beginning -// of the line -func cursorNextLine(n int) { - fmt.Printf(esc+"%dE", n) -} - -// Move the cursor up a given number of lines and place it at the beginning of -// the line -func cursorPrevLine(n int) { - fmt.Printf(esc+"%dF", n) -} - -// Clear the current line -func clearLine() { - fmt.Printf(esc + "2K") -} - -// Clear a given number of lines -func clearLines(n int) { - clearLine() - for i := 0; i < n; i++ { - cursorPrevLine(1) - clearLine() - } -} diff --git a/examples/fullscreen/main.go b/examples/fullscreen/main.go index 90d5d05..27a9707 100644 --- a/examples/fullscreen/main.go +++ b/examples/fullscreen/main.go @@ -8,6 +8,7 @@ import ( "time" "github.com/charmbracelet/tea" + "github.com/muesli/termenv" ) type model int @@ -15,8 +16,9 @@ type model int type tickMsg struct{} func main() { - tea.Fullscreen() - defer tea.ExitFullscreen() + termenv.AltScreen() + termenv.MoveCursor(1, 1) + defer termenv.ExitAltScreen() err := tea.NewProgram(initialize, update, view, subscriptions).Start() if err != nil { log.Fatal(err) diff --git a/examples/go.mod b/examples/go.mod index cba8229..d3776eb 100644 --- a/examples/go.mod +++ b/examples/go.mod @@ -8,4 +8,5 @@ require ( github.com/charmbracelet/tea v0.0.0-20200126032228-29799c315162 github.com/charmbracelet/teaparty v0.0.0-20200126032257-419df0b7a206 github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776 + github.com/muesli/termenv v0.4.1-0.20200131124310-936567584c3e ) diff --git a/examples/go.sum b/examples/go.sum index 0ab1bb2..1fe6c0b 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -4,7 +4,16 @@ github.com/charmbracelet/teaparty v0.0.0-20200126032257-419df0b7a206 h1:sjDz9mRG github.com/charmbracelet/teaparty v0.0.0-20200126032257-419df0b7a206/go.mod h1:pmTejx7xSMAHuh8aY5r1cwNzNiv0akVFNPeiae+e6RQ= github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776 h1:VRIbnDWRmAh5yBdz+J6yFMF5vso1It6vn+WmM/5l7MA= github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776/go.mod h1:9wvnDu3YOfxzWM9Cst40msBF1C2UdQgDv962oTxSuMs= +github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f h1:5CjVwnuUcp5adK4gmY6i72gpVFVnZDP2h5TmPScB6u4= +github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f/go.mod h1:nOFQdrUlIlx6M6ODdSpBj1NVA+VgLC6kmw60mkw34H4= +github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac= +github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= +github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/muesli/termenv v0.4.1-0.20200131124310-936567584c3e h1:6exaizu60WAeTP998SgBox6eaSs6rT36fR/0ebsrnOQ= +github.com/muesli/termenv v0.4.1-0.20200131124310-936567584c3e/go.mod h1:O1/I6sw+6KcrgAmcs6uiUVr7Lui+DNVbHTzt9Lm/PlI= github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942 h1:A7GG7zcGjl3jqAqGPmcNjd/D9hzL95SuoOQAaFNdLU0= github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942/go.mod h1:eCbImbZ95eXtAUIbLAuAVnBnwf83mjf6QIVH8SHYwqQ= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200120151820-655fe14d7479 h1:LhLiKguPgZL+Tglay4GhVtfF0kb8cvOJ0dHTCBO8YNI= golang.org/x/sys v0.0.0-20200120151820-655fe14d7479/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/go.mod b/go.mod index d192701..f9863d3 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/charmbracelet/tea go 1.13 require ( + github.com/muesli/termenv v0.4.1-0.20200131124310-936567584c3e github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942 golang.org/x/sys v0.0.0-20200120151820-655fe14d7479 // indirect ) diff --git a/go.sum b/go.sum index 5d1a519..d8ffc40 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,13 @@ +github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f h1:5CjVwnuUcp5adK4gmY6i72gpVFVnZDP2h5TmPScB6u4= +github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f/go.mod h1:nOFQdrUlIlx6M6ODdSpBj1NVA+VgLC6kmw60mkw34H4= +github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac= +github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= +github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/muesli/termenv v0.4.1-0.20200131124310-936567584c3e h1:6exaizu60WAeTP998SgBox6eaSs6rT36fR/0ebsrnOQ= +github.com/muesli/termenv v0.4.1-0.20200131124310-936567584c3e/go.mod h1:O1/I6sw+6KcrgAmcs6uiUVr7Lui+DNVbHTzt9Lm/PlI= github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942 h1:A7GG7zcGjl3jqAqGPmcNjd/D9hzL95SuoOQAaFNdLU0= github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942/go.mod h1:eCbImbZ95eXtAUIbLAuAVnBnwf83mjf6QIVH8SHYwqQ= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200120151820-655fe14d7479 h1:LhLiKguPgZL+Tglay4GhVtfF0kb8cvOJ0dHTCBO8YNI= golang.org/x/sys v0.0.0-20200120151820-655fe14d7479/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/tea.go b/tea.go index cfeb5f0..602b8a0 100644 --- a/tea.go +++ b/tea.go @@ -5,6 +5,8 @@ import ( "io" "os" "strings" + + "github.com/muesli/termenv" ) // Msg represents an action. It's used by Update to update the UI. @@ -192,7 +194,7 @@ func (p *Program) render(model Model, linesRendered int) int { view = strings.Replace(view, "\n", "\r\n", -1) if linesRendered > 0 { - clearLines(linesRendered) + termenv.ClearLines(linesRendered) } io.WriteString(os.Stdout, view) return strings.Count(view, "\r\n") diff --git a/tty_unix.go b/tty_unix.go index 67ae58f..b6a92d3 100644 --- a/tty_unix.go +++ b/tty_unix.go @@ -3,6 +3,7 @@ package tea import ( + "github.com/muesli/termenv" "github.com/pkg/term" ) @@ -18,11 +19,11 @@ func initTerminal() error { } tty.SetRaw() - hideCursor() + termenv.HideCursor() return nil } func restoreTerminal() { - showCursor() + termenv.ShowCursor() tty.Restore() } diff --git a/tty_windows.go b/tty_windows.go index 35ae455..89d7e24 100644 --- a/tty_windows.go +++ b/tty_windows.go @@ -2,11 +2,13 @@ package tea +import "github.com/muesli/termenv" + func initTerminal() error { - hideCursor() + termenv.HideCursor() return nil } func restoreTerminal() { - showCursor() + termenv.ShowCursor() }