avatar

package
v0.2.0 Latest Latest
Warning

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

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

Documentation

Overview

Package avatar draws a deterministic avatar for a name: a soft body in one of ten silhouettes with two capsule eyes, in a colour the name chose. The same name always draws the same avatar, so whoever a user learns in one list is who they recognise in the next. Nothing is stored or fetched.

The avatar stands for somebody. For a catalog of ready-made animated characters, picked by index or name to show a mood or a status, use package faces.

View draws the figure in terminal cells, PNG returns it as an image for a terminal that shows images, and SVG returns it as markup. The mapping from a name to its figure and colours is frozen: the tests pin it against the reference vectors in testdata.

Hue, Tone and Silhouette pin the colour or the shape, and Pins any single trait such as the size of the eyes, for an app with a house style, while the name still decides everything else.

The figure can react. Expression sets a pose the eyes hold (SetExpression eases into it), React pulls a face for a moment and lets it go, Blink closes and reopens the eyes once and StartIdle keeps the avatar breathing, blinking and glancing aside, all three driven by Update, and LookAt turns the eyes toward a target such as the mouse pointer.

A name is put in Unicode Normalization Form C, trimmed and lowercased before it is hashed, so "Alain" and " alain " are one avatar, and so are a name written with combining marks ("e" followed by U+0301) and its precomposed spelling. Set Raw to hash the name as written.

Stability: experimental. Its API may change in any minor release.

Example

A name always draws the same avatar. Shape and Colors report what it drew; View is the picture, here its four middle rows with the colours stripped, so the body shows as ink and the eyes as holes.

package main

import (
	"fmt"
	"strings"

	"github.com/ows4444/tui/ansi"
	"github.com/ows4444/tui/avatar"
)

func main() {
	m := avatar.New("alain00")
	m.Width, m.Height = 12, 6
	body, eyes, _ := m.Colors()
	fmt.Println(m.Shape(), body, eyes)
	for _, row := range strings.Split(ansi.StripANSI(m.View()), "\n")[1:5] {
		fmt.Println(strings.TrimRight(row, " "))
	}
	fmt.Println(m.Linearize())
}
Output:
round {165 221 255} {7 17 23}
▗▟█▛▜█████▙▖
▐██▌ ██▌ ▜█▌
▐███▄▟██▄▟█▌
▝▜████████▛▘
Avatar: alain00

Index

Examples

Constants

View Source
const (
	DefaultWidth  = 8
	DefaultHeight = 4
)

DefaultWidth and DefaultHeight are the size, in cells, of a Model from New. A cell is about twice as tall as it is wide, so a width of twice the height is square on screen.

View Source
const MaxPNGSize = 512

MaxPNGSize is the largest side, in pixels, PNG draws; a larger size is drawn at this one.

View Source
const ReactHold = 1500 * time.Millisecond

ReactHold is how long a reaction lasts, from the click to the moment the avatar starts back to its own Expression. The ease into the reaction is part of it.

Variables

This section is empty.

Functions

This section is empty.

Types

type Background

type Background int

Background is the plate drawn behind the body.

const (
	BackgroundNone Background = iota
	BackgroundSquircle
	BackgroundCircle
	BackgroundSquare
)

The backgrounds. BackgroundNone, the zero value, leaves the cells around the body untouched.

type Expression

type Expression int

Expression is a pose the avatar holds until it is changed. A pose reshapes and moves the two eyes, and may tint the body; it never adds a mark, so no avatar grows a mouth. No two poses differ by tint alone, so they stay apart with colour stripped.

const (
	ExpressionNone Expression = iota
	// ExpressionHappy is the eyes closed in a smile: two domes.
	ExpressionHappy
	// ExpressionSad is flat eyes dropped low, their outer ends down.
	ExpressionSad
	// ExpressionMad is wide flat bars, their inner ends down, and the body
	// flushed toward red. Eased into, it trembles.
	ExpressionMad
	// ExpressionSurprised is the only pose that grows the eyes: wide, tall
	// and round.
	ExpressionSurprised
	// ExpressionWink is one eye shut and the other open.
	ExpressionWink
	// ExpressionSleepy is both eyes nearly shut, and low.
	ExpressionSleepy
	// ExpressionThinking is the eyes at two heights, looking aside. Eased
	// into, the two trade heights on a loop: a loader with a face.
	ExpressionThinking
	// ExpressionSmug is both eyes half shut and tilted the same way, one
	// held higher than the other.
	ExpressionSmug
	// ExpressionUnsure is one eye open and the other squeezed and tilted.
	ExpressionUnsure
	// ExpressionScared is small eyes, lifted and drawn together. Eased into,
	// it trembles.
	ExpressionScared
	// ExpressionLove is tall eyes leaning together, and the body flushed
	// rose.
	ExpressionLove
	// ExpressionShy is small eyes, low and drawn together, and a pale blush.
	ExpressionShy
	// ExpressionSick is two slumped bars at different heights, and the body
	// turned green. Eased into, it trembles faintly.
	ExpressionSick
)

