spectrum

package
v0.22.0 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package spectrum implements an audio-style spectrum analyzer widget.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AlertControl

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

AlertControl manages a threshold dropdown, an alarm checkbox, and an optional warning banner for a spectrum visualization.

The control is intended for overlay-style demos or dashboards that want a ready-made alarm UI without reimplementing layout, state, and mouse wiring. NewAlertControl remains the single constructor entry point; additional methods extend behavior without introducing alternate setup paths.

func NewAlertControl

func NewAlertControl(min, max, step, selected int, onChange func(int) error) (*AlertControl, error)

NewAlertControl returns a new threshold and alarm control.

The generated thresholds cover the inclusive range `[min, max]` in `step` increments. The selected threshold snaps to the nearest generated value. The onChange callback is the low-level threshold hook and runs after user-driven threshold changes.

func (*AlertControl) AlertMessage

func (ac *AlertControl) AlertMessage() string

AlertMessage returns the current warning banner message.

func (*AlertControl) Draw

func (ac *AlertControl) Draw(t terminalapi.Terminal, graphArea image.Rectangle, primaryLabel string) error

Draw renders the alarm checkbox and threshold dropdown within graphArea.

The primary label is used only for horizontal alignment so the control can appear just to the right of a graph's leading title.

func (*AlertControl) DrawAlert

func (ac *AlertControl) DrawAlert(t terminalapi.Terminal, pane image.Rectangle, focused bool) error

DrawAlert renders the warning banner centered inside pane when focused.

func (*AlertControl) Enabled

func (ac *AlertControl) Enabled() bool

Enabled reports whether the alarm checkbox is currently checked.

func (*AlertControl) HandleMouse

func (ac *AlertControl) HandleMouse(pos image.Point, graphArea image.Rectangle, primaryLabel string) bool

HandleMouse routes an absolute mouse click into the alert control.

func (*AlertControl) SetEnabled

func (ac *AlertControl) SetEnabled(enabled bool)

SetEnabled replaces the checkbox state programmatically.

func (*AlertControl) SetThreshold

func (ac *AlertControl) SetThreshold(value int)

SetThreshold snaps value to the nearest configured threshold and updates the dropdown selection to match.

func (*AlertControl) Threshold

func (ac *AlertControl) Threshold() int

Threshold returns the currently selected threshold value.

func (*AlertControl) UpdateSamples

func (ac *AlertControl) UpdateSamples(values []int)

UpdateSamples refreshes the alarm state against the latest values.

type Mode

type Mode int

Mode controls whether the widget draws two mirrored channels or a single half-duplex channel.

const (
	// ModeStereo draws a mirrored primary and secondary channel.
	ModeStereo Mode = iota
	// ModeHalfDuplex draws only the primary channel against a single axis.
	ModeHalfDuplex
)

type Option

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

Option is used to provide options.

func AlertColor

func AlertColor(c cell.Color) Option

AlertColor sets the color used for rendered values that meet or exceed the configured threshold.

func AxisCellOpts

func AxisCellOpts(cOpts ...cell.Option) Option

AxisCellOpts sets the cell options used for the axis line.

func ChannelLabels

func ChannelLabels(primary, secondary string) Option

ChannelLabels sets the labels shown around the axis for the primary and secondary channels.

func Gradient

func Gradient(colors ...cell.Color) Option

Gradient sets the colors used for low-to-high amplitudes.

func HalfDuplex

func HalfDuplex() Option

HalfDuplex configures the widget to draw only the primary channel.

func HalfDuplexRune

func HalfDuplexRune(r rune) Option

HalfDuplexRune sets the rune used to draw the single-channel half-duplex columns or rows.

func Height

func Height(h int) Option

Height sets a fixed height for the Spectrum. If not set or set to zero, the widget uses all available vertical space.

func Horizontal

func Horizontal() Option

Horizontal configures the widget to draw mirrored horizontal rows.

func HorizontalRunes

func HorizontalRunes(runes ...rune) Option

HorizontalRunes sets the runes used to fill horizontally oriented channels. The runes are ordered from low amplitude to high amplitude.

func LabelCellOpts

func LabelCellOpts(cOpts ...cell.Option) Option

LabelCellOpts sets the cell options used for the axis labels.

func MaxValue

func MaxValue(v int) Option

MaxValue fixes the amplitude scale. Zero keeps the scale adaptive.

func PeakRunes

func PeakRunes(primary, secondary rune) Option

PeakRunes sets the peak runes for the primary and secondary channels.

func PrimaryRunes

func PrimaryRunes(runes ...rune) Option

PrimaryRunes sets the runes used to fill the primary channel body. The runes are ordered from low amplitude to high amplitude.

func SecondaryRunes

func SecondaryRunes(runes ...rune) Option

SecondaryRunes sets the runes used to fill the secondary channel body. The runes are ordered from low amplitude to high amplitude.

func Stereo

func Stereo() Option

Stereo configures the widget to draw both channels around the axis.

func Threshold

func Threshold(v int) Option

Threshold sets the alarm threshold value.

Values at or above the threshold are highlighted with AlertColor, and a threshold indicator line is drawn with ThresholdLineColor. Zero disables the threshold overlay.

func ThresholdLineColor

func ThresholdLineColor(c cell.Color) Option

ThresholdLineColor sets the color used to draw the threshold indicator line.

func Vertical

func Vertical() Option

Vertical configures the widget to draw mirrored vertical columns.

type Orientation

type Orientation int

Orientation controls whether the spectrum draws as vertical columns or horizontal rows.

const (
	// OrientationVertical draws mirrored columns around a horizontal axis.
	OrientationVertical Orientation = iota
	// OrientationHorizontal draws mirrored rows around a vertical axis.
	OrientationHorizontal
)

type Sample

type Sample struct {
	// X is the one-based visible sample index.
	X int
	// Y is the sample value at X.
	Y int
}

Sample identifies a visible data point under a rendered spectrum cell.

type Spectrum

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

Spectrum draws mirrored or half-duplex activity bars for fast-moving audio or telemetry signals.

Implements widgetapi.Widget. This object is thread-safe.

func New

func New(opts ...Option) (*Spectrum, error)

New returns a new Spectrum widget.

func (*Spectrum) Configure

func (s *Spectrum) Configure(opts ...Option) error

Configure updates rendering options while preserving the current samples.

func (*Spectrum) Draw

func (s *Spectrum) Draw(cvs *canvas.Canvas, meta *widgetapi.Meta) error

Draw draws the Spectrum widget onto the canvas. Implements widgetapi.Widget.Draw.

func (*Spectrum) Keyboard

func (*Spectrum) Keyboard(k *terminalapi.Keyboard, meta *widgetapi.EventMeta) error

Keyboard input isn't supported on the Spectrum widget.

func (*Spectrum) Mouse

func (*Spectrum) Mouse(m *terminalapi.Mouse, meta *widgetapi.EventMeta) error

Mouse input isn't supported on the Spectrum widget.

func (*Spectrum) Options

func (s *Spectrum) Options() widgetapi.Options

Options implements widgetapi.Widget.Options.

func (*Spectrum) SetHalfDuplex

func (s *Spectrum) SetHalfDuplex(primary []int) error

SetHalfDuplex replaces the primary channel and switches the widget to half-duplex mode.

func (*Spectrum) SetStereo

func (s *Spectrum) SetStereo(primary, secondary []int) error

SetStereo replaces the mirrored primary and secondary channels.

func (*Spectrum) Update

func (s *Spectrum) Update(primary, secondary []int) error

Update replaces channel samples using automatic mode selection. Passing a nil secondary slice selects half-duplex mode.

func (*Spectrum) ValueAt

func (s *Spectrum) ValueAt(size, pos image.Point) (Sample, bool)

ValueAt returns the visible sample under pos for a canvas of size.

The provided position must be relative to the widget canvas, not the terminal. The returned X value is one-based and follows the samples currently visible after fitting to the rendered span.

func (*Spectrum) ValueCapacity

func (s *Spectrum) ValueCapacity() int

ValueCapacity returns the number of visible values the widget could display along its active sampling axis on the last draw.

type Synth

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

Synth generates smoothed synthetic activity suitable for driving spectrum widgets in demos and quick prototypes.

The output slices are reused between calls to Step. Treat them as read-only and copy if they need to be retained.

func NewSynth

func NewSynth(stereoBins, halfDuplexBins, max int) *Synth

NewSynth allocates a reusable synthetic feed generator.

func (*Synth) Step

func (s *Synth) Step() (left, right, half []int)

Step advances internal phase and returns the current stereo and half-duplex samples.

Directories

Path Synopsis
Binary spectrumdemo shows the functionality of a spectrum widget.
Binary spectrumdemo shows the functionality of a spectrum widget.

Jump to

Keyboard shortcuts

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