consoleutil

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: MIT Imports: 40 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DetectWidth

func DetectWidth() int

Width in cells output wraps at: COLUMNS, else the terminal on stdout, the terminals a live display captured, stderr or stdin, else 80

func EmojiShortName

func EmojiShortName(emoji string) (string, bool)

Shortest GitHub style name of an emoji, the name chat platforms take for reactions, a skin toned emoji falling back to the name of its plain form

func Escape

func Escape(text string) string

Markup that prints the given text as written wherever it is placed in other markup, no part of it read as a tag: tag shaped parts escaped, a backslash before a bracket doubled, trailing backslashes doubled and closed off by an empty tag pair

func Plain

func Plain(markup string) string

Text of console markup with its tags resolved, their styles and any escape sequences dropped

func Render

func Render(markup string, color bool) string

Text of console markup, styled with escape sequences for the terminal's color profile when color is set, plain otherwise

func TerminalProfile

func TerminalProfile() colorprofile.Profile

Color profile of the terminal the program runs in, for output the program has chosen to style: what the terminal shows, at least the sixteen standard colors, attributes without colors under a non empty NO_COLOR

func WrapStyled

func WrapStyled(text string, width int) []string

Lines of ANSI styled text wrapped at width cells, long words folded, each line closing and reopening the styles it draws in

Types

type BarColumn

type BarColumn struct{ Width int }

Bar of Width cells, forty when zero, filling as steps are done and pulsing while the total is unknown

type Column

type Column struct {
	// Header text, printed as written in bold
	Header string
	// Style definition every cell of the column draws in
	Style string
	// Cells of content the column always takes, zero to fit the content
	Width int
	// Most cells of content the column takes, zero for no limit
	MaxWidth int
	// Side cells align to: left when empty, center or right
	Justify string
	// What a word wider than the column does: ellipsis when empty cuts it with an ellipsis, fold breaks it across lines, crop cuts it
	Overflow string
}

One column of a table: its header, the style its cells draw in and how they fit the column

type Console

type Console struct {
	// Destination of everything printed, nothing is written when nil
	Out io.Writer
	// Whether output is styled with escape sequences
	Color bool
	// Colors styled output uses, the terminal's own when left unknown or none
	Profile colorprofile.Profile
	// Cells lines wrap at, DetectWidth when zero
	Width int
	// Whether Out is an interactive terminal, the only kind status spinners and progress displays animate on
	Terminal bool
	// Every print dropped
	Quiet bool
	// Automatic coloring of numbers, quoted strings, paths, addresses and similar literals in printed markup turned off
	NoHighlight bool
	// Hyperlinks written as their address in parentheses after the linked text instead of as terminal links
	NoLinks bool
	// Receives the plain text of every print in place of writing it: printed markup unwrapped, tables, panels and renderables as their lines at the width
	Capture func(text string)
	// contains filtered or unexported fields
}

Printer of console markup, tables, panels and other renderables to a writer, wrapped at a width and styled with escape sequences when color is on

func NewConsole

func NewConsole(w io.Writer) *Console

Console over a writer, styled in the terminal's colors (or forced ones under FORCE_COLOR or CLICOLOR_FORCE) and animated only when the writer is a terminal that is not dumb

func (*Console) Input

func (c *Console) Input(prompt string) (string, error)

Line typed on standard input after the markup prompt, without its line end; io.EOF when input ends before any character

func (*Console) NewProgress

func (c *Console) NewProgress(refresh time.Duration, columns ...ProgressColumn) *Progress

Progress display over the console redrawn every refresh, a tenth of a second when not positive

func (*Console) Print

func (c *Console) Print(markup ...string)