The expressions. ExpressionNone, the zero value, is the figure at rest.

func (Expression) String

func (e Expression) String() string

String returns the expression's name, such as "happy"; ExpressionNone and any value that is not an expression are "none".

type Model

type Model struct {
	// Name is who the avatar stands for: a username, an email, an id. It is
	// trimmed and lowercased before hashing unless Raw is set.
	Name string
	// Width and Height are the size in terminal cells. The figure is square
	// and centred, so the shorter side (counting a row as two columns)
	// decides how large it is drawn.
	Width, Height int
	// Background is the plate behind the body.
	Background Background
	// Raw hashes Name as written. Set it for case-sensitive ids, which
	// normalization would otherwise collide.
	Raw bool
	// Hue pins the colour's hue, an angle in degrees in the OKLCh colour
	// space: about 30 is red-orange, 90 yellow, 140 green, 250 blue and 320
	// magenta. Zero lets the name choose; use 360 for the hue at 0. The name
	// still chooses the tone and the figure.
	Hue float64
	// Tone pins how light and saturated the body is. The zero value,
	// ToneAuto, lets the name choose.
	Tone Tone
	// Silhouette pins the shape. The zero value, SilhouetteAuto, lets the
	// name choose; the name still sizes and places it.
	Silhouette Silhouette
	// Pins fixes individual traits of the figure, each at a position from 0
	// to 1 across the range a name would choose from; a value outside that
	// is clamped. A trait not in the map, and every trait when it is nil,
	// comes from the name. Copies of a Model share the map: assign a new one
	// to change the pins of one copy.
	Pins map[Trait]float64
	// Expression is the pose the eyes hold. The zero value, ExpressionNone,
	// is the figure at rest. Assigning it changes the pose at once;
	// SetExpression eases into it.
	Expression Expression
	// LookX and LookY turn the eyes: each runs from -1 to 1, where LookX 1
	// is fully right and LookY 1 fully down, and zero is the name's own
	// resting gaze. LookAt sets both from where a target is.
	LookX, LookY float64
	// Motion is the reduced-motion preference. The zero value is
	// motion.Normal. With motion.Reduced, Blink, StartIdle and React play
	// nothing.
	Motion motion.Preference
	// Theme supplies the glyph set: under an ASCII one the avatar is drawn
	// with its Shades. The colours come from Name, not from the theme.
	Theme theme.Theme
	// contains filtered or unexported fields
}

Model is the avatar for Name, drawn as a Width by Height cell area.

func New

func New(name string) Model

New returns a Model for name at DefaultWidth by DefaultHeight cells, using theme.DarkTheme(). It remembers the pictures it has drawn lately, so a frame it has shown before is not drawn again.

func (m *Model) Blink() tui.Cmd

Blink closes and reopens the eyes once; return the Cmd from your Update, for example on a key press. Blinking again mid-blink restarts it: ticks left over from before are ignored. While the avatar idles, the idle loop carries on after the blink. Under motion.Reduced it returns nil and the eyes stay open.

func (Model) Blinking

func (m Model) Blinking() bool

Blinking reports whether a blink is playing.

func (Model) Colors

func (m Model) Colors() (body, eyes, background ansi.RGB)

Colors returns the colours the avatar is drawn in now: the body, the eyes, and the plate drawn when Background is set. An expression that tints, such as ExpressionMad, changes the body and may change the eyes. The eyes contrast with the body at 4.5:1 or better, whatever Hue and Tone are pinned and whatever expression shows.

func (*Model) Hover

func (m *Model) Hover(over bool) tui.Cmd

Hover runs the idle loop only while the pointer is over the avatar: pass whether it is, on every mouse move, and return the Cmd from your Update. The loop starts when the pointer arrives and stops, returning the avatar to rest, when it leaves; while the pointer stays, or stays away, nothing changes and the Cmd is nil. Under motion.Reduced it never starts.

