Documentation
¶
Overview ¶
Vox is a Go package designed to help make terminal/console applications more attractive.
It is a collection of small helper functions that aid in printing various pieces of information to the console.
- Various predefined and common printing tasks like printing property key/value pairs, result responses, etc.
- Print JSON data with syntax highlighting
- Easily print colorized output
- Display real time progress bars for tasks
- Easy helper functions for printing various types of messages: Alerts, errors, debug messages, etc.
- Control the output and input streams to help during application testing.
Printing to the screen ¶
There are a number of output functions to print data to the screen with out without coloring. Most of the output functions accept an a series of string parts that are combined together. Color constants can be interlaced between these parts to color the output.
vox.Println(vox.Red, "Some read text", vox.ResetColor)
There are also a number of "LogLevel" type functions that easily color the output.
vox.Alert("A warning") vox.Errorf("An error occured: %s", err.Error()) vox.Debug(""A debug message")
Prompting for input ¶
There are several helper functions for gathering input from the console.
strResponse := vox.Prompt("a message", "a default value") boolResponse := vox.PromptBool("a message", true) choices := []string{"option 1", "option 2"} choiceIndex := vox.PromptChoice("A message", choices, 1)
Pipelines ¶
Vox offers pipelines as a way of configuring one or more output streams. Four built in pipelines are provided with the package:
- ConsolePipeline - This is the default Pipeline set for any vox instance. This pipeline will present colored output to standard out.
- FilePipeline - This pipeline will redirect all data to a local file. This pipeline uses plain output, without color codes.
- TestPipeline - All output will be internally stored in a string slice and utility functions are provided to make accessing values easier. This pipeline should be used for unit tests.
- WriterPipeline - This is a generic pipeline that allows you to specifiy any writer that implements the io.Writer interface.
Testing ¶
A testing pipeline is provided that directs all output into an internal string slice. It also provides utility functions to make accessing values easier.
v := vox.New() pipline = vox.TestPipeline{} v.SetPipelines(pipeline) v.Print("test") if (pipeline.Last() != "test" { t.Error("not test") }
A Test helper function is provided to make this easier:
pipeline := vox.Test()
You can use the `SendInput` function. SendInput must be called before any prompt function, so that the data is ready in the buffer when `Prompt` is called.
func AskForFile() string { return vox.Prompt("Enter a file", "") } func TestReadConfig(t *testing.T) { // setup for testing pipeline = vox.Test() // verify an error output err := checkFile() if pipeline.Last() != fmt.Sprint(vox.Red, "No config file found.") { t.Errorf("Error message not printed: %s", pipeline.Last()) } // Asks user for a file path SendInput("test.txt") name := AskForFile() // Builds a config file SetupFile(name) err = checkFile() if err != nil { t.Errorf("Could not load config file: %s", err.Error()) } if pipeline.Last() != "Config file read" { t.Error("Value missmatch: %s", pipeline.Last()) } }
Index ¶
- Variables
- func AddPipeline(p Pipeline)
- func Alert(args ...interface{})
- func Alertf(format string, args ...interface{})
- func ClearInput()
- func Debug(args ...interface{})
- func Debugf(format string, args ...interface{})
- func Error(args ...interface{})
- func Errorf(format string, args ...interface{})
- func Fatal(args ...interface{})
- func Fatalf(format string, args ...interface{})
- func IncProgress()
- func Info(args ...interface{})
- func Infof(format string, args ...interface{})
- func Print(s ...interface{})
- func PrintJSON(contentBytes []byte)
- func PrintPlain(c Color, s ...interface{})
- func PrintProperty(name, value string)
- func PrintResult(desc string, err error)
- func PrintRich(c Color, s ...interface{})
- func Printc(c Color, s ...interface{})
- func Printf(format string, s ...interface{})
- func Println(s ...interface{})
- func Printlnc(c Color, s ...interface{})
- func Prompt(name, defaultVal string) string
- func PromptBool(message string, defaultVal bool) bool
- func PromptChoice(msg string, choices []string, defaultIdx int) string
- func SendInput(str string) error
- func SetInput(in *os.File)
- func SetPipelines(p Pipeline)
- func SetProgress(current int)
- func Sprintc(c Color, args ...interface{}) string
- func StartProgress(current, max int)
- func StopProgress()
- type Color
- type ConsolePipeline
- type FilePipeline
- type Pipeline
- type PipelineConfig
- type TestPipeline
- type Vox
- func (v *Vox) AddPipeline(p Pipeline)
- func (v *Vox) Alert(args ...interface{})
- func (v *Vox) Alertf(format string, args ...interface{})
- func (v *Vox) Debug(args ...interface{})
- func (v *Vox) Debugf(format string, args ...interface{})
- func (v *Vox) Error(args ...interface{})
- func (v *Vox) Errorf(format string, args ...interface{})
- func (v *Vox) Fatal(args ...interface{})
- func (v *Vox) Fatalf(format string, args ...interface{})
- func (v *Vox) IncProgress()
- func (v *Vox) Info(args ...interface{})
- func (v *Vox) Infof(format string, args ...interface{})
- func (v *Vox) Print(s ...interface{})
- func (v *Vox) PrintJSON(contentBytes []byte)
- func (v *Vox) PrintPlain(s ...interface{})
- func (v *Vox) PrintProperty(name, value string)
- func (v *Vox) PrintResult(desc string, err error)
- func (v *Vox) PrintRich(s ...interface{})
- func (v *Vox) Printc(c Color, s ...interface{})
- func (v *Vox) Printf(format string, s ...interface{})
- func (v *Vox) Println(s ...interface{})
- func (v *Vox) Printlnc(c Color, s ...interface{})
- func (v *Vox) Prompt(name, defaultValue string) string
- func (v *Vox) PromptBool(message string, defaultVal bool) bool
- func (v *Vox) PromptChoice(msg string, choices []string, defIdx int) string
- func (v *Vox) SendInput(str string) error
- func (v *Vox) SetInput(in *os.File)
- func (v *Vox) SetPipelines(p Pipeline)
- func (v *Vox) SetProgress(current int)
- func (v *Vox) StartProgress(current, max int)
- func (v *Vox) StopProgress()
- func (v *Vox) Test() *TestPipeline
- func (v *Vox) Write(p []byte) (n int, err error)
- type WriterPipeline
Constants ¶
This section is empty.
Variables ¶
var ( // Back - Black terminal color constant. This can be used as a string inside any of the output functions Black = Color{0} // Red - Red terminal color constant. This can be used as a string inside any of the output functions Red = Color{1} // Green - Green terminal color constant. This can be used as a string inside any of the output functions Green = Color{2} // Yellow - Yellow terminal color constant. This can be used as a string inside any of the output functions Yellow = Color{3} // Blue - Blue terminal color constant. This can be used as a string inside any of the output functions Blue = Color{4} // Magenta - Magenta terminal color constant. This can be used as a string inside any of the output functions Magenta = Color{5} // Cyan - Cyan terminal color constant. This can be used as a string inside any of the output functions Cyan = Color{6} // White - White terminal color constant. This can be used as a string inside any of the output functions White = Color{7} // ResetColor - Resets the terminal back to its default color. This can be used as a string inside any of the output functions ResetColor = Color{9} )
Functions ¶
func Alert ¶
func Alert(args ...interface{})
Alert - Print an info output. Console output is colored yellow.
func Alertf ¶
func Alertf(format string, args ...interface{})
Alertf - Print an info output. Console output is colored yellow.
func Debug ¶
func Debug(args ...interface{})
Debug - Print an debug output. Debug output is not colored.
func Debugf ¶
func Debugf(format string, args ...interface{})
Debugf - Print an debug output. Debug output is not colored.
func Error ¶
func Error(args ...interface{})
Error - Print output as an error. Console output is colored red.
func Errorf ¶
func Errorf(format string, args ...interface{})
Errorf - Print error output. Console output is colored red.
func Fatal ¶
func Fatal(args ...interface{})
Fatal - Prints an error message and then exits the application.
func Fatalf ¶
func Fatalf(format string, args ...interface{})
Fatalf - Prints an error message and then exits the application.
func IncProgress ¶
func IncProgress()
IncProgress - Increment the current progress value by name. If the new Current value is equal to the Max value StopProgress will be called automatically.
func Info ¶
func Info(args ...interface{})
Info - Print an info output. Console output is colored white.
func Infof ¶
func Infof(format string, args ...interface{})
Infof - Print an info output. Console output is colored white.
func PrintJSON ¶
func PrintJSON(contentBytes []byte)
PrintJSON - Prints a byte array contianing JSON content. This output will be color coded and syntax highlighted. It is also reformatted with indentation.
func PrintPlain ¶
func PrintPlain(c Color, s ...interface{})
PrintPlain will only print to plain pipelines, such as the FilePipeline
func PrintProperty ¶
func PrintProperty(name, value string)
PrintProperty - Prints a property name and value. The value will be right aligned.
func PrintResult ¶
PrintResult - Prints a name and a result message. If an error is passed it will result in a failure message ex. If nil is passed as the second argument it will result in a success. The status code will also be right aligned and color coded based on the result.
func PrintRich ¶
func PrintRich(c Color, s ...interface{})
PrintRich will only print to non plain pipelines. It is safe to embed color codes in this function.
func Printc ¶
func Printc(c Color, s ...interface{})
Printc - sends the string results of the objects prefixed with a color code. For plain pipelines the color code will be stripped out.
func Printf ¶
func Printf(format string, s ...interface{})
Printf - Prints a formatted string using a template and as series of variables.
func Println ¶
func Println(s ...interface{})
Println - Prints a number of tokens ending with a new line.
func Printlnc ¶
func Printlnc(c Color, s ...interface{})
Printlnc - Prints a number of tokens followed by a new line. This output is also wrapped in a color code and a reset.
func Prompt ¶
Prompt - Gets input from the input stream. By default Stdin. If an empty string is sent the default value will be returned.
func PromptBool ¶
PromptBool - Prompts the user for a boolean response.
func PromptChoice ¶
PromptChoice - Prompts for a choice of a series of options from the user.
func SetPipelines ¶
func SetPipelines(p Pipeline)
SetPipelines replaces all pipelines with the passed pipeline
func Sprintc ¶
Sprintc - Creates a string in a given color. The color code prepends the string and a reset code is appended to it.
func StartProgress ¶
func StartProgress(current, max int)
StartProgress - Start outputing a progressbar.
func StopProgress ¶
func StopProgress()
StopProgress - Stops outputing a progress bar and closes associated writers. This is called automatically if the Current value equals, or exceeds, the maximum value.
Types ¶
type Color ¶
type Color struct {
// contains filtered or unexported fields
}
Color - A structure which represents a single color. This structure should not need to be used directly. Variables for each color are exported in the package.
type ConsolePipeline ¶
type ConsolePipeline struct{}
ConsolePipeline a log pipeline that outputs directly to STDERR
func (*ConsolePipeline) Config ¶
func (c *ConsolePipeline) Config() *PipelineConfig
Config returns the pipeline configuration
func (*ConsolePipeline) Initialize ¶
func (c *ConsolePipeline) Initialize() error
Initialize has no logic for a ConsolePipeline
type FilePipeline ¶
type FilePipeline struct { Filepath string // contains filtered or unexported fields }
FilePipeline sends output into a local file
func (*FilePipeline) Config ¶
func (f *FilePipeline) Config() *PipelineConfig
Config returns the pipline configuration
func (*FilePipeline) Initialize ¶
func (f *FilePipeline) Initialize() error
Initialize opens the local file for reading
type Pipeline ¶
type Pipeline interface { Config() *PipelineConfig Write([]byte) (int, error) Initialize() error }
Pipeline represents a specific log pipeline
type PipelineConfig ¶
type PipelineConfig struct { // Plain - If set to true all color information and some formatting will be // stripped from the output. This is normally used for outputs directed towards // files. Plain bool }
PipelineConfig is the configuration for a pipeline The pipeline config should be returned from the Config function for any pipeline structure.
type TestPipeline ¶
TestPipeline a pipeline that can be used in tests
func Test ¶
func Test() *TestPipeline
Test - Sets up vox to print and read from in memory locations for testing.
func (*TestPipeline) Clear ¶
func (t *TestPipeline) Clear()
Clear removes all items in the pipelines buffer
func (*TestPipeline) Config ¶
func (t *TestPipeline) Config() *PipelineConfig
Config returns the pipline configuration
func (*TestPipeline) Initialize ¶
func (t *TestPipeline) Initialize() error
Initialize sets up the testing pipeline
func (*TestPipeline) Last ¶
func (t *TestPipeline) Last() string
Last returns the last section of data sent
type Vox ¶
type Vox struct {
// contains filtered or unexported fields
}
Vox - The main class for Vox all functions are called from this object. Direct functions use an auto generated Vox object.
func New ¶
func New() *Vox
New - creates a new Vox instance. This can be used as an alternative to the singletone instance. If multiple Vox instances are needed.
func (*Vox) AddPipeline ¶
AddPipeline adds a new pipeline to the logger
func (*Vox) Alert ¶
func (v *Vox) Alert(args ...interface{})
Alert - Print an info output. Console output is colored yellow.
func (*Vox) Debug ¶
func (v *Vox) Debug(args ...interface{})
Debug - Print an debug output. Debug output is not colored.
func (*Vox) Error ¶
func (v *Vox) Error(args ...interface{})
Error - Print output as an error. Console output is colored red.
func (*Vox) Fatal ¶
func (v *Vox) Fatal(args ...interface{})
Fatal - Prints an error message and then exits the application.
func (*Vox) IncProgress ¶
func (v *Vox) IncProgress()
IncProgress - Increment the current progress value by name. If the new Current value is equal to the Max value StopProgress will be called automatically.
func (*Vox) Info ¶
func (v *Vox) Info(args ...interface{})
Info - Print an info output. Console output is colored white.
func (*Vox) PrintJSON ¶
PrintJSON - Prints a byte array contianing JSON content. This output will be color coded and syntax highlighted. It is also reformatted with indentation.
func (*Vox) PrintPlain ¶
func (v *Vox) PrintPlain(s ...interface{})
PrintPlain will only print to non plain pipelines. It is safe to embed color codes in this function.
func (*Vox) PrintProperty ¶
PrintProperty - Prints a property name and value. The value will be right aligned.
func (*Vox) PrintResult ¶
PrintResult - Prints a name and a result message. If an error is passed it will result in a failure message ex. If nil is passed as the second argument it will result in a success. The status code will also be right aligned and color coded based on the result.
func (*Vox) PrintRich ¶
func (v *Vox) PrintRich(s ...interface{})
PrintRich will only print to non plain pipelines. It is safe to embed color codes in this function.
func (*Vox) Printc ¶
Printc - sends the string results of the objects prefixed with a color code. For plain pipelines the color code will be stripped out.
func (*Vox) Printf ¶
Printf - Prints a formatted string using a template and as series of variables.
func (*Vox) Println ¶
func (v *Vox) Println(s ...interface{})
Println - Prints a number of tokens ending with a new line.
func (*Vox) Printlnc ¶
Printlnc - Prints a number of tokens followed by a new line. This output is also wrapped in a color code and a reset.
func (*Vox) Prompt ¶
Prompt - Gets input from the input stream. By default Stdin. If an empty string is sent the default value will be returned.
func (*Vox) PromptBool ¶
PromptBool - Prompts the user for a boolean response.
func (*Vox) PromptChoice ¶
PromptChoice - Prompts for a choice of a series of options from the user.
func (*Vox) SetPipelines ¶
SetPipelines replaces all pipelines with the passed pipeline
func (*Vox) SetProgress ¶
SetProgress - Sets the current progress value.
func (*Vox) StartProgress ¶
StartProgress - Start outputing a progressbar.
func (*Vox) StopProgress ¶
func (v *Vox) StopProgress()
StopProgress - Stops outputing a progress bar and closes associated writers. This is called automatically if the Current value equals, or exceeds, the maximum value.
func (*Vox) Test ¶
func (v *Vox) Test() *TestPipeline
Test - Sets up vox to print and read from in memory locations for testing.
type WriterPipeline ¶
WriterPipeline implements a generic pipeline powered by an io.Writer stream
func (*WriterPipeline) Config ¶
func (w *WriterPipeline) Config() *PipelineConfig
Config returns a configuration for the pipeline. Plain is specified on the pipeline itself and patched into the configuration.
func (*WriterPipeline) Initialize ¶
func (w *WriterPipeline) Initialize() error
Initialize has no logic in a WriterPipeline