canvas

package
v0.0.0-...-9759625 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package canvas draws time-series graphs with braille characters, btop-style: 4 dot rows and 2 dot columns packed per terminal cell.

Index

Constants

View Source
const BigNumberHeight = 5

BigNumberHeight is the row count of every BigNumber render.

Variables

This section is empty.

Functions

func Bar

func Bar(width int, frac float64, filled, empty lipgloss.Style) string

Bar renders a horizontal fraction bar of the given cell width using full blocks and eighth-block edges.

func BigNumber

func BigNumber(v int, ramp Ramp) []string

BigNumber renders v as a 5-line block-digit banner colored along the ramp at value/100 — the btop-style hero readout for headline metrics.

func BigNumberWidth

func BigNumberWidth(n int) int

BigNumberWidth reports the cell width BigNumber needs for n glyphs (3 columns each plus a 1-column gap between glyphs).

func DualBar

func DualBar(width int, frac float64, rampA, rampB Ramp) string

DualBar renders a glances-style mixed meter of width cells: the first frac of the bar interpolates across rampA and the remainder across rampB, so a single always-full bar visualizes a direction split (download vs upload, read vs write) while the magnitude lives in the neighboring rate text. Whole bars are memoized per (rampA, rampB, width, fraction-step).

func GradientBar

func GradientBar(width int, frac float64, ramp Ramp, empty lipgloss.Style) string

GradientBar renders a btop-style meter: the filled part interpolates across the ramp along the bar's length (so long bars sweep green → red by position, not by threshold), the remainder renders dim. The edge cell uses an eighth block for sub-cell precision. Fills are memoized per (ramp, width, fraction-step) for typical meter widths — live values repeat far more often than they change, so most frames reuse the string. The fraction is quantized BEFORE the geometry is derived, so a cache step always renders exactly one bar shape.

func OverlayLabel

func OverlayLabel(block, label string) string

OverlayLabel writes label into the top-right corner of a rendered block (e.g. a braille graph), shortening the block's top row to make room — the place for a rate graph's auto-scale ceiling. The label is expected pre-styled; when almost no room remains the block returns unchanged.

func Sparkline

func Sparkline(values []float64, ramp Ramp) string

Sparkline renders the values (each 0..1) as one row of block glyphs, colored per glyph by ramping on the value.

func SparklineScaled

func SparklineScaled(values []float64, ramp Ramp) string

SparklineScaled renders raw-rate samples (any magnitude) as one row of block glyphs, scaling the window so its tallest sample fills the full height — the btop activity look. All-zero windows render as blanks.

Types

type Graph

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

Graph is a scrolling line graph backed by a ring buffer. Values are fractions in [0,1] (or [0,Max] when Max is set) and render as an area filled from the bottom. Use New; the zero value is not usable.

func New

func New(width, height int, ramp []color.Color) *Graph

New returns a graph of the given cell size. The ramp colors cells by the value of their column, from low to high.

func (*Graph) Push

func (g *Graph) Push(v float64)

Push appends one sample.

func (*Graph) Resize

func (g *Graph) Resize(width, height int)

Resize keeps the newest samples that fit the new capacity.

func (*Graph) SetMax

func (g *Graph) SetMax(max float64)

SetMax fixes the scale top; values are expected within [0, max].

func (*Graph) SetRamp

func (g *Graph) SetRamp(ramp []color.Color)

SetRamp replaces the color ramp and rebuilds the derived styles (used for live theme changes).

func (*Graph) View

func (g *Graph) View() string

View renders the graph as `height` lines of `width` cells each.

type Ramp

type Ramp []string

Ramp is an ordered list of hex colors ("#rrggbb") interpolated left to right across bars and sparklines. Ramps are treated as immutable once built: derived styles are cached per instance.

func (Ramp) At

func (r Ramp) At(t float64) string

At interpolates the ramp at t ∈ [0,1] and returns a hex color.

func (Ramp) Colors

func (r Ramp) Colors() []color.Color

Colors adapts the ramp to the []color.Color shape canvas.Graph takes.

func (Ramp) StyleAt

func (r Ramp) StyleAt(t float64) lipgloss.Style

StyleAt returns a cached foreground style for t ∈ [0,1] along the ramp — the allocation-free path for coloring values (table cells, hero digits).

type Scale

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

Scale tracks a decaying ceiling for auto-scaled rate graphs, btop style: it jumps instantly to fit a spike and then decays slowly back toward the recent values. The zero value is usable (ceiling 1).

func (*Scale) Max

func (s *Scale) Max() float64

Max returns the current ceiling.

func (*Scale) Observe

func (s *Scale) Observe(v float64) float64

Observe records one sample and returns the ceiling to render it against. The decay of 2% per sample yields a ~34s half-life at a 1s refresh; the ceiling never drops below the current sample or 1.

Jump to

Keyboard shortcuts

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