complete

package
v1.0.0-rc.5 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2026 License: MIT Imports: 4 Imported by: 0 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 complete is the pure completion engine behind the composer's tab-triggered lists (mentions, commands, emotes, emoji). It knows nothing about Bubble Tea, lipgloss or the theme: given a line and the cursor's byte offset it decides whether a trigger is open and which candidates to offer, and the UI does the drawing and the splicing. Modeled on internal/splash and internal/theme, which split a pure seam from the widget that draws it.

Offsets are byte offsets throughout, not rune offsets: Go's regexp package (and every Source built on it) is byte-native, and the UI is the one place that has to convert to and from the textinput widget's rune positions.

Index

Constants

View Source
const MaxCandidates = 10

MaxCandidates caps how many candidates a Result carries. The UI shows at most 5 at once and scrolls the rest into view, so 10 is room for two screenfuls without the list needing to re-query as the user scrolls past what a slower source (a future network lookup) already returned.

Variables

View Source
var CommandTable = []Command{
	{Name: "/ah", Aliases: []string{"airhorn"}, Access: AccessMod,
		Description: "makes a user's client play an airhorn"},
	{Name: "/alts", Aliases: nil, Access: AccessMod,
		Description: "checks for potential alt accounts"},
	{Name: "/away", Aliases: []string{"a"}, Access: AccessAll,
		Description: "sets yourself away"},
	{Name: "/back", Aliases: []string{"ba"}, Access: AccessAll,
		Description: "sets yourself back"},
	{Name: "/ban", Aliases: []string{"b"}, Access: AccessMod,
		Description: "bans a user"},
	{Name: "/block", Aliases: nil, Access: AccessAll,
		Description: "manage blocked users"},
	{Name: "/block/add", Aliases: nil, Access: AccessAll,
		Description: "block a user"},
	{Name: "/block/list", Aliases: nil, Access: AccessAll,
		Description: "list blocked users"},
	{Name: "/block/remove", Aliases: nil, Access: AccessAll,
		Description: "remove a blocked user"},
	{Name: "/disconnect", Aliases: []string{"dc"}, Access: AccessAdmin,
		Description: "silently disconnects a user"},
	{Name: "/dm", Aliases: nil, Access: AccessAll,
		Description: "sends a direct message to a user"},
	{Name: "/gimme", Aliases: nil, Access: AccessAll,
		Description: "sends ༆ つ ◕_◕ ༽つ with an optional message"},
	{Name: "/help", Aliases: []string{"h"}, Access: AccessAll,
		Description: "shows chat usage"},
	{Name: "/help/emoji", Aliases: nil, Access: AccessAll,
		Description: "shows emoji/emoticon usage"},
	{Name: "/ignore", Aliases: nil, Access: AccessAll,
		Description: "manage ignored users"},
	{Name: "/ignore/add", Aliases: nil, Access: AccessAll,
		Description: "ignore a user"},
	{Name: "/ignore/list", Aliases: nil, Access: AccessAll,
		Description: "list ignored users"},
	{Name: "/ignore/remove", Aliases: nil, Access: AccessAll,
		Description: "remove an ignored user"},
	{Name: "/kick", Aliases: []string{"k"}, Access: AccessMod,
		Description: "kicks a user"},
	{Name: "/lenny", Aliases: nil, Access: AccessAll,
		Description: "sends ( ͡° ͜ʖ ͡°) with an optional message"},
	{Name: "/me", Aliases: nil, Access: AccessAll,
		Description: "sends a me message"},
	{Name: "/play", Aliases: nil, Access: AccessAdmin,
		Description: "plays a sound effect for the channel"},
	{Name: "/random", Aliases: nil, Access: AccessAll,
		Description: "sends a random dank meme plus an optional message"},
	{Name: "/roll", Aliases: []string{"r"}, Access: AccessAll,
		Description: "rolls the dice (NdS)"},
	{Name: "/sfx", Aliases: nil, Access: AccessAll,
		Description: "lists sound effects or triggers one by id or alias"},
	{Name: "/shrug", Aliases: nil, Access: AccessAll,
		Description: "sends ¯\\_(ツ)_/¯ with an optional message"},
	{Name: "/slap", Aliases: []string{"s"}, Access: AccessAll,
		Description: "slaps users"},
	{Name: "/spoiler", Aliases: nil, Access: AccessAll,
		Description: "covers your message with a spoiler / NSFW warning"},
	{Name: "/table", Aliases: nil, Access: AccessAll,
		Description: "sends (╯°□°)╯︵ ┻━┻ with an optional message"},
	{Name: "/table/r", Aliases: nil, Access: AccessAll,
		Description: "sends (┬─┬ノ( º _ ºノ) with an optional message"},
	{Name: "/unban", Aliases: []string{"ub"}, Access: AccessMod,
		Description: "unbans a user"},
	{Name: "/yippee", Aliases: nil, Access: AccessAll,
		Description: "sends ᕕ( ᐛ )ᕗ with an optional message"},

	{Name: "/who", Aliases: nil, Access: AccessAll,
		Description: "lists who is in the channel (handled locally)"},
}