Prints console markup ([bold red]text[/] styles a run, [link=https://x.io]site[/] links one, \[ is a literal bracket) joined by spaces, wrapped at the width, its literals colored unless NoHighlight is set, a blank line when called without markup

func (*Console) PrintNoHighlight

func (c *Console) PrintNoHighlight(markup ...string)

Prints markup joined by spaces, wrapped at the width, with no coloring of its literals

func (*Console) PrintPanel

func (c *Console) PrintPanel(p Panel)

Prints a panel at the width, or fitted to its content when it asks

func (*Console) PrintRenderableWith

func (c *Console) PrintRenderableWith(r Renderable, o PrintOptions)

Prints a renderable at the width with the options applied, a style that does not parse laying nothing under it

func (*Console) PrintSoft

func (c *Console) PrintSoft(markup string)

Prints markup on lines left unwrapped for the terminal to wrap, so long commands and paths stay whole for copying

func (*Console) PrintTable

func (c *Console) PrintTable(t *Table)

Prints a table fitted to the width

func (*Console) Status

func (c *Console) Status(markup string) *Status

Starts a spinner beside the markup on a terminal, every console print to that terminal and, on Unix, whatever reaches the standard output and error descriptors shown above it meanwhile

type DescriptionColumn

type DescriptionColumn struct{}

Markup description of the task

type Markdown

type Markdown string

Document in markdown drawn for a terminal: centered and styled headings, emphasis, inline and syntax highlighted code, quotes, lists, tables, links and images

type Panel

type Panel struct {
	// Markup inside the box when Content is nil
	Body string
	// Markup set in the top border
	Title string
	// Markup set in the bottom border
	Subtitle string
	// Style definition of the border
	BorderStyle string
	// Box sized to its content instead of the whole width
	Fit bool
	// Renderable drawn inside the box in place of Body
	Content Renderable
	// Border drawn: rounded corners when empty, horizontals for rules above and below only
	Box string
	// Side the title and subtitle sit on: centered when empty, left or right
	TitleAlign string
	// Style definition laid under the border, the padding and what the box holds
	Style string
	// Lines of padding above and below and cells of padding on each side inside the border, none and one when nil
	Padding *[2]int
	// Most cells the box takes, the print width when zero
	Width int
}

Box drawn around markup or a renderable, a title set in its top border and a subtitle in its bottom one, as wide as it prints unless fitted

func (Panel) Render

func (p Panel) Render(color bool, width int) string

Lines of the box at width cells, styled for the terminal when color is set

type PrintOptions

type PrintOptions struct {
	// Style definition laid under everything printed
	Style string
	// Lines left unwrapped and uncut so the terminal wraps them
	SoftWrap bool
}

Settings of one renderable print beyond the console's own

type Progress

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

Live display of task rows redrawn in place on a terminal, the final rows printed once on any other writer

func (*Progress) AddTask

func (p *Progress) AddTask(description string, total int) int

Adds a running task of total steps, below zero for an unknown total, and returns its id

func (*Progress) Advance

func (p *Progress) Advance(id int, n int)

Advances a task by n steps, an unknown id doing nothing

func (*Progress) RemoveTask

func (p *Progress) RemoveTask(id int)

Removes a task row, an unknown id doing nothing

func (*Progress) Start

func (p *Progress) Start()

Starts the display: redrawn in place on a terminal, every console print to that terminal and, on Unix, whatever reaches the standard output and error descriptors shown above it whole lines at a time; nothing drawn on any other writer until Stop

func (*Progress) Stop

func (p *Progress) Stop()

Stops the display after a last frame left on screen, the final rows printed once when nothing was drawn live; when it returns the standard descriptors are back, what was written to them has been printed and its goroutines and descriptors are gone

func (*Progress) Update

func (p *Progress) Update(id int, description string)

Replaces the markup description of a task, an unknown id doing nothing

type ProgressColumn

type ProgressColumn interface {
	// contains filtered or unexported methods
}

One cell of each task row of a progress display

type Renderable

type Renderable interface {
	// contains filtered or unexported methods
}

Something a console prints as lines laid out at its width: Text, Markdown, a Table or a Panel

type SpinnerColumn

type SpinnerColumn struct{}

Dots spinner, a blank once the task finishes

type Status

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

Spinner beside a markup message on a terminal while work runs, erased when stopped, drawing nothing on other writers

func (*Status) Stop

func (s *Status) Stop()

Stops the spinner and erases its line, the standard descriptors given back and its goroutines gone when it returns, a second call doing nothing

func (*Status) Update

func (s *Status) Update(markup string)

Replaces the markup beside the spinner

type Style

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

Text style of console markup: foreground and background colors, attributes switched on or explicitly off, a hyperlink

func ParseStyle

func ParseStyle(def string) (Style, error)

Style a definition such as "bold red on white", "not italic", "#ff8800", "color(208)", "rgb(10,20,30)" or "link https://example.com" describes

func (Style) Render

func (s Style) Render(text string, p colorprofile.Profile) string

Text wrapped in the escape sequences that draw it in this style on a terminal with the given color profile, the text alone on a profile without escape sequences

type Table

type Table struct {
	// Markup centered above the table
	Title   string
	Columns []Column
	// Header row left out
	HideHeader bool
	// Borders and rules left out, cells set apart by their padding alone
	NoBox bool
	// Cells of padding on each side of every cell, one when zero
	PadX int
	// contains filtered or unexported fields
}

Grid of markup cells under bold headers in a box with a heavy head, an italic title centered above it, fitted to the width it prints in

func (*Table) AddRow

func (t *Table) AddRow(cells ...string)

Appends one row of markup cells, missing cells left empty

func (*Table) Render

func (t *Table) Render(color bool) string

Lines of the table at its natural width, styled for the terminal when color is set

func (*Table) RenderWidth

func (t *Table) RenderWidth(color bool, width int) string

Lines of the table fitted to width cells, styled for the terminal when color is set

type Task

type Task struct {
	ID          int
	Description string
	// Steps done so far
	Completed int
	// Steps in all, below zero while unknown
	Total int
	// Time since the task was added, frozen once it finished
	Elapsed time.Duration
	// Whether the steps done reached the total
	Finished bool
}

What a progress column knows about one task when it draws

type TaskColumn

type TaskColumn func(t Task) string

Markup a function builds from each task

type TaskProgressColumn

type TaskProgressColumn struct{}

Right aligned percentage done, empty while the total is unknown

type Text

type Text string

ANSI styled or plain text, its colors, attributes and hyperlinks kept, printed as it is and wrapped at the width

type TextColumn

type TextColumn string

Fixed markup, the same on every row

type TimeElapsedColumn

type TimeElapsedColumn struct{}

Time since the task was added, frozen at its finish

type TimeRemainingColumn

type TimeRemainingColumn struct{}

Estimated time left, dashes while the speed is unknown, empty while the total is

Jump to

Keyboard shortcuts

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