From cfb899789e464c6f2e86fae5be6dfb077df22008 Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Sat, 18 Jan 2020 10:33:43 -0500 Subject: [PATCH] Move ANSI stuff into its own file --- ansi.go | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tea.go | 62 ------------------------------------------------------- 2 files changed, 64 insertions(+), 62 deletions(-) create mode 100644 ansi.go diff --git a/ansi.go b/ansi.go new file mode 100644 index 0000000..b682405 --- /dev/null +++ b/ansi.go @@ -0,0 +1,64 @@ +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() + } +} diff --git a/tea.go b/tea.go index 9ca665e..4df9ad7 100644 --- a/tea.go +++ b/tea.go @@ -2,7 +2,6 @@ package tea import ( "errors" - "fmt" "io" "log" "log/syslog" @@ -11,9 +10,6 @@ import ( "github.com/pkg/term" ) -// Escape sequence -const esc = "\033[" - // The number of lines we last rendered var linesRendered = 0 @@ -176,64 +172,6 @@ func (p *Program) render(model Model) { linesRendered = strings.Count(view, "\r\n") } -// 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() - } -} - -// 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" -} - // UseSysLog logs to the system log. This becomes helpful when debugging since // we can't easily print to the terminal since our TUI is occupying it! //