Skip to content
Myr

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

9 files+34 added−71 removed

modified:examples/fullscreen/main.go+4 added-2 removed
6 unmodified lines
7 "log"8 "time"910 "github.com/charmbracelet/tea"11 "github.com/muesli/termenv"12)1314type model int1516type tickMsg struct{}1718func 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 lines
modified:examples/go.mod+1 added
6 unmodified lines
7require (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 lines
3github.com/charmbracelet/teaparty v0.0.0-20200126032257-419df0b7a206 h1:sjDz9mRGgyWvFH1T2FWKjlUecL6jjLUoKszsYBX+H0E=4github.com/charmbracelet/teaparty v0.0.0-20200126032257-419df0b7a206/go.mod h1:pmTejx7xSMAHuh8aY5r1cwNzNiv0akVFNPeiae+e6RQ=5github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776 h1:VRIbnDWRmAh5yBdz+J6yFMF5vso1It6vn+WmM/5l7MA=6github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776/go.mod h1:9wvnDu3YOfxzWM9Cst40msBF1C2UdQgDv962oTxSuMs=7github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f h1:5CjVwnuUcp5adK4gmY6i72gpVFVnZDP2h5TmPScB6u4=8github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f/go.mod h1:nOFQdrUlIlx6M6ODdSpBj1NVA+VgLC6kmw60mkw34H4=9github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac=10github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=11github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=12github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=13github.com/muesli/termenv v0.4.1-0.20200131124310-936567584c3e h1:6exaizu60WAeTP998SgBox6eaSs6rT36fR/0ebsrnOQ=14github.com/muesli/termenv v0.4.1-0.20200131124310-936567584c3e/go.mod h1:O1/I6sw+6KcrgAmcs6uiUVr7Lui+DNVbHTzt9Lm/PlI=15github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942 h1:A7GG7zcGjl3jqAqGPmcNjd/D9hzL95SuoOQAaFNdLU0=16github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942/go.mod h1:eCbImbZ95eXtAUIbLAuAVnBnwf83mjf6QIVH8SHYwqQ=17golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=18golang.org/x/sys v0.0.0-20200120151820-655fe14d7479 h1:LhLiKguPgZL+Tglay4GhVtfF0kb8cvOJ0dHTCBO8YNI=19golang.org/x/sys v0.0.0-20200120151820-655fe14d7479/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
deleted:ansi.go-64 removed
1package tea23import "fmt"45// Escape sequence6const esc = "\033["78// Fullscreen switches to the altscreen and clears the terminal. The former9// view can be restored with ExitFullscreen().10func Fullscreen() {11 fmt.Print(esc + "?1049h" + esc + "H")12}1314// ExitFullscreen exits the altscreen and returns the former terminal view15func ExitFullscreen() {16 fmt.Print(esc + "?1049l")17}1819// ClearScreen clears the visible portion of the terminal. Effectively, it20// fills the terminal with blank spaces.21func ClearScreen() {22 fmt.Printf(esc + "2J" + esc + "3J" + esc + "1;1H")23}2425// Invert inverts the foreground and background colors of a given string26func Invert(s string) string {27 return esc + "7m" + s + esc + "0m"28}2930// Hide the cursor31func hideCursor() {32 fmt.Printf(esc + "?25l")33}3435// Show the cursor36func showCursor() {37 fmt.Printf(esc + "?25h")38}3940// Move the cursor down a given number of lines and place it at the beginning41// of the line42func cursorNextLine(n int) {43 fmt.Printf(esc+"%dE", n)44}4546// Move the cursor up a given number of lines and place it at the beginning of47// the line48func cursorPrevLine(n int) {49 fmt.Printf(esc+"%dF", n)50}5152// Clear the current line53func clearLine() {54 fmt.Printf(esc + "2K")55}5657// Clear a given number of lines58func 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 line
23go 1.1345require (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
1github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f h1:5CjVwnuUcp5adK4gmY6i72gpVFVnZDP2h5TmPScB6u4=2github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f/go.mod h1:nOFQdrUlIlx6M6ODdSpBj1NVA+VgLC6kmw60mkw34H4=3github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac=4github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=5github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=6github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=7github.com/muesli/termenv v0.4.1-0.20200131124310-936567584c3e h1:6exaizu60WAeTP998SgBox6eaSs6rT36fR/0ebsrnOQ=8github.com/muesli/termenv v0.4.1-0.20200131124310-936567584c3e/go.mod h1:O1/I6sw+6KcrgAmcs6uiUVr7Lui+DNVbHTzt9Lm/PlI=9github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942 h1:A7GG7zcGjl3jqAqGPmcNjd/D9hzL95SuoOQAaFNdLU0=10github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942/go.mod h1:eCbImbZ95eXtAUIbLAuAVnBnwf83mjf6QIVH8SHYwqQ=11golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=12golang.org/x/sys v0.0.0-20200120151820-655fe14d7479 h1:LhLiKguPgZL+Tglay4GhVtfF0kb8cvOJ0dHTCBO8YNI=13golang.org/x/sys v0.0.0-20200120151820-655fe14d7479/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
modified:tea.go+3 added-1 removed
3 unmodified lines
4 "errors"5 "io"6 "os"7 "strings"89 "github.com/muesli/termenv"10)1112// Msg represents an action. It's used by Update to update the UI.13type Msg interface{}
179 unmodified lines
193 // start of a column after a newline194 view = strings.Replace(view, "\n", "\r\n", -1)195196 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 lines
modified:tty_unix.go+3 added-2 removed
1 unmodified line
23package tea45import (6 "github.com/muesli/termenv"7 "github.com/pkg/term"8)910var (
7 unmodified lines
18 return err19 }2021 tty.SetRaw()21 hideCursor()22 termenv.HideCursor()23 return nil24}2526func restoreTerminal() {26 showCursor()27 termenv.ShowCursor()28 tty.Restore()29}
modified:tty_windows.go+4 added-2 removed
1// +build windows23package tea45import "github.com/muesli/termenv"67func initTerminal() error {6 hideCursor()8 termenv.HideCursor()9 return nil10}1112func restoreTerminal() {11 showCursor()13 termenv.ShowCursor()14}