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
Provenance
- Commit
cfb8997- Tree
9afc072- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
d976902
2 files changed+64 added−62 removed
added:ansi.go+64 added
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:tea.go-62 removed
1 package tea2 3 import (4 "errors"5- "fmt"5 "io"6 "log"7 "log/syslog"8 "strings"9 10 "github.com/pkg/term"11 )12 14-// Escape sequence15-const esc = "\033["16-13 // The number of lines we last rendered14 var linesRendered = 015 16 // Msg represents an action. It's used by Update to update the UI.154 unmodified lines171 io.WriteString(p.rw, view)172 linesRendered = strings.Count(view, "\r\n")173 }174 179-// Hide the cursor180-func hideCursor() {181- fmt.Printf(esc + "?25l")182-}183-184-// Show the cursor185-func showCursor() {186- fmt.Printf(esc + "?25h")187-}188-189-// Move the cursor down a given number of lines and place it at the beginning190-// of the line191-func cursorNextLine(n int) {192- fmt.Printf(esc+"%dE", n)193-}194-195-// Move the cursor up a given number of lines and place it at the beginning of196-// the line197-func cursorPrevLine(n int) {198- fmt.Printf(esc+"%dF", n)199-}200-201-// Clear the current line202-func clearLine() {203- fmt.Printf(esc + "2K")204-}205-206-// Clear a given number of lines207-func clearLines(n int) {208- clearLine()209- for i := 0; i < n; i++ {210- cursorPrevLine(1)211- clearLine()212- }213-}214-215-// Fullscreen switches to the altscreen and clears the terminal. The former216-// view can be restored with ExitFullscreen().217-func Fullscreen() {218- fmt.Print(esc + "?1049h" + esc + "H")219-}220-221-// ExitFullscreen exits the altscreen and returns the former terminal view222-func ExitFullscreen() {223- fmt.Print(esc + "?1049l")224-}225-226-// ClearScreen clears the visible portion of the terminal. Effectively, it227-// fills the terminal with blank spaces.228-func ClearScreen() {229- fmt.Printf(esc + "2J" + esc + "3J" + esc + "1;1H")230-}231-232-// Invert inverts the foreground and background colors of a given string233-func Invert(s string) string {234- return esc + "7m" + s + esc + "0m"235-}236-175 // 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.log8 unmodified lines