func (Model) Idling

func (m Model) Idling() bool

Idling reports whether the idle loop is running.

func (Model) LayoutNode

func (m Model) LayoutNode() layout.Node

LayoutNode adapts the avatar to a layout.Node. Measure reports Width by Height; Render draws at exactly the allotted Size, using it in place of Width and Height. The Model is not changed.

func (Model) Linearize

func (m Model) Linearize() string

Linearize renders the avatar as plain text for accessible output (see tui.Linearizer): "Avatar: " and the name, or "Avatar" when the name is blank, followed by the expression when one is set, as in "Avatar: ada, thinking". The picture itself is decoration and is not described.

func (*Model) LookAt

func (m *Model) LookAt(dx, dy int)

LookAt turns the eyes toward a target dx columns right of and dy rows below the centre of the avatar: for the mouse pointer, the pointer's cell minus the centre cell. The further the target, the further the eyes turn, reaching a full look at one and a half avatar widths; LookAt(0, 0) returns them to rest. It sets LookX and LookY.

func (Model) PNG

func (m Model) PNG(size int) []byte

PNG returns the avatar as a square PNG image, size pixels on a side, with smooth edges and a transparent background where there is no plate. Like View it draws the avatar as it is now: its Expression, where it looks and a blink in progress. It is the picture for a terminal that shows images:

img := imageview.New(m.PNG(128), m.Width, m.Height, m.Linearize())
img.Kitty = caps.KittyGraphics

with View as the fallback elsewhere. The same Model always returns the same bytes. A Model from New keeps its recent images and returns the same slice for the same picture, so imageview, which encodes a slice once, redraws only when the avatar does: do not write to the slice. A size of zero or less returns nil.

func (*Model) React

func (m *Model) React() tui.Cmd

React makes the avatar pull a face: an expression other than the one it is showing, eased into, held until ReactHold has passed and then eased back to Expression. Return the Cmd from your Update, for example on a mouse click, and pass the Msgs that follow to Update. Reacting again during the hold picks another expression and starts the hold again.

The expression is not random: it is drawn from the name and from how many times this Model has reacted, so the same name reacts the same way every run, which keeps a recorded session reproducible, while a wall of names each reacts in its own order. A reaction that lands on a pose that moves trembles or rocks while it is held. Under motion.Reduced it returns nil and the expression does not change.

func (Model) Reacting

func (m Model) Reacting() bool

Reacting reports whether a reaction is being held.

func (Model) SVG

func (m Model) SVG() string

SVG returns the avatar as SVG markup on a 100 by 100 view box, with no width or height, so the page sizes it. Hue, Tone, Silhouette and Pins are part of the figure and are drawn. It is the figure at rest: Expression, a reaction, LookX, LookY, a blink in progress and the idle loop do not change it.

func (*Model) SetExpression

func (m *Model) SetExpression(e Expression) tui.Cmd

SetExpression changes Expression and eases the eyes into the new pose over a few frames; return the Cmd from your Update. A pose that moves (mad, scared and sick tremble, thinking rocks) then keeps moving until the expression changes again, for as long as its Msgs are passed to Update. Assigning the field changes the pose at once and draws it still, as does this under motion.Reduced, where it returns nil. While a reaction is held the change shows when the reaction is released.

func (Model) SetTheme

func (m Model) SetTheme(t theme.Theme) Model

SetTheme returns m with t applied. It makes Model a tui.ThemeSetter, so a root model can forward the Program's theme (see tui.WithTheme).

func (Model) Shape

func (m Model) Shape() string

Shape returns the name of the avatar's silhouette: round, organic, boxy, capsule, nub, cloud, droplet, hexagon, sun or triangle. It is the pinned Silhouette when one is set.

func (*Model) StartIdle

func (m *Model) StartIdle() tui.Cmd

StartIdle begins the idle loop: the avatar breathes, blinks now and then and glances aside, at a rhythm drawn from its name. Return the Cmd from your Init or Update; the loop runs until StopIdle. Restarting is safe at any moment. Under motion.Reduced it returns nil and the avatar rests.

func (*Model) StopIdle

func (m *Model) StopIdle()

StopIdle ends the idle loop and returns the avatar to rest. A tick still on its way is ignored.

func (Model) Update

func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)

