inline

package
v0.7.5 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: BSD-3-Clause Imports: 11 Imported by: 2 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.

README

inline

Styled text and in-place progress in the normal terminal scrollback. No raw mode, no alternate screen, no input handling, no cursor hiding — the shell keeps owning the terminal. Intended for CLI tools (package managers, service tooling, build scripts) that want color and live status without a full-screen TUI.

Cross-platform (Unix, Windows). Depends only on the color package for 24-bit RGB inputs, the ANSI 16 indices, and automatic degradation to 256 or 16 colors.

Model

Output is split into two zones:

installed openssl          ← permanent lines (Log) — scroll normally
installed zlib
⠹ installing curl          ← live block (Update) — rewritten in place
[██████░░░░░░░░] 2/5

Log prints permanent lines above the live block; Update replaces the live block by cursor-up + clear + rewrite; Done erases the block and optionally prints final lines. Interleaving is handled internally — Log during an active live block erases, prints, and redraws in one flush.

API

Printer
  • New(w io.Writer) *Printer: a printer, safe for concurrent use but for SetColor, which races a Paint on another goroutine. Its output is a terminal when w is an *os.File on one; a terminal that reports no size, a serial line, is still one, at 80×24. Color defaults on for a terminal with NO_COLOR unset and TERM not dumb.
  • Log(format string, a ...any): prints one permanent line above the live block (Printf semantics, newline appended).
  • Update(lines ...string): replaces the live block, rewriting in place; a no-op on non-terminal output.
  • Done(final ...string): erases the live block and prints final permanent lines. Call before exit.
  • Paint(s string, st Style) string: s wrapped in SGR codes for the detected color mode, or unchanged when color is off.
  • SetColor(on bool): overrides color detection, to force styling into a pipe for less -R. Affects Paint only; Update remains terminal-gated.
  • Size() (w, h int): the terminal's current dimensions, 80×24 when unknown.
  • IsTerminal() bool: whether the output is a terminal.
Style

A value type, unstyled at its zero value, composed by its builders:

  • Fg(c color.RGB) Style, FgANSI(i uint8) Style: a style with a foreground, an ANSI 16 one for FgANSI (color.ANSIRed..).
  • (s Style) Bg(c color.RGB) Style, (s Style) BgANSI(i uint8) Style: adds a background, or an ANSI 16 one.
  • (s Style) Bold() Style: adds bold; also Dim, Italic, Underline, Blink and Reverse.
warn := inline.Fg(color.Amber).Bold()
p.Log("%s low disk space", p.Paint("warning:", warn))

True color terminals get 38;2;R;G;B; 256-color terminals get 38;5;N via Redmean mapping dynamically handled by the color package; 16-color terminals (TERM of linux, cons25*, vt and a digit, ansi or *-16color, whatever COLORTERM says) get the nearest ANSI index.

ANSI 16 colors (FgANSI, BgANSI) are sent as 30-37/90-97 in every mode: the terminal's theme picks the shade, and a text console renders them as faithfully as an emulator. Prefer them for semantic colors such as log levels.

errSt := inline.FgANSI(color.ANSIRed).Bold()
p.Log("%s disk full", p.Paint("ERROR", errSt))
Progress helpers

Pure string builders, no Printer required:

  • Bar(width int, pct float64, chars [3]rune) string: a progress bar of width cells, pct clamped to [0,1], half-cell resolution via the partial rune.
  • BarBlock: the default characters, [3]rune{'█', '▌', '░'}.
  • Spinner(frame int) string: the braille spinner frame for a monotonic counter.

Compose with Paint for colored bars:

line := "[" + p.Paint(inline.Bar(30, pct, inline.BarBlock), barStyle) + "]"

Non-terminal output

When output is a pipe or file (CI, redirection): Update is a no-op, Paint returns input unchanged, Log and Done print plain sequential text. A tool using inline degrades to ordinary log output with no code changes.

Example

Simulated package installation — spinner, overall progress bar, permanent completion lines:

package main

import (
	"fmt"
	"os"
	"time"

	"github.com/lixenwraith/color"
	"github.com/lixenwraith/terminal/inline"
)

func main() {
	p := inline.New(os.Stdout)

	name := inline.Fg(color.LightSkyBlue).Bold()
	okSt := inline.Fg(color.LimeGreen).Bold()
	dim := inline.Fg(color.IronGray)

	pkgs := []string{"openssl", "zlib", "curl", "git", "go"}
	frame := 0

	for i, pkg := range pkgs {
		const steps = 25
		for s := range steps {
			pct := (float64(i) + float64(s)/steps) / float64(len(pkgs))
			p.Update(
				inline.Spinner(frame)+" installing "+p.Paint(pkg, name),
				"["+inline.Bar(32, pct, inline.BarBlock)+"] "+
					p.Paint(fmt.Sprintf("%d/%d", i+1, len(pkgs)), dim),
			)
			frame++
			time.Sleep(40 * time.Millisecond)
		}
		p.Log("%s %s", p.Paint("✓", okSt), pkg)
	}

	p.Done(p.Paint("✓ 5 packages installed", okSt))
}