CommandTable is the hand-ported copy of upstream's SLASH_COMMANDS (32 rows) plus the CLI-local /who, reconciled by hand against upstream commit b54377d7 (src/client/app/chat/autocomplete/slash_commands.ts) under ADR 0002's drift contract, extended to this table by AGENTS.md's internal/complete paragraph. The table keeps upstream's own order (already alphabetical by name), with /who appended after it, so a diff against upstream's SLASH_COMMANDS array is a straight read; Candidates re-sorts its filtered result, so this declaration order is never itself relied on for display. Access is mirrored here only to hide dead ends from the list — the server is the only enforcement point, exactly as upstream's own getSlashCommands documents.

Functions

func Rank

func Rank[T any](term string, items []T, key func(T) string) []T

Rank orders items by how closely key(item) matches term, case-insensitively: a prefix match ranks above a substring match, which ranks above a subsequence match (every rune of term appears in key(item), in order, not necessarily adjacent). Items matching none of the three are dropped. Within a tier, items sort alphabetically (case-insensitive); a tie on the lowercased key (e.g. a roster carrying both "Bob" and "bob") breaks on the exact key instead of falling back to input order, so the result stays the same regardless of which order a caller's map range happened to visit them in. Items tied on the exact key too keep their input order (sort.SliceStable), which is what makes a source's own ordering (e.g. upstream's static command table) survive a genuine tie. An empty term matches everything at the prefix tier, so the result is every item, alphabetical.

This is the CLI's in-house departure from upstream's fuzzysort (ADR 0006): deterministic, dependency-free, and close enough to what a person expects when they don't know the exact name.

Types

type Access

type Access int

Access is the server's role gate on a command, mirrored here only to hide dead ends from the list (see CommandTable's doc comment for why the server, not this package, is the actual enforcement point). Values are a rank in ascending order: Candidates keeps a command when cmd.Access <= Viewer().

const (
	AccessAll   Access = iota // no role check
	AccessMod                 // admin or chat mod
	AccessAdmin               // admin only; chat mods are refused too
)

func AccessFor

func AccessFor(isAdmin, isChatMod bool) Access

AccessFor maps the wire's privilege flags onto a rank: admin wins over mod, and neither flag set is the everyone tier.

type Candidate

type Candidate struct {
	// Insert is the full replacement for the span, trailing space
	// included where the wire format wants one (e.g. "@alice ").
	Insert string
	// Label is what the row shows, e.g. "/kick (k)".
	Label string
	// Detail is a dimmed suffix, e.g. a command's description. May be
	// "", in which case the row shows no detail at all.
	Detail string
	// Dim marks a candidate that should draw faint, e.g. an away user.
	Dim bool
}

Candidate is one row a Source offers for the open trigger.

type Command

type Command struct {
	// Name is the canonical command, with the leading "/".
	Name string
	// Aliases are accepted alternate names, without the leading "/".
	Aliases []string
	// Access is the role tier the server's handler enforces.
	Access Access
	// Description is the short, human-facing summary shown dimmed in
	// the completion list, taken verbatim from upstream.
	Description string
}

Command is one row of the slash-command table.

