Documentation
¶
Overview ¶
Package cli contains utility functions for dealing with cli commands within CLI applications written in Go.
The main component is the CLI interface that implements cli features through the Terminal struct. Reference: github.com/skeptycal/cli/terminal
The ansi color terminal support is provided by the ansi package. Reference: github.com/skeptycal/ansi
Index ¶
- Constants
- Variables
- func Atoi(s string) int
- func Background(str string, color int) string
- func Bold(str string) string
- func Box(args ...any) (n int, err error)
- func Br()
- func CheckIfTerminal(w io.Writer) bool
- func Clear()
- func Color(str string, color int) string
- func Cols() int
- func Context(data string, idx, max int) string
- func Cr(c string)
- func CurrentHeight() int
- func DbEcho(args ...any) (n int, err error)
- func DbEchoNoLn(args ...any) (n int, err error)
- func DbEchof(format string, args ...any) (n int, err error)
- func FileWriteCloser(filename string, truncate bool) (io.WriteCloser, error)
- func Flag(flag *flag.Flag) any
- func Flush()
- func FlushNoOverflow() error
- func Fr()
- func GetFileList(path string, pattern, exclude []string, recurse bool) ([]string, error)
- func GetXY(x int, y int) (int, int)
- func Getenv(envVarName string, defaultValue string) (retval string)
- func Head[E any](s []E) []E
- func HeadN[E any](s []E, n int) []E
- func Height() int
- func Highlight(str, substr string, color int) string
- func HighlightRegion(str string, from, to, color int) string
- func Hr()
- 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 NewWriteCloserCLI(w any) io.WriteCloser
- 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 ReadFileCli(filename string) string
- func ResetLine(str string) (out string)
- func Rows() int
- func StatCli(filename string) os.FileInfo
- func Tail[E any](s []E) []E
- func TailN[E any](s []E, n int) []E
- func Vprint(args ...any) (int, error)
- func Vprintf(format string, args ...any) (int, error)
- func Vprintln(args ...any) (int, error)
- func Width() int
- func WriteFile(filename, s string) (n int, err error)
- func WriteString(w io.Writer, s string) (n int, err error)
- func XPixels() int
- func YPixels() int
- type CLI
- type Getter
- type Printer
- type Value
Constants ¶
const ( NewLine string = "\n" // Newline character Tab string = "\t" // Tab character Space = " " // Space character DefaultScreenWidthString = "80" DefaultHeadByteLength = 79 DefaultTailByteLength = 20 DefaultHeadLineLength = 5 DefaultTailLineLength = 5 )
const ( NormalMode os.FileMode = 0644 DirMode os.FileMode = 0755 )
const PCT = 0x8000 << shift
Variables ¶
var ( // DEBUG flag to enable debug logging and features DEBUG = true // Terminal flag to enable CLI terminal display IsTerminal = terminal.IsTerminal(int(os.Stdout.Fd())) )
var ( // user home directory HOME = envvars.HOME // current working directory PWD = envvars.PWD // Initial column width of CLI terminal display COLUMNS = 80 // Initial row height of CLI terminal display ROWS = 24 )
environment variables
var ( ErrNotImplemented = errors.New("not implemented") ErrNoForceFlag = errors.New("no -force flag") )
var ( DefaultDebugFlag = false DefaultInFile = "./gilist.txt" DefaultOutFile = "../gitignore_gen.go" DefaultConfigFile = ".gosimple.conf" )
//////// Flags defaults
var ( DebugFlag = Flags.Bool("debug", false, "turn on debug mode") ForceFlag = Flags.Bool("force", false, "force writing to file") VerboseFlag = Flags.Bool("verbose", false, "turn on verbose mode") QuietFlag = Flags.Bool("quiet", false, "turn on quiet mode") InFile = Flags.String("in", DefaultInFile, "name of input file") OutFile = Flags.String("out", DefaultOutFile, "name of output file") ConfigFile = Flags.String("config", DefaultConfigFile, "name of config file") LogLevel = &logLevelFlag{defaultLogLevel} )
//////// cli Flags
var Col = t.Col
var (
DbColor string = "\033[1;31m" // ANSI dbecho code
)
var Flags = flag.NewFlagSet("cli", flag.ExitOnError)
Flags is a flag.FlagSet that can be inherited or modified.
var ( // Global errorlogger instance Log = errorlogger.New() )
errorlogger
Screen - Global screen buffer Its not recommended write to buffer directly, use package Print,Printf,Println fucntions instead.
Functions ¶
func CheckIfTerminal ¶
func Cols ¶
func Cols() 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 DbEcho ¶
DbEcho formats using the default formats for its operands and writes to debugWriter. Spaces are always added between operands and a newline is appended. This is equivalent to fmt.Println behavior, not fmt.Print behavior.
It returns the number of bytes written and any write error encountered.
DbEcho sends output based on DebugFlag setting and Log level >= 2. If the first argument is a string and contains at least one % symbol, it is used as a format string for a DbEchof() version of this function.
func DbEchoNoLn ¶
DbEchoNoLn formats using the default formats for its operands and writes to debugWriter. Spaces are added between operands when neither is a string. It returns the number of bytes written and any write error encountered.
DbEchoNoLn sends output based on DebugFlag setting and Log level >= 2.
Behavior is opposite of Print vs. Println in that the default DbEcho behavior is to add a Newline and the default DbEchoNoLn behavior is to leave all arguments unchanged. This function writes only the arguments; no formatting or updates are done; no newlines are added.
func DbEchof ¶
DbEchof formats according to a format specifier and writes to debugWriter. It returns the number of bytes written and any write error encountered.
DbEcho sends output based on DebugFlag setting and Log level >= 2.
As a convenience, if the final character in the format string is not a Newline, then one is added.
func FileWriteCloser ¶
func FileWriteCloser(filename string, truncate bool) (io.WriteCloser, error)
FileWriteCloser returns an io.FileWriteCloser from the given filename. The file is truncated upon opening if truncate is true. Otherwise, the file is opened in append mode. If the file does not exist, a new file is created.
As a precaution, the writer uses os.Stdout unless the -force (ForceFlag) CLI option is enabled.
func FlushNoOverflow ¶
func FlushNoOverflow() error
Flush buffer and ensure that it will not overflow screen
func Fr ¶
func Fr()
Fr prints a screen-wide footer underline using the footerChar string contained in the package options. The screen width is updated by the Columns() function.
func GetFileList ¶
GetFileList returns a list of files in the specified path that match any of the specified patterns while excluding those that match the specified exclude list.
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 Getenv ¶
Getenv returns the value of the string while replaces ${var} or $var in the string according to the values of the current environment variables. References to undefined variables are replaced by defaultValue.
d := Getenv("${HOME}/.config") fmt.Println(d) // /Users/skeptycal/.config
func Head ¶
func Head[E any](s []E) []E
Head returns the first n elements of a sequence. If n is longer than s, s is returned unchanged. The default value of n is used. If another value of n is needed, use HeadN(s S, n int).
func HeadN ¶
Head returns the first n elements of a sequence s. If n is longer than s, s is returned unchanged.
func HighlightRegion ¶
func Hr ¶
func Hr()
Hr prints a screen-wide header underline using the headerChar string contained in the package options. The screen width is updated by the Columns() function.
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 NewWriteCloserCLI ¶
func NewWriteCloserCLI(w any) io.WriteCloser
func ReadFileCli ¶
GetDataCli gets the contents of filename and returns the string version. In the Cli version, any error results in log.Fatal().
func StatCli ¶
StatCli returns the os.FileInfo from filename. In the Cli version, any error results in log.Fatal().
func Tail ¶
func Tail[E any](s []E) []E
Tail returns the last n elements of a sequence. If n is longer than s, s is returned unchanged. The default value of n is used. If another value of n is needed, use TailN(s S, n int).
func TailN ¶
Tail returns the last n elements of a sequence s. If n is longer than s, s is returned unchanged.
func Vprint ¶
Vprint sends output based on VerboseFlag setting and Log level >= 4. If the first argument is a string and contains at least one % symbol, it is used as a format string for a Printf version of this function.
func Vprintf ¶
Vprintf sends output based on VerboseFlag setting and Log level >= 4. The first argument is a format string for a Printf version of the Vprint function.
func Vprintln ¶
Vprintln sends output based on VerboseFlag setting and Log level >= 4. A trailing newline is appended to the output.
If the first argument is a string and contains at least one % symbol, it is used as a format string for a Printf version of this function.
func WriteFile ¶
WriteFile writes the string to filename. As a precaution, the writer uses os.Stdout unless the -force (ForceFlag) CLI option is enabled.
func WriteString ¶
WriteString writes the string to w. It implements the io.StringWriter interface.
As a precaution, the writer uses os.Stdout unless the -force (ForceFlag) CLI option is enabled.
Types ¶
type CLI ¶
type CLI interface { io.Writer io.StringWriter fmt.Stringer Printer terminal.CLIControls SetColor(color ansi.AnsiColor) Reset() (n int, err error) }
CLI implements an ANSI compatible terminal interface.
type Printer ¶
type Printer interface { Print(args ...interface{}) (n int, err error) Printf(format string, args ...interface{}) (n int, err error) Println(args ...interface{}) (n int, err error) }
Printer implements the common printer interface elements Print, Printf, and Println
type Value ¶
Value is the interface to the dynamic value stored in a flag. (The default value is represented as a string.)
If a Value has an IsBoolFlag() bool method returning true, the command-line parser makes -name equivalent to -name=true rather than using the next command-line argument.
Set is called once, in command line order, for each flag present. The flag package may call the String method with a zero-valued receiver, such as a nil pointer.
Reference: standard library flag package
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
Package ansi provides an extensive set of ANSI escape codes for use in command line interfaces (CLIs) using industry standard well-documented terminal commands.
|
Package ansi provides an extensive set of ANSI escape codes for use in command line interfaces (CLIs) using industry standard well-documented terminal commands. |
cmd
|
|
Package errorlogger implements error logging to a variety of output formats.
|
Package errorlogger implements error logging to a variety of output formats. |
Package terminal provides information about the state of the terminal.
|
Package terminal provides information about the state of the terminal. |