console

package
v0.0.0-...-798cd6d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 13, 2023 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package console adds useful stuff to stdin/stdout landscape.

Index

Examples

Constants

This section is empty.

Variables

View Source
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

func IntToCheckmark(i int) string

IntToCheckmark returns a string with ansi color instruction characters: a red ✘ if the argument is zero, a green ✔ otherwise.

func StringToCheckmark

func StringToCheckmark(s string) string

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

func Btoc(b bool) Checkmark

Btoc returns a Checkmark from a boolean, red ✘ if the argument is false, a green ✔ otherwise.

func Itoc

func Itoc(i int) Checkmark

Itoc returns a Checkmark from an int.

func Stoc

func Stoc(s string) Checkmark

Stoc returns a Checkmark from a string.

func (Checkmark) Inverse

func (c Checkmark) Inverse() Checkmark

Inverse returns the opposite of the given Checkmark: a red ✘ if the argument is a green ✔ and vice versa, a yellow ? otherwise.

func (Checkmark) String

func (c Checkmark) String() string

type Converter

type Converter interface {
	// Convert performs the conversion.
	Convert(s string) (interface{}, error)
}

Converter converts strings to any type.

type Option

type Option func(t *Table)

Option allows to mutate the table.

func Headers

func Headers(hs ...string) Option

Headers is an option to store the header texts in the table.

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 ForBool

func ForBool(key, text string, def bool) Question

ForBool creates a Question with a bool as target value.

func ForPassword

func ForPassword(key, text string) Question

ForPassword creates a Question with a sting as target value. The input from the terminal will not be echoed.

func ForString

func ForString(key, text, def string) Question

ForString creates a Question with a string as target value.

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

func (*Survey) Ask

func (s *Survey) Ask(qs []Question, v interface{}) error

Ask confronts the user with the passed questions. Questions are traversed in order.

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. |
+------+-----+---------------+

func NewTable

func NewTable(opts ...Option) *Table

NewTable creates a new table and applies the given Options to it.

func (*Table) Append

func (t *Table) Append(row []string)

Append extends the table body by one line. The length of the row should match the length of the headers as well as the other rows.

func (*Table) Print

func (t *Table) Print(w io.Writer)

Print writes the text representation of the table to a given io.Writer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL