s99term

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 18 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.

README

S99Term

S99Term is the terminal render layer for smegg99 command-line programs. It detects what the terminal can do on three independent axes and degrades to readable plain text at the bottom of each one.

Built with lipgloss, bubbles, colorprofile and s99logger.

What It Does

Console owns one output stream and is the only thing in the package that writes. It takes the stream from Options.Out, so nothing here ever reaches stdout; the consuming CLI passes os.Stderr and keeps stdout for machine output. Renderer composes plain strings and holds no stream, so every shape is testable without a terminal. Styles holds every colour, border and pad value, so no screen builds one inline.

A state is a glyph, a word and a colour, so a line reads the same without colour. Every glyph in status.go is exactly three cells wide and the word column is padded to a shared width, so the label starts at the same index on every line whatever the state is.

The package holds no human sentence. Words is a map[Status]string the consuming CLI supplies from its own locale catalog. The only literals here are field keys, which are identifiers and are never translated.

Step drives the bubbles spinner and progress components from a plain ticker. There is no Bubble Tea program, so nothing writes a terminal query and nothing leaves a reply in the input queue for the shell. Console.Sink() returns an s99logger.Sink, so a CLI has one writer for its logs and its presentation, and a log record that arrives while a step is animating is printed above the live line rather than through it.

The three axes

Each axis is detected separately, has its own override in Options, and falls back on its own. None of them costs a terminal round trip.

Axis Detected from Override Without it
Colour colorprofile.Detect(Out, Env) in console.go Options.Profile colorprofile.Writer strips the escapes the stream cannot carry; the glyph and the word stay
Animation whether Out is an *os.File on a terminal with a width Options.Animate Step prints nothing until End, which writes one finished line
Charset utf8 in charset.go, reading LC_ALL, LC_CTYPE and LANG in that order Options.Charset the styles pick lipgloss.ASCIIBorder, Console.frames picks spinner.Line, and a counted step's bar fills with # and -

Colour and charset are detected separately. A 16-colour UTF-8 terminal draws box borders, and a truecolor terminal under LANG=C does not, because it cannot render U+2500.

Prerequisites

Build Dependencies
  • Go 1.27, the version go.mod declares. No cgo.
  • just, which runs every recipe in justfile.
  • git, optional. Without it TestTagAgreesWithVersionFile skips.

This is a library. It builds no binary of its own.

Build

just check

check runs check-fmt, vet, test and race in that order. check-fmt fails when a file is not gofmt clean; fmt is the separate recipe that rewrites them. race matters here because the step ticker and Step.End share one mutex.

just demo

demo runs internal/demo, which prints one line of every state, one table and one counted step, and writes one JSON object to stdout so the two streams can be told apart.

Usage

import (
	"os"

	"github.com/smegg99/s99logger"

	"github.com/smegg99/s99term"
)

c := s99term.New(s99term.Options{
	Out:   os.Stderr,
	Env:   os.Environ(),
	Words: s99term.Words{s99term.StatusOK: "ok", s99term.StatusFailed: "failed"},
})
defer c.Close()

log := s99logger.New(c.Sink(), s99logger.Options{Service: "demo"})

step := c.BeginCounted("building locales", 4)
step.Advance(4)
step.End(s99term.StatusOK, s99term.F("keys", "412"))

c.Line(s99term.StatusSkipped, "desktop target not configured")

Options.Words is the only place human text enters the package. Leave a state out of the map and its line carries the glyph alone.

This is what just demo writes to stderr with LANG=C and stderr piped, so every axis is at its fallback:

s99term 1.1.0
[!] warning  stale_key  locale=cs count=3 service=demo time=21:04:13
[+] ok       building locales  keys=412 took=1.001s
[~] stale    checking drift  locale=cs took=501ms
[x] failed   building cs  reason="missing catalog"
[-] skipped  desktop target not configured
[ ] waiting  queued behind the sync lock
[*] running  watching for changes
             catalog_loaded  locales=3
+-----------+--------+-------+
| KEY       | LOCALE | STATE |
+-----------+--------+-------+
| app.title | pl     | ok    |
| app.sub   | cs     | stale |
+-----------+--------+-------+

Version

VERSION holds MAJOR.MINOR.PATCH and is the only place the version is written. version.go embeds it. Declared() returns exactly that string. A tagged commit whose VERSION disagrees with its tag fails TestTagAgreesWithVersionFile.

License

MIT. See LICENSE.

Documentation

Overview

Package s99term is the terminal render layer for smegg99 command-line programs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Declared

func Declared() string

Declared returns the version written in VERSION.

Types

type Console

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

Console owns one output stream: its colour level, its charset, its width, and the one live step that may be animating on it.

func New

func New(opts Options) *Console

New resolves the colour, animation and charset axes and builds the writer.

func (*Console) Begin

func (c *Console) Begin(label string) *Step

Begin starts a step whose end is not countable.

func (*Console) BeginCounted

func (c *Console) BeginCounted(label string, total int) *Step

BeginCounted starts a step with a known number of units, shown as a bar.

func (*Console) Charset

func (c *Console) Charset() bool

func (*Console) Close

func (c *Console) Close()

Close ends any live step and restores the cursor. Defer it in every command.

func (*Console) Line

func (c *Console) Line(st Status, label string, fields ...Field)

Line writes one status line.

func (*Console) Print

func (c *Console) Print(block string)

Print writes one already-rendered block, downsampled for the stream. A live step's frame is erased first and redrawn after, so the block is never torn.

func (*Console) Profile

func (c *Console) Profile() colorprofile.Profile

func (*Console) Renderer

func (c *Console) Renderer() Renderer

func (*Console) Sink

func (c *Console) Sink() s99logger.Sink

Sink returns an s99logger.Sink that renders records through the shared styles. The logger keeps owning the event stream; this owns its presentation.

func (*Console) Styles

func (c *Console) Styles() Styles

func (*Console) Width

func (c *Console) Width() int

Width returns the terminal width, or 80 when the stream has none.

type Field

type Field struct {
	Key   string
	Value string
}

Field is one key and value appended to a line. The key is an identifier, like an s99logger attr key, so it is never translated.

func Dur

func Dur(key string, d time.Duration) Field

Dur is a field holding a duration rounded to the millisecond.

func F

func F(key, value string) Field

F is a field with a value that is already a string.

type Options

type Options struct {
	// Out receives every line. Pass os.Stderr to keep stdout for machine output.
	Out io.Writer
	// Env feeds colour and charset detection. Nil reads os.Environ().
	Env []string
	// Words names each state. A state left out prints its glyph alone.
	Words Words
	// Profile overrides colour detection. The zero value detects.
	Profile colorprofile.Profile
	// Animate overrides animation. Nil animates only on a sized terminal.
	Animate *bool
	// Charset overrides the charset answer. Nil reads the locale variables.
	Charset *bool
	// Now supplies step timestamps. Nil uses time.Now.
	Now func() time.Time
}

Options configures a Console.

type Palette

type Palette struct {
	Accent color.Color
	Muted  color.Color
	Line   color.Color
	Good   color.Color
	Bad    color.Color
	Warn   color.Color
}

Palette holds every colour. All but Accent are ANSI indices, so no background query is needed.

type Renderer

type Renderer struct {
	Styles Styles
	Words  Words
}

Renderer composes the strings. It holds no stream, so every function here is testable without a terminal.

func NewRenderer

func NewRenderer(styles Styles, words Words) Renderer

NewRenderer pairs a palette with the catalog words for each state.

func (Renderer) Fields

func (r Renderer) Fields(fields ...Field) string

Fields renders key=value pairs, quoting any value that would break a line.

func (Renderer) Frame

func (r Renderer) Frame(spinner, label, trailer string) string

Frame is the live line while a step runs: spinner, label, and whatever the step wants to show on the right, such as a progress bar.

func (Renderer) Line

func (r Renderer) Line(st Status, label string, fields ...Field) string

Line is one finished statement: state, label, then whatever it measured.

func (Renderer) StatusCell

func (r Renderer) StatusCell(st Status) string

StatusCell is the glyph and the word, padded to the shared column width.

func (Renderer) Title

func (r Renderer) Title(text string) string

Title is a heading above a group of lines.

type Status

type Status int

Status is what a line says happened, independent of how it is coloured.

const (
	StatusNone Status = iota
	StatusOK
	StatusFailed
	StatusSkipped
	StatusWaiting
	StatusRunning
	StatusWarning
	StatusStale
)

StatusNone is an info-level log record: no state, but aligned under step lines.

func (Status) Glyph

func (st Status) Glyph() string

Glyph is the three-cell marker for st, so colour is never the only carrier.

type Step

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

Step is one unit of work the reader waits on. On a terminal it animates one inline line; on any other stream it prints nothing until it ends.

func (*Step) Advance

func (s *Step) Advance(n int)

Advance adds n finished units to a counted step.

func (*Step) End

func (s *Step) End(st Status, fields ...Field)

End closes the step and leaves exactly one finished line behind it; a later End does nothing.

type Styles

type Styles struct {
	Palette     Palette
	Border      lipgloss.Border
	Unicode     bool
	Title       lipgloss.Style
	Ink         lipgloss.Style
	Muted       lipgloss.Style
	AccentStyle lipgloss.Style
	Line        lipgloss.Style
	Good        lipgloss.Style
	Bad         lipgloss.Style
	Warn        lipgloss.Style
	Key         lipgloss.Style
	Field       lipgloss.Style
	Spinner     lipgloss.Style
	Panel       lipgloss.Style
	StatusWidth int
}

Styles owns every colour, border and pad value in the package.

func (Styles) Table

func (s Styles) Table() *table.Table

Table is the one table shape in the package.

type Words

type Words map[Status]string

Words maps each state to its localized name, supplied by the consuming CLI.

Directories

Path Synopsis
internal
demo command
Command demo prints one of every rendered shape for checking the palette.
Command demo prints one of every rendered shape for checking the palette.

Jump to

Keyboard shortcuts

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