Documentation
¶
Overview ¶
Package tarang animates text inside terminal user interfaces.
Core types are pure, deterministic, and host-agnostic: advance time via Player.Advance and render into any TUI loop with Player.View, Player.AppendANSI, or Player.Frame.
Index ¶
- type Attr
- type Cell
- type Color
- type ColorProfile
- type Effect
- type Frame
- func (f *Frame) AppendANSI(dst []byte, profile ColorProfile) []byte
- func (f *Frame) At(x, y int) Cell
- func (f *Frame) Clear(style Style)
- func (f *Frame) Height() int
- func (f *Frame) PlainString() string
- func (f *Frame) Resize(width, height int)
- func (f *Frame) Set(x, y int, c Cell)
- func (f *Frame) SetGlyph(x, y int, g Glyph, style Style)
- func (f *Frame) SetRune(x, y int, r rune, style Style)
- func (f *Frame) String(profile ColorProfile) string
- func (f *Frame) Width() int
- func (f *Frame) WriteANSI(w io.Writer, profile ColorProfile) (int, error)
- type Glyph
- type Gradient
- type Option
- func EnvOptions(lookup func(string) (string, bool)) []Option
- func FromEnv() Option
- func WithBaseStyle(s Style) Option
- func WithColorProfile(p ColorProfile) Option
- func WithMaxStep(dt time.Duration) Option
- func WithReducedMotion(on bool) Option
- func WithSeed(seed uint64) Option
- func WithSize(width, height int) Option
- type Player
- func (p *Player) Advance(dt time.Duration) bool
- func (p *Player) AppendANSI(dst []byte) []byte
- func (p *Player) Done() bool
- func (p *Player) Elapsed() time.Duration
- func (p *Player) Frame() *Frame
- func (p *Player) Pause()
- func (p *Player) Paused() bool
- func (p *Player) Reset()
- func (p *Player) Resize(width, height int)
- func (p *Player) Resume()
- func (p *Player) SetText(text string)
- func (p *Player) Size() (width, height int)
- func (p *Player) View() string
- func (p *Player) WriteANSI(w io.Writer) (int, error)
- type Style
- type Target
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Attr ¶
type Attr uint8
Attr is a bit set of text attributes emitted as SGR parameters.
const ( // AttrBold renders bold text (SGR 1). AttrBold Attr = 1 << iota // AttrDim renders faint text (SGR 2). AttrDim // AttrItalic renders italic text (SGR 3). AttrItalic // AttrUnderline renders underlined text (SGR 4). AttrUnderline // AttrReverse swaps foreground and background (SGR 7). AttrReverse )
type Cell ¶
type Cell struct {
Rune rune
Extra string
Width uint8 // 0 = wide continuation, 1 = narrow, 2 = wide
Style Style
}
Cell is one grid position in a Frame.
Width is 1 for a normal cell, 2 for the leading half of a wide glyph, and 0 for the continuation cell that a wide glyph owns at the next column.
type Color ¶
Color is an RGB color with an explicit set flag.
The zero Color is unset and renders as the terminal default. Use RGB or Hex to construct a set color.
func Hex ¶
Hex parses a color written as six hexadecimal digits with an optional leading "#". Any other length, an extra prefix, whitespace, or a non-hex digit yields the zero Color and false.
func (Color) Lerp ¶
Lerp interpolates from c to to by t, clamped to [0, 1].
An unset side is treated as opaque black. When both sides are unset the result is unset; otherwise the result is always set.
func (Color) ToANSI16 ¶
ToANSI16 returns the nearest standard 16-color palette index (0..15) by squared Euclidean distance. The lowest index wins ties.
type ColorProfile ¶
type ColorProfile uint8
ColorProfile selects how a Frame converts colors to SGR sequences.
const ( // ProfileTrueColor emits 24-bit SGR sequences (38;2;r;g;b / 48;2;r;g;b). ProfileTrueColor ColorProfile = iota // ProfileANSI256 emits 8-bit SGR sequences (38;5;n / 48;5;n). ProfileANSI256 // ProfileANSI16 emits 4-bit SGR sequences (30-37, 90-97 / 40-47, 100-107). ProfileANSI16 // ProfileNone suppresses colors while preserving text attributes. ProfileNone )
type Effect ¶
type Effect interface {
// Init prepares the effect for target with a deterministic seed.
Init(target Target, seed uint64)
// Step advances the effect by dt and draws into dst, reporting whether the
// frame changed and whether the effect has finished.
Step(dt time.Duration, dst *Frame) (changed, done bool)
// Reset rewinds the effect to its post-Init state.
Reset()
}
Effect animates a Target over time by drawing into frames.
An Effect is a single-goroutine state machine. Init prepares it for a target and seed, Step advances it by dt, and Reset rewinds it to its post-Init state without reallocating.
func Parallel ¶
Parallel returns an effect that advances effects concurrently and composites their frames in argument order.
Each child draws into a reusable scratch frame, so steady-state steps do not allocate. Cells that are empty or styled as blank are transparent, letting later children draw over earlier ones. Nil effects are ignored.
Example ¶
package main
import (
"fmt"
"time"
"github.com/vyrx-dev/tarang"
"github.com/vyrx-dev/tarang/effects"
)
// advance runs a player to completion with a fixed step.
func advance(p *tarang.Player) {
for !p.Done() {
p.Advance(100 * time.Millisecond)
}
}
func main() {
p := tarang.New("go",
tarang.Parallel(
effects.Waves(),
effects.ColorShift(),
),
tarang.WithColorProfile(tarang.ProfileNone),
tarang.WithSeed(42),
)
advance(p)
fmt.Println(p.View())
}
Output: go
func Sequence ¶
Sequence returns an effect that plays effects one after another.
Each child is initialized with the parent seed plus its index. A finished child immediately hands over to the next one, which renders its starting state in the same step. Nil effects are ignored.
Example ¶
package main
import (
"fmt"
"time"
"github.com/vyrx-dev/tarang"
"github.com/vyrx-dev/tarang/effects"
)
// advance runs a player to completion with a fixed step.
func advance(p *tarang.Player) {
for !p.Done() {
p.Advance(100 * time.Millisecond)
}
}
func main() {
p := tarang.New("go",
tarang.Sequence(
effects.Wipe(effects.WithWipeDirection(effects.LeftToRight)),
effects.Slide(effects.WithSlideDirection(effects.TopToBottom)),
),
tarang.WithColorProfile(tarang.ProfileNone),
tarang.WithSeed(42),
)
advance(p)
fmt.Println(p.View())
}
Output: go
type Frame ¶
type Frame struct {
// contains filtered or unexported fields
}
Frame is a flat, reusable 2D cell grid that effects draw into and hosts render as ANSI.
A Frame is not safe for concurrent use. Its buffer stays valid until the next mutating call.
func NewFrame ¶
NewFrame returns a frame of width by height cells, cleared to spaces.
Negative dimensions clamp to zero.
func (*Frame) AppendANSI ¶
func (f *Frame) AppendANSI(dst []byte, profile ColorProfile) []byte
AppendANSI appends the frame as ANSI text to dst and returns the result.
Each line is emitted without a trailing newline. Styles are tracked across cells so only changes are emitted, with a reset before each newline and at the end of the frame when a style is active. Under ProfileANSI256 and ProfileANSI16, colors that downgrade to the same palette index count as equal.
func (*Frame) PlainString ¶
PlainString returns the frame as uncolored text, with lines separated by a newline and no trailing newline. Wide glyphs appear once and empty cells render as a space.
func (*Frame) Resize ¶
Resize changes the frame dimensions, clearing all cells to spaces.
Negative dimensions clamp to zero. The cell buffer is reused when it has enough capacity.
func (*Frame) Set ¶
Set writes c at x, y and keeps wide glyphs whole.
Out-of-bounds writes, zero-width cells, and wide cells that do not fit on the row are no-ops. Writing over either half of an existing wide glyph clears the other half to a space.
func (*Frame) String ¶
func (f *Frame) String(profile ColorProfile) string
String returns the frame as ANSI text.
type Glyph ¶
type Glyph struct {
X, Y int
Rune rune // non-zero for single-rune grapheme clusters
Extra string // non-empty only for multi-rune grapheme clusters (Rune == 0)
Width uint8 // 1 (narrow) or 2 (wide)
Index int // 0-based reading-order index among visible glyphs
}
Glyph is one visible grapheme cluster placed on a Target grid.
Single-rune clusters carry their rune in Rune and leave Extra empty. Multi-rune clusters (combining marks, ZWJ sequences) leave Rune zero and store the whole cluster in Extra.
type Gradient ¶
type Gradient struct {
Stops []Color
}
Gradient interpolates between ordered color stops.
func NewGradient ¶
NewGradient returns a Gradient over stops, copying the slice so later mutations of stops do not affect the gradient.
type Option ¶
type Option func(*playerConfig)
Option configures a Player at construction time.
Options apply left to right, so later options override earlier ones.
func EnvOptions ¶
EnvOptions translates environment degradation hints into [Option]s.
A non-empty NO_COLOR disables color, a REDUCED_MOTION value other than "0" or "false" skips animation, and TERM=dumb disables both. The returned options apply in that order, so options appended after them take precedence.
func FromEnv ¶
func FromEnv() Option
FromEnv returns an Option that applies EnvOptions using the process environment.
Place it first so explicit options that follow still win.
Example ¶
package main
import (
"fmt"
"os"
"github.com/vyrx-dev/tarang"
"github.com/vyrx-dev/tarang/effects"
)
// setEnv applies key/value pairs and returns a restore function.
func setEnv(kv ...string) func() {
type oldEnv struct {
value string
set bool
}
old := make([]oldEnv, len(kv)/2)
for i := range old {
old[i].value, old[i].set = os.LookupEnv(kv[i*2])
_ = os.Setenv(kv[i*2], kv[i*2+1])
}
return func() {
for i, o := range old {
if o.set {
_ = os.Setenv(kv[i*2], o.value)
continue
}
_ = os.Unsetenv(kv[i*2])
}
}
}
func main() {
defer setEnv("NO_COLOR", "1", "TERM", "dumb")()
p := tarang.New("ok", effects.Typewriter(), tarang.FromEnv())
fmt.Println(p.Done())
fmt.Println(p.View())
}
Output: true ok
func WithBaseStyle ¶
WithBaseStyle sets the style used for static and reduced-motion text.
func WithColorProfile ¶
func WithColorProfile(p ColorProfile) Option
WithColorProfile sets the color profile used to render frames.
func WithMaxStep ¶
WithMaxStep clamps the largest dt that Player.Advance applies in one call.
A non-positive dt is ignored, keeping the current clamp (100ms by default).
func WithReducedMotion ¶
WithReducedMotion renders the final text immediately instead of animating.
func WithSeed ¶
WithSeed sets the deterministic seed passed to Effect.Init.
type Player ¶
type Player struct {
// contains filtered or unexported fields
}
Player drives an Effect over time and renders its frames as ANSI text.
A Player is a single-goroutine state machine with no internal timers. A finished or paused Player costs nothing per host frame: Advance returns immediately and View returns the cached string.
func New ¶
New returns a Player that animates text with fx.
A nil fx or reduced motion draws the final text immediately and starts done. Nil options are ignored.
Example ¶
package main
import (
"fmt"
"time"
"github.com/vyrx-dev/tarang"
"github.com/vyrx-dev/tarang/effects"
)
// advance runs a player to completion with a fixed step.
func advance(p *tarang.Player) {
for !p.Done() {
p.Advance(100 * time.Millisecond)
}
}
func main() {
p := tarang.New("tarang", effects.Typewriter(),
tarang.WithColorProfile(tarang.ProfileNone),
tarang.WithSeed(42),
)
advance(p)
fmt.Println(p.View())
}
Output: tarang
func (*Player) Advance ¶
Advance moves the animation forward by dt and reports whether the frame changed.
dt is clamped to the configured maximum step. Paused or finished players ignore the call and return false.
func (*Player) AppendANSI ¶
AppendANSI appends the current frame as ANSI text to dst without allocating when dst has enough capacity.
func (*Player) Frame ¶
Frame returns the player's cell grid.
The pointer is valid until the next mutating call.
func (*Player) Reset ¶
func (p *Player) Reset()
Reset rewinds the effect and clears elapsed time, keeping text, size, and the paused state.
func (*Player) Resize ¶
Resize re-lays out the text on a width by height grid.
A non-positive dimension restores natural sizing on that axis. A finished or static player redraws the final text; a running player restarts its effect at the new size. The paused state is preserved.
func (*Player) SetText ¶
SetText replaces the animated text and restarts the animation.
Size constraints and the paused state are preserved.
type Style ¶
Style is the foreground color, background color, and attribute set of a cell. The zero Style means default colors and no attributes.
type Target ¶
type Target struct {
Width, Height int
Glyphs []Glyph
Text string // original raw input passed to ParseTarget
}
Target is parsed text laid out on a fixed grid of glyphs.
Spaces, controls, and glyphs clipped by the grid bounds are not emitted, so Glyphs contains only cells an effect needs to draw.
func ParseTarget ¶
ParseTarget lays out text on a width by height grid using UAX #29 grapheme clusters and UAX #11 display widths.
Carriage returns are normalized to newlines, tabs expand to four spaces, and C0 controls and DEL are dropped. A non-positive width or height uses the text's natural bound on that axis. Text is only clipped, never wrapped.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package ease provides standard easing curves for Tarang effects.
|
Package ease provides standard easing curves for Tarang effects. |
|
Package effects provides ready-to-use text animation effects for tarang.
|
Package effects provides ready-to-use text animation effects for tarang. |
|
examples
|
|
|
standalone
command
Command standalone animates a multi-line banner in the terminal.
|
Command standalone animates a multi-line banner in the terminal. |
|
scripts
|
|
|
gencasts
command
Command gencasts renders asciinema v2 previews for every built-in effect.
|
Command gencasts renders asciinema v2 previews for every built-in effect. |