Move ansi helpers to termenv

This commit is contained in:
Christian Muehlhaeuser 2020-01-31 13:47:36 +01:00
parent bb06373836
commit f382f4db4a
No known key found for this signature in database
GPG Key ID: 3CF9FA45CA1EBB7E
9 changed files with 34 additions and 71 deletions

64
ansi.go
View File

@ -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()
}
}

View File

@ -8,6 +8,7 @@ import (
"time" "time"
"github.com/charmbracelet/tea" "github.com/charmbracelet/tea"
"github.com/muesli/termenv"
) )
type model int type model int
@ -15,8 +16,9 @@ type model int
type tickMsg struct{} type tickMsg struct{}
func main() { func main() {
tea.Fullscreen() termenv.AltScreen()
defer tea.ExitFullscreen() termenv.MoveCursor(1, 1)
defer termenv.ExitAltScreen()
err := tea.NewProgram(initialize, update, view, subscriptions).Start() err := tea.NewProgram(initialize, update, view, subscriptions).Start()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)

View File

@ -8,4 +8,5 @@ require (
github.com/charmbracelet/tea v0.0.0-20200126032228-29799c315162 github.com/charmbracelet/tea v0.0.0-20200126032228-29799c315162
github.com/charmbracelet/teaparty v0.0.0-20200126032257-419df0b7a206 github.com/charmbracelet/teaparty v0.0.0-20200126032257-419df0b7a206
github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776 github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776
github.com/muesli/termenv v0.4.1-0.20200131124310-936567584c3e
) )

View File

@ -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/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 h1:VRIbnDWRmAh5yBdz+J6yFMF5vso1It6vn+WmM/5l7MA=
github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776/go.mod h1:9wvnDu3YOfxzWM9Cst40msBF1C2UdQgDv962oTxSuMs= 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 h1:A7GG7zcGjl3jqAqGPmcNjd/D9hzL95SuoOQAaFNdLU0=
github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942/go.mod h1:eCbImbZ95eXtAUIbLAuAVnBnwf83mjf6QIVH8SHYwqQ= 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 h1:LhLiKguPgZL+Tglay4GhVtfF0kb8cvOJ0dHTCBO8YNI=
golang.org/x/sys v0.0.0-20200120151820-655fe14d7479/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200120151820-655fe14d7479/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

1
go.mod
View File

@ -3,6 +3,7 @@ module github.com/charmbracelet/tea
go 1.13 go 1.13
require ( require (
github.com/muesli/termenv v0.4.1-0.20200131124310-936567584c3e
github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942 github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942
golang.org/x/sys v0.0.0-20200120151820-655fe14d7479 // indirect golang.org/x/sys v0.0.0-20200120151820-655fe14d7479 // indirect
) )

9
go.sum
View File

@ -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 h1:A7GG7zcGjl3jqAqGPmcNjd/D9hzL95SuoOQAaFNdLU0=
github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942/go.mod h1:eCbImbZ95eXtAUIbLAuAVnBnwf83mjf6QIVH8SHYwqQ= 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 h1:LhLiKguPgZL+Tglay4GhVtfF0kb8cvOJ0dHTCBO8YNI=
golang.org/x/sys v0.0.0-20200120151820-655fe14d7479/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200120151820-655fe14d7479/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

4
tea.go
View File

@ -5,6 +5,8 @@ import (
"io" "io"
"os" "os"
"strings" "strings"
"github.com/muesli/termenv"
) )
// Msg represents an action. It's used by Update to update the UI. // 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) view = strings.Replace(view, "\n", "\r\n", -1)
if linesRendered > 0 { if linesRendered > 0 {
clearLines(linesRendered) termenv.ClearLines(linesRendered)
} }
io.WriteString(os.Stdout, view) io.WriteString(os.Stdout, view)
return strings.Count(view, "\r\n") return strings.Count(view, "\r\n")

View File

@ -3,6 +3,7 @@
package tea package tea
import ( import (
"github.com/muesli/termenv"
"github.com/pkg/term" "github.com/pkg/term"
) )
@ -18,11 +19,11 @@ func initTerminal() error {
} }
tty.SetRaw() tty.SetRaw()
hideCursor() termenv.HideCursor()
return nil return nil
} }
func restoreTerminal() { func restoreTerminal() {
showCursor() termenv.ShowCursor()
tty.Restore() tty.Restore()
} }

View File

@ -2,11 +2,13 @@
package tea package tea
import "github.com/muesli/termenv"
func initTerminal() error { func initTerminal() error {
hideCursor() termenv.HideCursor()
return nil return nil
} }
func restoreTerminal() { func restoreTerminal() {
showCursor() termenv.ShowCursor()
} }