Categorygithub.com/k0kubun/go-ansi
modulepackage
0.0.0-20180517002512-3bf9e2903213
Repository: https://github.com/k0kubun/go-ansi.git
Documentation: pkg.go.dev

# README

go-ansi

Windows-portable ANSI escape sequence utility for Go language

What's this?

This library converts ANSI escape sequences to Windows API calls on Windows environment.
You can easily use this feature by replacing fmt with ansi.

Output redirection

Many coloring libraries for Go just use ANSI escape sequences, which don't work on Windows.

If you use go-ansi, you can use these libraries' nice APIs for Windows too. Not only coloring, many ANSI escape sequences are available.

color.Output = ansi.NewAnsiStdout()
color.Cyan("fatih/color")

colorstring.Fprintln(ansi.NewAnsiStdout(), "[green]mitchellh/colorstring")

Cursor

You can control cursor in your terminal. Of course it works on cmd.exe. In a following table, "Shell" shows a unix-like shortcut for the action. (It is not provided by this library and just for the explanation.)

APIEscape CodeShellDescription
ansi.CursorUp(n)CSI n AC-pMove the cursor n cells to up
ansi.CursorDown(n)CSI n BC-nMove the cursor n cells to down
ansi.CursorForward(n)CSI n CC-fMove the cursor n cells to right
ansi.CursorBack(n)CSI n DC-bMove the cursor n cells to left
ansi.CursorNextLine(n)CSI n EC-n C-aMove cursor to beginning of the line n lines down.
ansi.CursorPreviousLine(n)CSI n FC-p C-aMove cursor to beginning of the line n lines up.
ansi.CursorHorizontalAbsolute(x)CSI n GC-a,
C-e
Moves the cursor to column n.

Display

You can easily control your terminal display. You can easily provide unix-like shell functionarities for display, such as C-k or C-l.

APIEscape CodeShellDescription
ansi.EraseInLine(n)CSI n KC-k, C-u,
C-a C-k
0: clear to the end of the line.
1: clear to the beginning of the line.
2: clear entire line.

API document

https://godoc.org/github.com/k0kubun/go-ansi

Notes

This is just a cursor and display supported version of mattn/go-colorable. I used almost the same implementation as it for coloring. Many thanks for @mattn.

License

MIT License

# Functions

Move the cursor n cells to left.
Move the cursor n cells to down.
Move the cursor n cells to right.
Hide the cursor.
Move cursor horizontally to x.
Move cursor to beginning of the line n lines down.
Move cursor to beginning of the line n lines up.
Show the cursor.
Move the cursor n cells to up.
No description provided by the author
Returns special stderr, which converts escape sequences to Windows API calls on Windows environment.
Returns special stdout, which converts escape sequences to Windows API calls on Windows environment.
Print prints given arguments with escape sequence conversion for windows.
Printf prints a given format with escape sequence conversion for windows.
Println prints given arguments with newline and escape sequence conversion for windows.