Move ansi helpers to termenv
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Christian Muehlhaeuser · committed by Christian Muehlhaeuser · parent bb06373 · Browse files · 9 files·+34 added−71 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
9 files+34 added−71 removed
- other:+34 added−71 removed
Provenance
- Commit
f382f4d- Tree
ffd5fac- Author
Christian Muehlhaeuser <muesli@gmail.com>- Committer
Christian Muehlhaeuser <muesli@gmail.com>- Parent
bb06373
9 files changed+34 added−71 removed
modified:examples/fullscreen/main.go+4 added-2 removed
6 unmodified lines7 "log"8 "time"9 10 "github.com/charmbracelet/tea"11+ "github.com/muesli/termenv"12 )13 14 type model int15 16 type tickMsg struct{}17 18 func main() {18- tea.Fullscreen()19- defer tea.ExitFullscreen()19+ termenv.AltScreen()20+ termenv.MoveCursor(1, 1)21+ defer termenv.ExitAltScreen()22 err := tea.NewProgram(initialize, update, view, subscriptions).Start()23 if err != nil {24 log.Fatal(err)25 }45 unmodified linesmodified:examples/go.mod+1 added
6 unmodified lines7 require (8 github.com/charmbracelet/tea v0.0.0-20200126032228-29799c3151629 github.com/charmbracelet/teaparty v0.0.0-20200126032257-419df0b7a20610 github.com/fogleman/ease v0.0.0-20170301025033-8da417bf177611+ github.com/muesli/termenv v0.4.1-0.20200131124310-936567584c3e12 )modified:examples/go.sum+9 added
2 unmodified lines3 github.com/charmbracelet/teaparty v0.0.0-20200126032257-419df0b7a206 h1:sjDz9mRGgyWvFH1T2FWKjlUecL6jjLUoKszsYBX+H0E=4 github.com/charmbracelet/teaparty v0.0.0-20200126032257-419df0b7a206/go.mod h1:pmTejx7xSMAHuh8aY5r1cwNzNiv0akVFNPeiae+e6RQ=5 github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776 h1:VRIbnDWRmAh5yBdz+J6yFMF5vso1It6vn+WmM/5l7MA=6 github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776/go.mod h1:9wvnDu3YOfxzWM9Cst40msBF1C2UdQgDv962oTxSuMs=7+github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f h1:5CjVwnuUcp5adK4gmY6i72gpVFVnZDP2h5TmPScB6u4=8+github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f/go.mod h1:nOFQdrUlIlx6M6ODdSpBj1NVA+VgLC6kmw60mkw34H4=9+github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac=10+github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=11+github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=12+github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=13+github.com/muesli/termenv v0.4.1-0.20200131124310-936567584c3e h1:6exaizu60WAeTP998SgBox6eaSs6rT36fR/0ebsrnOQ=14+github.com/muesli/termenv v0.4.1-0.20200131124310-936567584c3e/go.mod h1:O1/I6sw+6KcrgAmcs6uiUVr7Lui+DNVbHTzt9Lm/PlI=15 github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942 h1:A7GG7zcGjl3jqAqGPmcNjd/D9hzL95SuoOQAaFNdLU0=16 github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942/go.mod h1:eCbImbZ95eXtAUIbLAuAVnBnwf83mjf6QIVH8SHYwqQ=17+golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=18 golang.org/x/sys v0.0.0-20200120151820-655fe14d7479 h1:LhLiKguPgZL+Tglay4GhVtfF0kb8cvOJ0dHTCBO8YNI=19 golang.org/x/sys v0.0.0-20200120151820-655fe14d7479/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=deleted:ansi.go-64 removed
1-package tea2-3-import "fmt"4-5-// Escape sequence6-const esc = "\033["7-8-// Fullscreen switches to the altscreen and clears the terminal. The former9-// view can be restored with ExitFullscreen().10-func Fullscreen() {11- fmt.Print(esc + "?1049h" + esc + "H")12-}13-14-// ExitFullscreen exits the altscreen and returns the former terminal view15-func ExitFullscreen() {16- fmt.Print(esc + "?1049l")17-}18-19-// ClearScreen clears the visible portion of the terminal. Effectively, it20-// fills the terminal with blank spaces.21-func ClearScreen() {22- fmt.Printf(esc + "2J" + esc + "3J" + esc + "1;1H")23-}24-25-// Invert inverts the foreground and background colors of a given string26-func Invert(s string) string {27- return esc + "7m" + s + esc + "0m"28-}29-30-// Hide the cursor31-func hideCursor() {32- fmt.Printf(esc + "?25l")33-}34-35-// Show the cursor36-func showCursor() {37- fmt.Printf(esc + "?25h")38-}39-40-// Move the cursor down a given number of lines and place it at the beginning41-// of the line42-func cursorNextLine(n int) {43- fmt.Printf(esc+"%dE", n)44-}45-46-// Move the cursor up a given number of lines and place it at the beginning of47-// the line48-func cursorPrevLine(n int) {49- fmt.Printf(esc+"%dF", n)50-}51-52-// Clear the current line53-func clearLine() {54- fmt.Printf(esc + "2K")55-}56-57-// Clear a given number of lines58-func clearLines(n int) {59- clearLine()60- for i := 0; i < n; i++ {61- cursorPrevLine(1)62- clearLine()63- }64-}modified:go.mod+1 added
1 unmodified line2 3 go 1.134 5 require (6+ github.com/muesli/termenv v0.4.1-0.20200131124310-936567584c3e7 github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e9428 golang.org/x/sys v0.0.0-20200120151820-655fe14d7479 // indirect9 )modified:go.sum+9 added
1+github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f h1:5CjVwnuUcp5adK4gmY6i72gpVFVnZDP2h5TmPScB6u4=2+github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f/go.mod h1:nOFQdrUlIlx6M6ODdSpBj1NVA+VgLC6kmw60mkw34H4=3+github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac=4+github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=5+github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=6+github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=7+github.com/muesli/termenv v0.4.1-0.20200131124310-936567584c3e h1:6exaizu60WAeTP998SgBox6eaSs6rT36fR/0ebsrnOQ=8+github.com/muesli/termenv v0.4.1-0.20200131124310-936567584c3e/go.mod h1:O1/I6sw+6KcrgAmcs6uiUVr7Lui+DNVbHTzt9Lm/PlI=9 github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942 h1:A7GG7zcGjl3jqAqGPmcNjd/D9hzL95SuoOQAaFNdLU0=10 github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942/go.mod h1:eCbImbZ95eXtAUIbLAuAVnBnwf83mjf6QIVH8SHYwqQ=11+golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=12 golang.org/x/sys v0.0.0-20200120151820-655fe14d7479 h1:LhLiKguPgZL+Tglay4GhVtfF0kb8cvOJ0dHTCBO8YNI=13 golang.org/x/sys v0.0.0-20200120151820-655fe14d7479/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=modified:tea.go+3 added-1 removed
3 unmodified lines4 "errors"5 "io"6 "os"7 "strings"8+9+ "github.com/muesli/termenv"10 )11 12 // Msg represents an action. It's used by Update to update the UI.13 type Msg interface{}179 unmodified lines193 // start of a column after a newline194 view = strings.Replace(view, "\n", "\r\n", -1)195 196 if linesRendered > 0 {195- clearLines(linesRendered)197+ termenv.ClearLines(linesRendered)198 }199 io.WriteString(os.Stdout, view)200 return strings.Count(view, "\r\n")201 }70 unmodified linesmodified:tty_unix.go+3 added-2 removed
1 unmodified line2 3 package tea4 5 import (6+ "github.com/muesli/termenv"7 "github.com/pkg/term"8 )9 10 var (7 unmodified lines18 return err19 }20 21 tty.SetRaw()21- hideCursor()22+ termenv.HideCursor()23 return nil24 }25 26 func restoreTerminal() {26- showCursor()27+ termenv.ShowCursor()28 tty.Restore()29 }modified:tty_windows.go+4 added-2 removed
1 // +build windows2 3 package tea4 5+import "github.com/muesli/termenv"6+7 func initTerminal() error {6- hideCursor()8+ termenv.HideCursor()9 return nil10 }11 12 func restoreTerminal() {11- showCursor()13+ termenv.ShowCursor()14 }