Windows-portable ANSI escape sequence utility for Go language
This library converts ANSI escape sequences to Windows API calls on Windows environment.
You can easily use this feature by replacing fmt
with ansi
.
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")
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.)
API | Escape Code | Shell | Description |
---|---|---|---|
ansi.CursorUp(n) | CSI n A |
C-p | Move the cursor n cells to up |
ansi.CursorDown(n) | CSI n B |
C-n | Move the cursor n cells to down |
ansi.CursorForward(n) | CSI n C |
C-f | Move the cursor n cells to right |
ansi.CursorBack(n) | CSI n D |
C-b | Move the cursor n cells to left |
ansi.CursorNextLine(n) | CSI n E |
C-n C-a | Move cursor to beginning of the line n lines down. |
ansi.CursorPreviousLine(n) | CSI n F |
C-p C-a | Move cursor to beginning of the line n lines up. |
ansi.CursorHorizontalAbsolute(x) | CSI n G |
C-a, C-e |
Moves the cursor to column n. |
You can easily control your terminal display. You can easily provide unix-like shell functionarities for display, such as C-k or C-l.
API | Escape Code | Shell | Description |
---|---|---|---|
ansi.EraseInLine(n) | CSI n K |
C-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. |
https://godoc.org/github.com/k0kubun/go-ansi
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.
MIT License