clui

package module
v0.0.0-...-b2c5cd7 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2021 License: MPL-2.0 Imports: 6 Imported by: 8

README

Go Clui

Opinionated, minimalistic and cross platform Go library to manage output of command line apps.

Clui concepts:

  • Layout: the output style, eg. plain or machine readable
  • VerbosityLevel: how the output is filtered
  • Interactivity: if wait for user's answers

Import the library:

import (
    "github.com/enr/clui"
)

Creation of a default Clui:

ui, err := clui.NewClui()

Creation with configuration:

verbosity := func(ui *clui.Clui) {
    ui.VerbosityLevel = clui.VerbosityLevelHigh
}
ui, _ := clui.NewClui(verbosity)

See examples directory for more.

License

A lot of code of this library was taken from Packer UI released under the same license (Mozilla Public License Version 2.0).

Mozilla Public License Version 2.0 - see LICENSE file.

Copyright 2015 clui contributors

Documentation

Index

Constants

View Source
const (
	// UIColorNone no color
	UIColorNone UIColor = 0
	// UIColorRed red
	UIColorRed = 31
	// UIColorGreen green
	UIColorGreen = 32
	// UIColorYellow yellow
	UIColorYellow = 33
	// UIColorBlue blue
	UIColorBlue = 34
	// UIColorMagenta magenta
	UIColorMagenta = 35
	// UIColorCyan cyan
	UIColorCyan = 36
	// UIColorGray gray
	UIColorGray = 37
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Clui

type Clui struct {
	Layout         Layout
	VerbosityLevel VerbosityLevel
	Interactive    bool
	Color          bool
	Reader         io.Reader
	StdWriter      io.Writer
	ErrorWriter    io.Writer
}

Clui is the actual message writer.

func DefaultClui

func DefaultClui() *Clui

DefaultClui is the factory function for a Clui with default configuration.

func NewClui

func NewClui(options ...func(*Clui)) (*Clui, error)

NewClui is the factory function allowing to configure the Clui.

func (*Clui) Confidential

func (c *Clui) Confidential(message string)

Confidential write a confidential message

func (*Clui) Confidentialf

func (c *Clui) Confidentialf(format string, a ...interface{})

Confidentialf write a confidential message

func (*Clui) Error

func (c *Clui) Error(message string)

Error write a error message

func (*Clui) Errorf

func (c *Clui) Errorf(format string, a ...interface{})

Errorf write a error message

func (*Clui) Lifecycle

func (c *Clui) Lifecycle(message string)

Lifecycle write a info message

func (*Clui) Lifecyclef

func (c *Clui) Lifecyclef(format string, a ...interface{})

Lifecyclef write a info message

func (*Clui) Question

func (c *Clui) Question(query string) (string, error)

Question to user. Returns an empty string if called in no-interactive mode.

func (*Clui) QuestionWithDefault

func (c *Clui) QuestionWithDefault(query string, defaultValue string) (string, error)

QuestionWithDefault is a question to user with default value. Returns the default value if called in no-interactive mode.

func (*Clui) Success

func (c *Clui) Success(message string)

Success write a success message

func (*Clui) Successf

func (c *Clui) Successf(format string, a ...interface{})

Successf write a success message

func (*Clui) Title

func (c *Clui) Title(message string)

Title write a title message

func (*Clui) Titlef

func (c *Clui) Titlef(format string, a ...interface{})

Titlef write a title message

func (*Clui) Warn

func (c *Clui) Warn(message string)

Warn write a warn message

func (*Clui) Warnf

func (c *Clui) Warnf(format string, a ...interface{})

Warnf write a warn message

type Layout

type Layout interface {
	// Apply merge category and text.
	Apply(MessageCategory, string) string
	// SupportsColors returns if the Layout can render colored messages in the console.
	SupportsColors() bool
}

Layout of the final message

type MachineReadableLayout

type MachineReadableLayout struct{}

MachineReadableLayout layout for programmatic access

func (*MachineReadableLayout) Apply

func (l *MachineReadableLayout) Apply(category MessageCategory, message string) string

Apply returns a comma separated string

func (*MachineReadableLayout) SupportsColors

func (l *MachineReadableLayout) SupportsColors() bool

SupportsColors return always false

type MessageCategory

type MessageCategory int8

MessageCategory classify the message

const (
	// MessageCategoryConfidential confidential messages
	MessageCategoryConfidential MessageCategory = iota - 1
	// MessageCategoryInfo info
	MessageCategoryInfo
	// MessageCategoryWarn warning
	MessageCategoryWarn
	// MessageCategoryError error
	MessageCategoryError
	// MessageCategoryQuestion question
	MessageCategoryQuestion
)

type PlainLayout

type PlainLayout struct{}

PlainLayout is the defaut and simplest Layout

func (*PlainLayout) Apply

func (l *PlainLayout) Apply(category MessageCategory, message string) string

Apply returns the text message string

func (*PlainLayout) SupportsColors

func (l *PlainLayout) SupportsColors() bool

SupportsColors returns always true

type UIColor

type UIColor uint

UIColor represents color

type VerbosityLevel

type VerbosityLevel int8

VerbosityLevel of messages

const (
	// VerbosityLevelMute mutes all messages
	VerbosityLevelMute VerbosityLevel = iota - 1
	// VerbosityLevelLow allows warn and error
	VerbosityLevelLow
	// VerbosityLevelMedium allows lifecycle, success, warn and error
	VerbosityLevelMedium
	// VerbosityLevelHigh allows all messages
	VerbosityLevelHigh
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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