Notes

  • Width is measured in runes (unicode/utf8); East Asian wide characters and combining marks are not width-aware.
  • Live block lines must occupy one visual row each: no \n, tabs, or control characters. Lines are truncated to terminal width automatically; embedded SGR from Paint is preserved through truncation.
  • The live block is clamped to terminal height − 1 rows (newest lines kept).
  • Pass external strings (package names, paths) as Log arguments, never as the format string.
  • Ctrl-C mid-update leaves the live block on screen but the terminal in a normal state — no raw mode or screen buffer to restore.

Documentation

Overview

Package inline renders styled text and in-place progress in the normal terminal scrollback: no raw mode, no alternate screen, no input handling, no cursor hiding. Intended for CLI tools that want color and live status without owning the screen.

Model: permanent lines scroll via Log; a live block of status lines is pinned below them and rewritten in place via Update. Done erases the block and optionally prints final permanent lines.

Non-terminal output (pipes, CI): Update is a no-op, styling is stripped unless overridden with SetColor(true), Log and Done print plainly.

Width is measured in runes (unicode/utf8); wide and combining characters are not width-aware.

Index

Constants

This section is empty.

Variables

View Source
var BarBlock = [3]rune{'█', '▌', '░'}

BarBlock is the default bar character set [filled, partial, empty]

Functions

func Bar

func Bar(width int, pct float64, chars [3]rune) string

Bar renders an unstyled progress bar of width cells, pct in [0,1]

func Spinner

func Spinner(frame int) string

Spinner returns the frame for a monotonic counter

Types

type Attribute

type Attribute uint8

Attribute represents visual text modifiers for inline styling

const (
	AttributeNone Attribute = 0
	Bold          Attribute = 1 << 0
	Dim           Attribute = 1 << 1
	Italic        Attribute = 1 << 2
	Underline     Attribute = 1 << 3
	Blink         Attribute = 1 << 4
	Reverse       Attribute = 1 << 5
)

type Printer

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

Printer manages styled output and the live block. Safe for concurrent use but for SetColor, which races a Paint on another goroutine.

func New

func New(w io.Writer) *Printer

New creates a Printer for w. A terminal that reports no size, a serial line, is still one, at 80x24; styling defaults on for terminals with NO_COLOR unset and TERM not dumb.

func (*Printer) Done

func (p *Printer) Done(final ...string)

Done erases the live block and prints final permanent lines

func (*Printer) IsTerminal

func (p *Printer) IsTerminal() bool

IsTerminal reports whether the output is a terminal

func (*Printer) Log

func (p *Printer) Log(format string, a ...any)

Log prints a permanent line above the live block

func (*Printer) Paint

func (p *Printer) Paint(s string, st Style) string

Paint returns s styled for the detected terminal, unchanged when color is disabled. Composes with Log: p.Log("%s %s", p.Paint("ok", st), name)

func (*Printer) SetColor

func (p *Printer) SetColor(on bool)

SetColor overrides style detection. Affects Paint only; live-block updates remain terminal-gated.

func (*Printer) Size

func (p *Printer) Size() (w, h int)

Size returns terminal dimensions, 80×24 when unknown

func (*Printer) Update

func (p *Printer) Update(lines ...string)

Update replaces the live block, rewriting in place. No-op on non-terminal output.

type Style

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

Style describes text appearance; zero value is unstyled. Composable: inline.Fg(color.Amber).Bold().Underline()

func Fg

func Fg(c color.RGB) Style

Fg starts a style with foreground color

func FgANSI

func FgANSI(i uint8) Style

FgANSI starts a style with an ANSI 16 foreground (color.ANSIRed..). The terminal's theme picks the shade, and every terminal, a text console included, renders it.

func (Style) Bg

func (s Style) Bg(c color.RGB) Style

Bg sets background color

func (Style) BgANSI

func (s Style) BgANSI(i uint8) Style

BgANSI sets an ANSI 16 background

func (s Style) Blink() Style

Blink applies the blink attribute

func (Style) Bold

func (s Style) Bold() Style

Bold applies the bold attribute

func (Style) Dim

func (s Style) Dim() Style

Dim applies the dim/faint attribute

func (Style) Italic

func (s Style) Italic() Style

Italic applies the italic attribute

func (Style) Reverse

func (s Style) Reverse() Style

Reverse applies the reverse video attribute

func (Style) Underline

func (s Style) Underline() Style

Underline applies the underline attribute

Jump to

Keyboard shortcuts

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