ui

package
v0.39.1 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package ui provides the terminal UI formatter for Hermit.

This encapsulates both logging and progress.

Output will be cleared if the higher level Hermit operation is successful.

Hermit progress is conveyed via a single progress bar at the bottom of its output, ala modern Ubuntu apt progress. The line below the progress bar will be the list of concurrent actions being run. The capacity of the progress bar will dynamically adjust as new tasks are added.

The progress bar will use partial Unicode blocks: https://en.wikipedia.org/wiki/Block_Elements#Character_table

Log output appears above the progress bar.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LogElapsed

func LogElapsed(log Logger, message string, args ...interface{}) func()

LogElapsed logs the duration of a function call. Use with defer:

defer LogElapsed(log, "something")()

Types

type Level

type Level int

Level for a log message.

const (
	// LevelAuto will detect the log level from the environment via
	// HERMIT_LOG=<level>, DEBUG=1, then finally from flag.
	LevelAuto  Level = iota // auto
	LevelTrace              // trace
	LevelDebug              // debug
	LevelInfo               // info
	LevelWarn               // warn
	LevelError              // error
	LevelFatal              // fatal
)

Log levels.

func AutoLevel added in v0.10.0

func AutoLevel(level Level) Level

AutoLevel sets the log level from environment variables if set to LevelAuto.

func LevelFromString

func LevelFromString(s string) (Level, error)

LevelFromString maps a level to a string.

func (Level) String

func (i Level) String() string

func (*Level) UnmarshalText

func (l *Level) UnmarshalText(text []byte) error

func (Level) Visible

func (l Level) Visible(other Level) bool

Visible returns true if "other" is visible.

type Logger

type Logger interface {
	io.Writer
	Tracef(format string, args ...interface{})
	Debugf(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Warnf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
	Fatalf(format string, args ...interface{})
	WriterAt(level Level) SyncWriter
}

Logger interface.

type SyncWriter

type SyncWriter interface {
	io.Writer
	Sync() error
}

SyncWriter is an io.Writer that can be Sync()ed.

type Task

type Task struct {
	// contains filtered or unexported fields
}

Task encapsulates progress and logging for a single operation.

Operations are not thread safe.

func (*Task) Add

func (o *Task) Add(n int)

Add to progress of the Task.

func (*Task) Debugf

func (l *Task) Debugf(format string, args ...interface{})

Debugf logs a message at debug level.

func (*Task) Done

func (o *Task) Done()

Done marks the operation as complete.

func (*Task) Errorf

func (l *Task) Errorf(format string, args ...interface{})

Errorf logs a message at error level.

func (*Task) Fatalf

func (l *Task) Fatalf(format string, args ...interface{})

Fatalf logs a fatal message and exits with a non-zero status.

Additionally, log output will not be cleared.

func (*Task) Infof

func (l *Task) Infof(format string, args ...interface{})

Infof logs a message at info level.

func (*Task) ProgressWriter

func (o *Task) ProgressWriter() io.Writer

ProgressWriter returns a writer that moves the progress bar as it is written to.

The Size() should have previously been set to the maximum number of bytes that will be written.

func (*Task) Size

func (o *Task) Size(n int) *Task

Size sets the size of the Task.

func (*Task) SubProgress

func (o *Task) SubProgress(subtask string, size int) *Task

SubProgress registers and returns a new subtask with progress.

func (*Task) SubTask

func (o *Task) SubTask(subtask string) *Task

SubTask creates a new unsized subtask.

The size of the operations progress can be configured later.

func (*Task) Tracef

func (l *Task) Tracef(format string, args ...interface{})

Tracef logs a message at trace level.

func (*Task) Warnf

func (l *Task) Warnf(format string, args ...interface{})

Warnf logs a message at warning level.

func (*Task) WillLog

func (o *Task) WillLog(level Level) bool

WillLog returns true if "level" will be logged.

func (*Task) WriterAt

func (l *Task) WriterAt(level Level) SyncWriter

type UI

type UI struct {
	// contains filtered or unexported fields
}

UI controls the display of logs and task progress.

func New

func New(level Level, stdout, stderr SyncWriter, stdoutIsTTY, stderrIsTTY bool) *UI

New creates a new UI.

func NewForTesting

func NewForTesting() (*UI, *bytes.Buffer)

NewForTesting returns a new UI that writes all output to the returned bytes.Buffer.

func (*UI) Clear

func (w *UI) Clear()

Clear the progress indicator.

func (*UI) Confirmation added in v0.2.21

func (w *UI) Confirmation(message string, args ...interface{}) (bool, error)

Confirmation from the user with y/N options to proceed

func (UI) Debugf

func (l UI) Debugf(format string, args ...interface{})

Debugf logs a message at debug level.

func (UI) Errorf

func (l UI) Errorf(format string, args ...interface{})

Errorf logs a message at error level.

func (UI) Fatalf

func (l UI) Fatalf(format string, args ...interface{})

Fatalf logs a fatal message and exits with a non-zero status.

Additionally, log output will not be cleared.

func (UI) Infof

func (l UI) Infof(format string, args ...interface{})

Infof logs a message at info level.

func (*UI) Printf

func (w *UI) Printf(format string, args ...interface{})

Printf prints directly to the stdout without log formatting

func (*UI) Progress

func (w *UI) Progress(task string, size int) *Task

Progress creates a new task with progress indicator of size.

The resulting Task can be used as a ui.Logger.

func (*UI) SetLevel

func (w *UI) SetLevel(level Level)

SetLevel sets the UI's minimum log level.

func (*UI) SetProgressBarEnabled

func (w *UI) SetProgressBarEnabled(enabled bool)

SetProgressBarEnabled defines if we want to show the progress bar to the user

func (*UI) Task

func (w *UI) Task(task string) *Task

Task creates a new unstarted task.

The resulting Task can be used as a ui.Logger.

Task progress can be modified later.

func (UI) Tracef

func (l UI) Tracef(format string, args ...interface{})

Tracef logs a message at trace level.

func (UI) Warnf

func (l UI) Warnf(format string, args ...interface{})

Warnf logs a message at warning level.

func (*UI) WillLog

func (w *UI) WillLog(level Level) bool

WillLog returns true if "level" will be logged.

func (UI) WriterAt

func (l UI) WriterAt(level Level) SyncWriter

Jump to

Keyboard shortcuts

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