pet

package
v2.0.5 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2026 License: MIT Imports: 28 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 pet reads and draws petdex pets: small animated companions kept as a folder holding a pet.json and a sheet of frames, one row of cells for each state a pet is shown in, as petdex.dev publishes them and as /hatch makes them

Index

Constants

View Source
const (
	FrameW = 192
	FrameH = 208
)

Pixels of one cell of a sheet

View Source
const (
	MinScale     = 0.1
	MaxScale     = 3.0
	DefaultScale = 0.33
)

How big a pet is drawn, as a share of the pixels of its sheet: the least, the most and the size it has unless a user sets another

View Source
const (
	Directions = 16
	Rest       = 8
	Front      = Directions
)

Ways a pet of the second layout looks: 0 up, then on clockwise in steps of 22.5 degrees (4 right, 8 down, 12 left), the frames of rows 9 and 10 of its sheet in that order. The way its head turns back to, down toward the user, before it faces front. And the frame of Look that faces front, the neutral frame in row 0, column 6 of the sheet, which a sheet may leave blank

View Source
const Beat = 1600 * time.Millisecond

How long a pet shows how something that just happened went, such as a reply that ended, before it goes back to what it does

View Source
const Frame = 160 * time.Millisecond

Time one frame of an animation is shown before the next

View Source
const Generator = "generator"

What createdBy says of a pet that /hatch made

View Source
const Index = "https://petdex.dev/api/manifest"

Address of the index of the pets petdex.dev publishes, read whole with one request

View Source
const ManifestName = "pet.json"

File of a pet's folder that says what the pet is

View Source
const (
	MinCols = 16
)

Columns a pet drawn in text blocks takes at least, and those its full size takes: one for every eight pixels of a cell

View Source
const VersionField = "spriteVersionNumber"

Field of pet.json that says which layout of petdex the sheet is of, 2 for the second, whose pet looks around

Variables

View Source
var (
	Rows      = []string{"idle", "running-right", "running-left", "waving", "jumping", "failed", "waiting", "running", "review"}
	LookRows  = []string{"look-1-8", "look-9-16"}
	EightRows = []string{"idle", "wave", "run", "failed", "review", "jump", "extra1", "extra2"}
)

Rows of a sheet of nine rows, the first layout of petdex. The two rows a sheet of eleven adds, its second layout (spriteVersionNumber 2), which hold the sixteen ways the pet looks, Directions in their order, with the neutral frame facing front in row 0, column 6; and the rows of a sheet of eight, the layout of older pets

View Source
var ErrNoPet = errors.New("no such pet")

No pet of the name is in the folder

Every state a pet is shown in, in the order a preview goes through them

Functions

func Blank

func Blank(img *image.NRGBA) bool

Whether every pixel of a picture is empty

func Clamp

func Clamp(scale float64) float64

The size kept between the least and the most, the default for no size at all

func Cols

func Cols(scale float64, set int) int

Columns a pet drawn in text blocks takes: those set when any are, otherwise those the size gives, never fewer than the least

func Direction

func Direction(dx, dy float64) int

Direction of a vector of the screen in pixels, x to the right and y down, among the ways a pet looks

func Fit

func Fit(frames []*image.NRGBA, box image.Rectangle, w, h int) []*image.NRGBA

Fit returns each frame cut to the box and scaled into w by h pixels with its proportions kept and set in the middle, each a new picture. Every frame in a sheet is cut to the same box, so the pet never slides or swells from one frame or state to the next. A pet made larger keeps the sharp pixels of its sheet, and one made smaller is smoothed.

func Fond

func Fond(text string) bool

Whether a message a user wrote is fond of the agent, which makes a pet jump for joy

func Hosted

func Hosted(address, index string) bool

Whether the address is of the host the index is on or one below it, the only hosts a pet of the index is fetched from

func Layout

func Layout(rows int) []string

Layout of a sheet of so many rows: the eleven of the second layout of petdex for eleven or more, its nine for nine or ten, the older eight otherwise

func NRGBA

func NRGBA(src image.Image) *image.NRGBA

Copy of a picture as straight alpha pixels whose first is at 0,0

func Opaque

func Opaque(img *image.NRGBA) image.Rectangle

Smallest box holding every pixel of the picture that is not empty, empty for a blank one

func Row

func Row(state State, rows int) int

Row of a sheet of so many rows that holds the state, the idle row when the sheet has none for it

func Scaled

func Scaled(box image.Rectangle, scale float64) (w, h int)

