Skip to content

Instantly share code, notes, and snippets.

@alexisvisco
Last active October 26, 2018 13:16
Show Gist options
  • Save alexisvisco/17465158c163dcb1915c9e57bc28ca60 to your computer and use it in GitHub Desktop.
Save alexisvisco/17465158c163dcb1915c9e57bc28ca60 to your computer and use it in GitHub Desktop.
package ainsi
//
//This package is a util library to provide human writeable ainsi codes for terminals.
//Author: Alexis Viscogliosi
//
import "fmt"
// TcUp moves cursor up by n
func TcUp(n int) {
fmt.Printf("\u001b[%dA", n)
}
// TcDown moves cursor down by n
func TcDown(n int) {
fmt.Printf("\u001b[%dB", n)
}
// TcRight moves cursor right by n
func TcRight(n int) {
fmt.Printf("\u001b[%dC", n)
}
// TcLeft moves cursor left by n
func TcLeft(n int) {
fmt.Printf("\u001b[%dD", n)
}
// TcNextLine moves cursor to beginning of line n lines down
func TcNextLine(n int) {
fmt.Printf("\u001b[%dE", n)
}
// TcPrevLine moves cursor to beginning of line n lines up
func TcPrevLine(n int) {
fmt.Printf("\u001b[%dF", n)
}
// TcSetColum moves cursor to column n
func TcSetColum(n int) {
fmt.Printf("\u001b[%dG", n)
}
// TcSetPos moves cursor to row and column
func TcSetPos(row int, column int) {
fmt.Printf("\u001b[%d;%dH", row, column)
}
type ClearOpt int
const (
ClearCursorToEnd = 0 // clears from cursor until end of screen,
ClearCursorToBeginning = 1 // clears from cursor to beginning of screen
ClearEntireScreen = 2 // clears entire screen
)
// TcClear Clear the screen
func TcClear(opt ClearOpt) {
fmt.Printf("\u001b[%dJ", opt)
}
type ClearLineOpt int
const (
ClearCursorToEndLine = 0 // clears from cursor to end of line
ClearCursorToBeginningLine = 1 // clears from cursor to start of line
ClearEntireLine = 2 // clears entire line
)
// TcClear Clear the line
func TcClearLine(opt ClearLineOpt) {
fmt.Printf("\u001b[%dJ", opt)
}
// TcSavePos saves the current cursor position
func TcSavePos() {
fmt.Printf("\u001b[s")
}
// TcRestorePos restores the cursor to the last saved position
func TcRestorePos() {
fmt.Printf("\u001b[u")
}
// TcHideCursor hide cursor
func TcHideCursor() {
fmt.Printf("\u001b[?25l")
}
// TcHideCursor show cursor
func TcShowCursor() {
fmt.Printf("\u001b[?25h")
}
// TcBegin set the cursor to the left
func TcBegin() {
fmt.Printf("\r")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment