Documentation ¶
Overview ¶
Package console adds useful stuff to stdin/stdout landscape.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // Red can be used as Sprintf, where the output it wrapped in escape characters which will render the text red in terminals. Red = color.New(color.Bold, color.FgRed).SprintfFunc() // Green can be used as Sprintf, where the output it wrapped in escape characters which will render the text green in terminals. Green = color.New(color.Bold, color.FgGreen).SprintfFunc() // Yellow can be used as Sprintf, where the output it wrapped in escape characters which will render the text yellow in terminals. Yellow = color.New(color.Bold, color.FgYellow).SprintfFunc() // Cyan can be used as Sprintf, where the output it wrapped in escape characters which will render the text cyan in terminals. Cyan = color.New(color.FgCyan).SprintfFunc() )
Functions ¶
func IntToCheckmark ¶
IntToCheckmark returns a string with ansi color instruction characters: a red ✘ if the argument is zero, a green ✔ otherwise.
func StringToCheckmark ¶
StringToCheckmark returns a string with ansi color instruction characters: a red ✘ if the argument is "0", a yellow ? if the argument is "", a green ✔ otherwise.
Types ¶
type Checkmark ¶
type Checkmark string
Checkmark type is a string with some functions attached.
Example ¶
Checkmark represents a state of one the three: "✘", "✔", "?". It can be parsed from several primitives.
package main import ( "fmt" "github.com/gotohr/fritzctl/internal/console" ) func main() { fmt.Println(console.Btoc(true)) fmt.Println(console.Btoc(false)) fmt.Println(console.Itoc(1)) fmt.Println(console.Itoc(0)) fmt.Println(console.Stoc("1")) fmt.Println(console.Stoc("0")) fmt.Println(console.Stoc("")) }
Output: ✔ ✘ ✔ ✘ ✔ ✘ ?
func Btoc ¶
Btoc returns a Checkmark from a boolean, red ✘ if the argument is false, a green ✔ otherwise.
type Converter ¶
type Converter interface { // Convert performs the conversion. Convert(s string) (interface{}, error) }
Converter converts strings to any type.
type Question ¶
type Question struct { Key string // Key identifies the target field name. Text string // Text is presented to the user. Converter Converter // Converter should map text input to a target type. CustomSource func() (string, error) // CustomSource may replace the custom input source. Defaulter func() interface{} // Defaulter may supply a fallback value when an empty input is supplied. }
Question is a container for confronting the user to decide on an answer. codebeat:disable[TOO_MANY_IVARS]
func ForPassword ¶
ForPassword creates a Question with a sting as target value. The input from the terminal will not be echoed.
type Survey ¶
type Survey struct { In io.Reader // In is the input source, e.g. os.Stdin. Out io.Writer // Out is the output sink, e.g. os.Stdout. }
Survey contains the configuration on how to present/obtain questions/answers to/from the user.
Example ¶
Survey is a sequential print-read process to obtain data, e.g. from stdin.
package main import ( "bytes" "fmt" "os" "github.com/gotohr/fritzctl/internal/console" ) func main() { r := bytes.NewReader([]byte("example.com\n")) s := console.Survey{In: r, Out: os.Stdout} t := struct{ Host string }{} s.Ask([]console.Question{ console.ForString("Host", "Enter host", "localhost"), }, &t) fmt.Println(t.Host) }
Output: ? Enter host [localhost]: example.com
type Table ¶
type Table struct {
// contains filtered or unexported fields
}
Table contains the data to draw a table.
Example ¶
Table generally wraps borders and separators around structured data.
package main import ( "os" "github.com/gotohr/fritzctl/internal/console" ) func main() { t := console.NewTable(console.Headers("NAME", "AGE", "ADDRESS")) t.Append([]string{"John", "35", "14th Oak Rd"}) t.Append([]string{"Jane", "24", "27th Elm St."}) t.Append([]string{"Tom", "55", "4th Maple Rd."}) t.Print(os.Stdout) }
Output: +------+-----+---------------+ | NAME | AGE | ADDRESS | +------+-----+---------------+ | John | 35 | 14th Oak Rd | | Jane | 24 | 27th Elm St. | | Tom | 55 | 4th Maple Rd. | +------+-----+---------------+