Scaled returns the pixels across and down that a pet takes at the scale when its frames fill the box, at least one each way.

func SheetExt

func SheetExt(address string) string

Extension the sheet at the address is kept under: .png for an address whose path ends so, .gif for one that ends so, otherwise .webp

func Slug

func Slug(title string) string

Slug returns a folder name for a pet with the title: lower case ASCII letters and digits, every run of anything else a dash, or "pet" for a title without any. It keeps at most 200 characters, within the file system's 255-byte name limit with room for a number that tells two pets with one title apart.

func TitleOf

func TitleOf(words string) string

TitleOf returns a short title for a pet from its description: the first three words, each capitalized, with words dropped from the end past titleMost characters. It returns "" for an empty description.

func Turn

func Turn(from, to int) int

Direction a step from one toward another, the shorter way round, the same once there

func Unique

func Unique(name string, taken func(name string) bool) string

The name, or the first of it with -2, -3 and on after it, that taken says is free

func Valid

func Valid(name string) bool

Whether the text can name a pet's folder, so it never reaches outside the folder of pets

func Zipped

func Zipped(data []byte) bool

Whether the bytes open as a zip, which a pet handed as a file may be

Types

type Broken

type Broken struct {
	Name, Dir, Why string
}

A pet folder of a folder that holds no usable pet, and why

type Entry

type Entry struct {
	// Name of the pet's folder once installed, and what a user calls it
	Name, Title string
	// Kind of thing it is, pet for most, and who sent it in
	Kind, By string
	// Addresses of its sheet, of its pet.json and of a zip of its whole folder. The sheet's is always there
	Sheet, Manifest, Zip string
}

One pet of the index

func ParseIndex

func ParseIndex(data []byte) ([]Entry, error)

Pets of an index: {"pets": [{"slug", "displayName", "kind", "submittedBy", "spritesheetUrl", "petJsonUrl", "zipUrl"}]}, other fields read past, those with no slug or no sheet left out, and stand-ins that show the sheet of another pet. An error for text that holds no list of pets

func (Entry) Curated

func (e Entry) Curated() bool

Whether petdex picked the pet itself, which its sheet's address says by a folder named curated

type Folder

type Folder string

A folder holding pets, each in a folder of its own named by the pet

func (Folder) Add

func (f Folder) Add(name string, manifest Manifest, sheet []byte, ext string) (Pet, error)

Puts a pet into the folder under the name, in place of one of that name: its sheet as a file of the extension given and its pet.json naming that file. Written beside the folder first and moved in at once, so a pet is never seen half written

func (Folder) Export

func (f Folder) Export(name string) ([]byte, error)

The pet of the name as a zip holding its folder, as petdex hands pets out: every file of the folder but those whose name starts with a dot, under a folder of the pet's name

func (Folder) List

func (f Folder) List() ([]Pet, []Broken, error)

The pets of the folder by name, and the pet folders in it that hold none that can be used. No pets for a folder that is not there

func (Folder) Load

func (f Folder) Load(name string) (Pet, error)

The pet of the name, ErrNoPet when the folder holds no pet folder of that name. A pet whose pet.json does not read or whose sheet is missing is an error that says so

func (Folder) Move

func (f Folder) Move(name string, into Folder, to string) error

Moves the pet of the name into another folder of pets under a new name, its pet.json saying the new one

func (Folder) Remove

func (f Folder) Remove(name string) (bool, error)

Removes the pet of the name, false when the folder holds none of it

func (Folder) Retitle

func (f Folder) Retitle(name, title string) error

Gives the pet of the name another title, which its pet.json keeps

type Manifest

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

What pet.json says of a pet, every field it holds kept in the order it was written

func NewManifest

func NewManifest(name, title, about, sheet string) Manifest

Manifest of a new pet: its name, its title, what it is and its sheet's file

func ReadManifest

func ReadManifest(data []byte) (Manifest, error)

Manifest the text of a pet.json holds, a byte order mark before it allowed

func Unpack

func Unpack(data []byte) (Manifest, []byte, string, error)

Unpack returns the pet in a zip, in the format petdex shares pets and Export writes them: its pet.json, its sheet and the sheet file's extension. The sheet is the file pet.json gives, otherwise a file with one of the usual names, otherwise the zip's only picture. Files are read from the folder that contains pet.json, or from the top.

func (Manifest) CreatedBy

func (m Manifest) CreatedBy() string

func (Manifest) Description