Update advances a blink one frame, the idle loop one beat, or the ease between two poses one frame, per tick, and starts a reaction back when its hold is over. A blink rests when it is done unless the avatar idles. It ignores a tick or a release that another avatar, or an animation since replaced, scheduled, and every other Msg.

func (Model) View

func (m Model) View() string

View renders Height rows, each exactly Width cells wide. Each cell is a two by two block of pixels drawn with a quadrant block character, in two colours: the body is the character's ink, and the eyes and the plate are the cell's background. Without colour the body still reads as a solid silhouette with the eyes cut out of it. In a small avatar the eyes are drawn larger than their true size so they stay legible, and each takes at least one pixel except during a blink. Under an ASCII glyph set a cell is one of the theme's Shades, darker the more of it the body covers. Width <= 0 or Height <= 0 renders "".

type Silhouette

type Silhouette int

Silhouette pins the avatar's shape. SilhouetteAuto, the zero value, lets the name choose.

const (
	SilhouetteAuto Silhouette = iota
	SilhouetteRound
	SilhouetteOrganic
	SilhouetteBoxy
	SilhouetteCapsule
	SilhouetteNub
	SilhouetteCloud
	SilhouetteDroplet
	SilhouetteHexagon
	SilhouetteSun
	SilhouetteTriangle
)

The silhouettes, in the order of how often a name draws them.

func (Silhouette) String

func (s Silhouette) String() string

String returns the silhouette's name, such as "round"; SilhouetteAuto and any value that is not a silhouette are "auto".

type Tone

type Tone int

Tone pins the avatar's swatch: how light and how saturated its body is. ToneAuto, the zero value, lets the name choose.

const (
	ToneAuto Tone = iota
	TonePastel
	TonePale
	ToneMid
	ToneDeep
	ToneBright
	ToneInk
)

The tones, pale to ink.

func (Tone) String

func (t Tone) String() string

String returns the tone's name, such as "pastel"; ToneAuto and any value that is not a tone are "auto".

type Trait

type Trait int

Trait names one of the numbers a name chooses for its avatar, for Model.Pins.

const (
	// TraitBodySize is how much of the frame the body takes.
	TraitBodySize Trait = iota
	// TraitBodyProportion runs from wider than tall to taller than wide.
	TraitBodyProportion
	// TraitBodySquareness runs from an ellipse toward a rounded square.
	TraitBodySquareness
	// TraitEyeSize is the width of the eyes.
	TraitEyeSize
	// TraitEyeRoundness runs from round eyes to tall capsules.
	TraitEyeRoundness
	// TraitEyeSquareness runs from soft ends to square ones.
	TraitEyeSquareness
	// TraitEyeSeparation is how far apart the eyes are.
	TraitEyeSeparation
	// TraitEyeLean runs from leaning one way, through upright, to the other.
	TraitEyeLean
	// TraitGazeX is where the eyes rest, from left to right.
	TraitGazeX
	// TraitGazeY is where the eyes rest, from high to low.
	TraitGazeY

	// TraitTilt is how far a boxy, hexagon or triangle body is turned, from
	// one way, through level, to the other.
	TraitTilt
	// TraitSquat is how flat a capsule is.
	TraitSquat
	// TraitCornerRounding runs from sharp corners to none at all, on a
	// hexagon or a triangle.
	TraitCornerRounding
	// TraitPetals is how many petals a sun has.
	TraitPetals
	// TraitPetalDistance is how far a sun's petals sit from its centre.
	TraitPetalDistance
	// TraitPetalSize is the size of a sun's petals.
	TraitPetalSize
	// TraitPetalRotation turns a sun's ring of petals.
	TraitPetalRotation
	// TraitLobes is how many lobes a cloud has.
	TraitLobes
	// TraitNubs is how many nubs a nub body has.
	TraitNubs
	// TraitNubAngle is where the first nub sits around the body.
	TraitNubAngle
	// TraitNubSize is the size of the nubs.
	TraitNubSize
	// TraitTipLength is how tall a droplet's point is.
	TraitTipLength
)

The traits that can be pinned. Each is a position from 0 to 1 across the range the name would have chosen from, so 0 is the smallest, roundest, fewest or leftmost an avatar ever is and 1 the largest, squarest, most or rightmost.

func (Trait) String

func (t Trait) String() string

String returns the trait's name, such as "eye size"; a value that is not a trait is "unknown".

Jump to

Keyboard shortcuts

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