Skip to content
Myr

Move ANSI stuff into its own file

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Christian Rocha · committed by Christian Rocha · parent d976902 · Browse files · 2 files+64 added−62 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+64 added−62 removed

  • other:+64 added−62 removed

2 files+64 added−62 removed

added:ansi.go+64 added
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:tea.go-62 removed
1package tea23import (4 "errors"5 "fmt"5 "io"6 "log"7 "log/syslog"8 "strings"910 "github.com/pkg/term"11)1214// Escape sequence15const esc = "\033["1613// The number of lines we last rendered14var linesRendered = 01516// Msg represents an action. It's used by Update to update the UI.
154 unmodified lines
171 io.WriteString(p.rw, view)172 linesRendered = strings.Count(view, "\r\n")173}174179// Hide the cursor180func hideCursor() {181 fmt.Printf(esc + "?25l")182}183184// Show the cursor185func showCursor() {186 fmt.Printf(esc + "?25h")187}188189// Move the cursor down a given number of lines and place it at the beginning190// of the line191func cursorNextLine(n int) {192 fmt.Printf(esc+"%dE", n)193}194195// Move the cursor up a given number of lines and place it at the beginning of196// the line197func cursorPrevLine(n int) {198 fmt.Printf(esc+"%dF", n)199}200201// Clear the current line202func clearLine() {203 fmt.Printf(esc + "2K")204}205206// Clear a given number of lines207func clearLines(n int) {208 clearLine()209 for i := 0; i < n; i++ {210 cursorPrevLine(1)211 clearLine()212 }213}214215// Fullscreen switches to the altscreen and clears the terminal. The former216// view can be restored with ExitFullscreen().217func Fullscreen() {218 fmt.Print(esc + "?1049h" + esc + "H")219}220221// ExitFullscreen exits the altscreen and returns the former terminal view222func ExitFullscreen() {223 fmt.Print(esc + "?1049l")224}225226// ClearScreen clears the visible portion of the terminal. Effectively, it227// fills the terminal with blank spaces.228func ClearScreen() {229 fmt.Printf(esc + "2J" + esc + "3J" + esc + "1;1H")230}231232// Invert inverts the foreground and background colors of a given string233func Invert(s string) string {234 return esc + "7m" + s + esc + "0m"235}236175// UseSysLog logs to the system log. This becomes helpful when debugging since176// we can't easily print to the terminal since our TUI is occupying it!177//178// On macOS this is a just a matter of: tail -f /var/log/system.log
8 unmodified lines