func (m Manifest) Description() string

func (Manifest) DisplayName

func (m Manifest) DisplayName() string

func (Manifest) ID

func (m Manifest) ID() string

func (Manifest) JSON

func (m Manifest) JSON() ([]byte, error)

Text of pet.json, indented by two and every character past ASCII escaped, as petdex writes it

func (Manifest) Named

func (m Manifest) Named(name, title, sheet, maker string) Manifest

Copy of the manifest naming the pet, its title, its sheet's file and who made it, whatever it said of them before. A field given empty is left as it was

func (Manifest) Sheet

func (m Manifest) Sheet() string

func (Manifest) With

func (m Manifest) With(key string, value any) Manifest

Copy of the manifest with the field set, its place kept when it was there and last otherwise

type Pet

type Pet struct {
	// Name of its folder, which names it everywhere
	Name string
	// What a user calls it, its name when pet.json gives none, and what it is
	Title, About string
	Dir          string
	// File of its sheet
	Sheet    string
	Manifest Manifest
}

One pet of a folder

func (Pet) Hatched

func (p Pet) Hatched() bool

Whether /hatch made the pet

type Sheet

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

A sheet of a pet decoded: its cells of FrameW by FrameH pixels, one row for each state

func Decode

func Decode(data []byte) (*Sheet, error)

Sheet of the bytes of a WebP, PNG, GIF or JPEG file: an error for bytes that are no picture, or a picture smaller than one cell or larger than a sheet of a pet is

func (*Sheet) Bounds

func (s *Sheet) Bounds() image.Rectangle

Smallest box holding every pixel that is not empty of every frame of every state the sheet draws, in the pixels of one cell. The whole cell for a sheet with none

func (*Sheet) CanLook

func (s *Sheet) CanLook() bool

Whether the pet can look around: its sheet holds the sixteen ways it looks

func (*Sheet) Check

func (s *Sheet) Check() error

Problem of a sheet a pet can't be drawn from: one whose idle row is blank. nil for a sheet that draws

func (*Sheet) Counts

func (s *Sheet) Counts() map[string]int

Frames of every row of the sheet, by its name

func (*Sheet) Frames

func (s *Sheet) Frames(state State) []*image.NRGBA

Frames the state is drawn with: those of its row, the first of idle alone for asleep, the ways it looks for Look. None for a sheet whose row is blank

func (*Sheet) Layout

func (s *Sheet) Layout() []string

Names of the rows of the sheet, as many as it has, in its layout

func (*Sheet) Looks

func (s *Sheet) Looks() []*image.NRGBA

Looks returns the Look frames: one for each of the pet's sixteen look directions, in direction order, then the neutral front-facing frame when the sheet has that frame. It returns nil when the sheet uses the first layout or has a blank cell among the sixteen.

func (*Sheet) Size

func (s *Sheet) Size() (cols, rows int)

Cells across and rows of the sheet

type Signals

type Signals struct {
	// A reply is under way, it waits on the user for an approval or an answer, it runs a tool, it thinks before it writes
	Busy, Waiting, Tool, Thinking bool
	// When a reply last ended well, when one failed, when the user last wrote something fond to it and when anything last happened in
	// it. Zero for never
	Ended, Failed, Cheered, Last time.Time
	// The reply that ended last left every task of its list done
	Finished bool
}

What moves a pet: what the session it watches is doing, and when things last happened in it

type State

type State string

What a pet is shown doing

const (
	Idle    State = "idle"
	Wave    State = "wave"
	Run     State = "run"
	Failed  State = "failed"
	Review  State = "review"
	Jump    State = "jump"
	Waiting State = "waiting"
	// Nothing happened for a while: the first frame of idle, held still
	Asleep State = "asleep"
	// The head turned toward something, one frame for each way it looks, the frame facing front last: what a pet of the second layout of
	// petdex does at rest while something is worth a look
	Look State = "look"
)

func Mood

func Mood(s Signals, now time.Time, sleep time.Duration) State

State the signals show at the moment, the most telling first: a failure, joy, a reply that ended well, a wait on the user, a tool at work, thinking, any work. Asleep once nothing happened for as long as sleep, never for no sleep. Idle otherwise

Directories

Path Synopsis
Package hatch makes a pet from a description: an image model draws first looks, then draws the picked look again as a strip of poses for each state.
Package hatch makes a pet from a description: an image model draws first looks, then draws the picked look again as a strip of poses for each state.

Jump to

Keyboard shortcuts

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