Documentation
¶
Overview ¶
Package cli contains utility functions for dealing with cli commands within Go.
The main component is the CLI interface that implements cli features through the Terminal struct. Reference: github.com/skeptycal/cli
The ansi color terminal support is provided by the ansi package. Reference: github.com/skeptycal/ansi
Index ¶
- Constants
- Variables
- func Background(str string, color int) string
- func BasicEncode(b interface{}) string
- func Bold(str string) string
- func CheckIfTerminal(w io.Writer) bool
- func Clear()
- func Color(str string, color int) string
- func Columns() int
- func Context(data string, idx, max int) string
- func CurrentHeight() int
- func Flush()
- func GetWinSize() (*unix.Winsize, error)
- func GetXY(x int, y int) (int, int)
- func Height() int
- func Highlight(str, substr string, color int) string
- func HighlightRegion(str string, from, to, color int) string
- func MoveCursor(x int, y int)
- func MoveCursorBackward(bias int)
- func MoveCursorDown(bias int)
- func MoveCursorForward(bias int)
- func MoveCursorUp(bias int)
- func MoveTo(str string, x int, y int) (out string)
- func NewLogger() *logrus.Logger
- func Print(a ...interface{}) (n int, err error)
- func Printf(format string, a ...interface{}) (n int, err error)
- func Println(a ...interface{}) (n int, err error)
- func ResetLine(str string) (out string)
- func Width() int
- func Wrap(s string, width int) string
- type Ansi
- type AnsiColor
- type CLI
- type CLIControls
- type CursorControls
- type Terminal
- func (t *Terminal) Br()
- func (t *Terminal) CLS()
- func (t *Terminal) ClearLine()
- func (t *Terminal) CursorX(x int)
- func (t *Terminal) DevMode(b bool)
- func (t *Terminal) Down(n int)
- func (t *Terminal) Hide()
- func (t *Terminal) Hr()
- func (t *Terminal) Left(n int)
- func (t *Terminal) LineBreak(c string)
- func (t *Terminal) NextLine(n int)
- func (t *Terminal) PreviousLine(n int)
- func (t *Terminal) Print(args ...interface{}) (n int, err error)
- func (t *Terminal) Printf(fmtString string, args ...interface{}) (n int, err error)
- func (t *Terminal) Println(args ...interface{}) (n int, err error)
- func (t *Terminal) Reset() (n int, err error)
- func (t *Terminal) Right(n int)
- func (t *Terminal) SetColor(color Ansi)
- func (t *Terminal) Show()
- func (t *Terminal) String() string
- func (t *Terminal) Up(n int)
- func (t *Terminal) UseColor(b bool)
- func (t *Terminal) Write(p []byte) (n int, err error)
- func (t *Terminal) WriteString(s string) (n int, err error)
Constants ¶
const ( BLACK = iota RED GREEN YELLOW BLUE MAGENTA CYAN WHITE )
List of possible colors
const PCT = 0x8000 << shift
const (
Reset string = ansi.Reset
)
const ResetColor = "\033[32m"
ResetColor - Reset to default color
const ResetLineConst = "\r\033[K"
ResetLineConst - Return cursor to start of line and clean it
Variables ¶
var ( ResetBytes []byte = ansi.ResetBytes InverseBytes string = simpleEncode(ansi.Inverse) )
Screen - Global screen buffer Its not recommended write to buffer dirrectly, use package Print,Printf,Println fucntions instead.
Functions ¶
func BasicEncode ¶
func BasicEncode(b interface{}) string
BasicEncode encodes a basic (3-4 bit) ANSI color code. The code may be passed in as a byte, int, uint, float32, float64, or string and will be converted to the best guess of a byte (uint8) value before encoding.
It is best to use a byte value ...
The format is "\x1b[%dm"
func CheckIfTerminal ¶ added in v0.3.0
func Columns ¶
func Columns() int
Columns returns the number of columns in the terminal, similar to the COLUMNS environment variable on macOS and Linux systems.
func CurrentHeight ¶
func CurrentHeight() int
CurrentHeight gets current height. Line count in Screen buffer.
func GetWinSize ¶ added in v0.3.0
GetWinSize returns device caps for the terminal. The Winsize struct returned includes:
Row, Col, Xpixel, and Ypixel.
func GetXY ¶
GetXY gets relative or absolute coordinates To get relative, set PCT flag to number:
// Get 10% of total width to `x` and 20 to y x, y = tm.GetXY(10|tm.PCT, 20)
func HighlightRegion ¶
func MoveCursorBackward ¶
func MoveCursorBackward(bias int)
MoveCursorBackward - Move cursor backward relative the current position
func MoveCursorDown ¶
func MoveCursorDown(bias int)
MoveCursorDown - Move cursor down relative the current position
func MoveCursorForward ¶
func MoveCursorForward(bias int)
MoveCursorForward - Move cursor forward relative the current position
func MoveCursorUp ¶
func MoveCursorUp(bias int)
MoveCursorUp - Move cursor up relative the current position
func NewLogger ¶
NewLogger creates a new logrus logger that is compatible with the go log package and has terminal stderr defaults set.
Configuration should be set by changing `Formatter`, `Out` and `Hooks` directly on the default logger instance. You can also just instantiate your own directly:
import logrus "github.com/sirupsen/logrus"
var log = &logrus.Logger{
Out: os.Stderr,
Formatter: new(logrus.TextFormatter),
Hooks: make(logrus.LevelHooks),
Level: logrus.DebugLevel,
}
It's recommended to make this a global instance called `log`.
Types ¶
type CLI ¶
type CLI interface {
io.Writer
io.StringWriter
String() string
Print(args ...interface{}) (n int, err error)
Printf(format string, args ...interface{}) (n int, err error)
Println(args ...interface{}) (n int, err error)
SetColor(color ansi.Ansi)
Reset() (n int, err error)
CLIControls
}
CLI implements an ANSI compatible terminal interface.
func New ¶
func New() CLI
New returns a new ANSI compatible terminal interface based on os.Stdout with ANSI support enabled by default.
func NewFromWriter ¶
type CLIControls ¶
type CLIControls interface {
CLS()
ClearLine()
Hr()
Br()
CursorControls
UseColor(b bool)
DevMode(b bool)
}
type CursorControls ¶
type Terminal ¶
type Terminal struct {
// contains filtered or unexported fields
}
func (*Terminal) ClearLine ¶
func (t *Terminal) ClearLine()
ClearLine clears moves the cursor to the start of the the current line and clears the line.
func (*Terminal) DevMode ¶
DevMode manually sets the Dev mode to true (for debugging) or false (for production). Default is false.
func (*Terminal) LineBreak ¶
LineBreak creates a CLI line break (dotted line, underline, etc.) by repeating the string c enough times to span the screen width.
This is useful in delimiting lines of text in terminal output.
func (*Terminal) PreviousLine ¶
PreviousLine moves the cursor to beginning of the line n lines up.
func (*Terminal) Println ¶
Println wraps args in ANSI 8-bit color codes (256 color codes) and adds a newline character
func (*Terminal) SetColor ¶
SetColor sets the ANSI foreground, background, and effect codes for upcoming output.
func (*Terminal) String ¶
String describes the terminal. If devMode is true, it generates a list of dev info.
func (*Terminal) Write ¶
Write writes len(p) bytes from p to the Terminal data stream. The bytes are wrapped in ansi escape codes using the current Terminal colorBytes field and the Reset constant. This ensures that the terminal is not left in an unknown state if other programs write to it concurrently.
It returns the number of bytes written from p (0 <= n <= len(p)). The bytes sent to set and reset ANSI escape codes are not included in the returned value. This maintains compatibility with the io.Writer interface.
Any error encountered that caused the write to stop early is also returned. Write returns io.ErrShortWrite if n < len(p) and no other explicit error was identified.
As specified by io.Writer, Write does not modify the slice data, even temporarily and does not retain p.
func (*Terminal) WriteString ¶
WriteString writes the contents of s to the underlying data stream.
It uses the io.Writer interface and follows standard conventions:
It returns the number of bytes written from s (0 <= n <= len(s)) and any error encountered that caused the write to stop early. WriteString must return a non-nil error if it returns n < len(s). Write must not modify the string data, even temporarily.
Implementations must not retain s.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
examples/cli
command
|
|
|
examples/colortest
command
|
|
|
examples/wraplines
command
|
|
|
Package terminal provides information about the state of the terminal.
|
Package terminal provides information about the state of the terminal. |