type Commands

type Commands struct {
	// Viewer reports the caller's rank, read fresh on every query so a
	// revalidated token's flags are reflected without a hook back into
	// this package. Required: Candidates calls it unguarded, so a
	// zero-value Commands panics on first use rather than silently
	// completing nothing (the same contract Mentions's Users and Self
	// fields carry).
	Viewer func() Access
}

Commands completes slash commands for a viewer of a given rank.

func (Commands) Candidates

func (c Commands) Candidates(term string) []Candidate

Candidates filters CommandTable to the viewer's rank, then applies upstream's own matching rule (never the fuzzy Rank every other source uses, per ADR 0006): term is split on "*", each piece is regexp-escaped and rejoined with ".*", and the resulting pattern is matched, case-insensitively, as a prefix of the command's name or of "/" plus any alias. A term the trigger's character class produced cannot make regexp.Compile fail, but a nil result rather than a panic is returned if it ever does. The result is sorted by Name (plain string order, which is what upstream's localeCompare gives for these ASCII names) and is not capped here; Engine.Complete caps at MaxCandidates.

func (Commands) Match

func (c Commands) Match(line string, cursor int) (start int, term string, ok bool)

Match reports the span of an open command line, per commandTrigger. The whole line up to the cursor must be the trigger (start is always 0), which is what keeps "/x y" mid-line from opening a list once a space has been typed. Only line[:cursor] is consulted, per Source's contract: with the cursor inside a half-typed name ("/ki|ck", cursor right after "ki"), the span is [0, cursor) ("/ki") and accepting splices the canonical name in ahead of the untouched "ck" tail, the same as every other source.

func (Commands) Name

func (Commands) Name() string

Name identifies the command source for Engine's sticky dismiss.

type Emoji

type Emoji struct{}

Emoji completes emoji shortnames from emojiCatalog, the embedded list internal/complete/gen writes from the upstream checkout. Like Emotes it has no fields: the catalog is static between builds.

func (Emoji) Candidates

func (Emoji) Candidates(term string) []Candidate

Candidates ranks emojiCatalog by shortname against term, stripping a "*" first the same way Emotes does: the shared three-tier Rank already treats a gap as "anything" at its subsequence tier, so stripping the wildcard reproduces upstream's fuzzysort behavior without a wildcard implementation of its own. Insert always ends with ": " (the delimiter and the trailing space the wire format wants); Label pairs a fixed-width glyph column with the shortname so every row's name starts at the same column regardless of how wide the glyph itself renders.

func (Emoji) Match

func (Emoji) Match(line string, cursor int) (start int, term string, ok bool)

Match reports the span of an open ":shortname" token, per emojiTrigger. start is the byte offset of the ":", not the first character of term.

func (Emoji) Name

func (Emoji) Name() string

Name identifies the emoji source for Engine's sticky dismiss.

type Emotes

type Emotes struct{}

Emotes completes NG emote ("dank meme") shortcodes from emoteCodes, the embedded list internal/complete/gen writes from the upstream checkout. Unlike Mentions and Commands it has no fields: the list is static between builds, so there is nothing to read fresh per query.

func (Emotes) Candidates

func (Emotes) Candidates(term string) []Candidate

Candidates ranks emoteCodes against term with a "*" in term treated as a gap of any length: the shared three-tier Rank already lets its subsequence tier match a gap, and upstream's fuzzysort (which this engine replaces per ADR 0006) has no wildcard semantics of its own either, so stripping the "*" before ranking reproduces the same result without inventing one. A code with no match at any tier is dropped, and Engine never opens a list with none left.

func (Emotes) Match

func (Emotes) Match(line string, cursor int) (start int, term string, ok bool)

Match reports the span of an open ng…/tf… token, per emoteTrigger. term is the whole token (wildcards included); Candidates strips them before ranking.

func (Emotes) Name

func (Emotes) Name() string

Name identifies the emote source for Engine's sticky dismiss.

type Engine

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

Engine holds the one piece of state completion needs across keystrokes: the span, if any, the user closed with esc. Its zero value is ready to use.

func (*Engine) Complete

func (e *Engine) Complete(line string, cursor int, sources []Source) (Result, bool)

Complete tries sources in order and asks the first one whose trigger matches for its candidates — first Match wins, the same rule upstream's engine uses, so a future source's trigger cannot shadow an earlier one by accident. It reports ok=false when no source matches, when the winning span is the one Dismiss last closed (and the trigger has not moved since), or when the winner has zero candidates: a list never opens empty. Candidates beyond MaxCandidates are dropped.

A no-match, or a match whose {source, start} differs from the dismissed span, clears the dismissal — so a new trigger elsewhere in the line, or on a following keystroke, opens normally.

cursor is clamped to [0, len(line)] so a caller need not guard a stale cursor position itself.

func (*Engine) Dismiss

func (e *Engine) Dismiss(r Result)

Dismiss keeps r's span closed until its trigger fires with a different start (further typing inside the same span, which only moves the end, stays quiet). Typically called from esc.

type Mentions

type Mentions struct {
	// Users returns the current roster. Called on every query, so the
	// list is never stale; the UI hands in a closure over its map.
	Users func() []User
	// Self is excluded from the candidates; compared case-insensitively.
	Self func() string
}

Mentions completes @-mentions from the live roster. Unlike a source backed by static data, both fields are called fresh on every query rather than captured once, so a join, a leave or an away change between keystrokes is reflected without any change notification flowing into this package. Both fields are required: Candidates calls them unguarded, so a zero-value Mentions panics on first use rather than silently completing nothing.

func (Mentions) Candidates

func (m Mentions) Candidates(term string) []Candidate

Candidates ranks the current roster against term: an empty term yields everyone alphabetical, like /who; a non-empty one goes through the shared three-tier Rank. Self is dropped first so it never occupies a ranking slot. A name containing "!" or "*" cannot exist on the site, so a hand-typed "@!" or "@*" simply ranks against nothing and the list stays closed (Engine never opens an empty result).

func (Mentions) Match

func (m Mentions) Match(line string, cursor int) (start int, term string, ok bool)

Match reports the span of an open @-mention, per mentionTrigger. term is the partial name typed so far, empty right after a bare "@".

func (Mentions) Name

func (Mentions) Name() string

Name identifies the mention source for Engine's sticky dismiss.

type Result

type Result struct {
	// Source is the winning Source's Name.
	Source string
	// Start and End are byte offsets into the line the completion
	// replaces on accept. End is always the cursor offset passed to
	// Complete, not necessarily line's length.
	Start, End int
	// Candidates is 1..MaxCandidates, in display order. Complete never
	// returns a Result with zero candidates — see its doc comment.
	Candidates []Candidate
}

Result is one open completion: which source won, the span of the line it owns, and the candidates to show for it.

type Source

type Source interface {
	// Name is a stable id for the source, used as half of the
	// sticky-dismiss key. It must not change across calls.
	Name() string
	// Match reports the span [start, cursor) in line that this source
	// owns, or ok=false if its trigger has not fired. Only
	// line[:cursor] is consulted — text after the cursor is never
	// part of a trigger. term is what Candidates ranks against; it is
	// usually, but need not be, line[start:cursor].
	Match(line string, cursor int) (start int, term string, ok bool)
	// Candidates ranks the source's list against term, in the order
	// the list should display them. May return nil when nothing
	// matches; a nil or empty result means the list does not open.
	Candidates(term string) []Candidate
}

Source is one trigger and the candidate list it offers once its trigger has fired. A source is stateless between calls: the engine re-queries it on every keystroke rather than caching, since the backing data (the roster, the privilege flags) can change under it.

type User

type User struct {
	Name string
	Away bool
}

User is the slice of a roster row completion needs: enough to rank and to draw a row, nothing that would couple this package to the wire format.

Directories

Path Synopsis
Command gen writes internal/complete/emotes_gen.go from the upstream ngchat checkout, invoked by the //go:generate directive at the top of internal/complete/complete.go.
Command gen writes internal/complete/emotes_gen.go from the upstream ngchat checkout, invoked by the //go:generate directive at the top of internal/complete/complete.go.

Jump to

Keyboard shortcuts

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