terminallink

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2026 License: MIT Imports: 14 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 terminallink detects URL, file, issue, and git-spec spans in terminal text.

Detection lives here so the project Workspaces plugin and the global Workspaces preview share one matcher. Hosts activate the kinds they understand and ignore the rest. Adding a kind therefore does not require editing two hosts.

KindIssue is emitted for td-<hex> ids so a later terminal | file + td split can bind a live td pane. It must not open internal/app/issue_preview.go. KindDiff is emitted for existence-gated lowercase hex revs, "commit <rev>", and A..B / A...B ranges. HEAD and branch names are not scanned.

Index

Constants

View Source
const (
	// MaxResourceLocatorChars bounds one matched locator in runes. A locator
	// reaches persisted state and becomes an argv element of a provider call.
	MaxResourceLocatorChars = 200
	// MaxResourceMatchesPerLine bounds how many resource spans one line may
	// contribute, across every matcher. It is the scanner's protection against
	// a configured pattern that matches everywhere.
	MaxResourceMatchesPerLine = 32
)

Bounds on what an external matcher may produce. They are stated here rather than imported because internal/resource depends on this package for OSC stripping, so the dependency cannot run the other way. A test in internal/resource asserts the two copies agree.

View Source
const MaxNewDiffResolves = 16

MaxNewDiffResolves is the host budget for new git existence checks per (surface, buffer revision). Further unique spec tokens stay plain text until the next revision.

Variables

This section is empty.

Functions

func Activatable added in v1.0.3

func Activatable(k Kind) bool

Activatable reports whether a kind is one a host can act on, and so one that may be underlined. Both terminal surfaces asked this question with their own copy of the same switch; a new kind must not be able to reach one surface's decoration and miss the other's hit testing.

func Decorate

func Decorate(line string, spans []Span) string

Decorate underlines file, issue, git-spec, and external resource spans and synthesizes OSC-8 for validated URLs. A host passes only the kinds it binds — an underline no click can honor is a dead link, and which kinds a host binds is the host's own answer. Callers must StripOSC8 first.

A resource span is underlined like any other local activation. It never becomes a hyperlink: only a validated http(s) URL earns OSC-8, and a locator is not a URL.

func IssueID added in v0.99.0

func IssueID(value string) bool

IssueID reports whether value is a td id of the shape this package detects. A host that restores an id from disk asks here rather than trusting the file: the click path can only ever produce this shape, and the id becomes an argv element of the fetch.

func Markdown

func Markdown(path string) bool

Markdown reports whether path should open as rendered markdown.

func OpenHTTP

func OpenHTTP(raw string) tea.Cmd

OpenHTTP opens a validated http(s) URL in the system browser.

func OpenRegular

func OpenRegular(absolute string) (*os.File, error)

OpenRegular opens the EvalSymlinks result of absolute when it is a regular file. Callers own the returned handle.

func ResolveCommit added in v1.0.0

func ResolveCommit(workdir, rev string) (oid string, ok bool)

ResolveCommit reports whether rev names a commit in workdir.

func ResolveFile

func ResolveFile(base, raw string) (display, absolute string, ok bool)

ResolveFile maps a printed path onto a regular file on this machine.

It tries the selected root first, then an absolute or ~/ path. The file must exist, be regular, and survive EvalSymlinks. It does not have to sit inside the selected worktree. Display is a root-relative slash path when the file is inside base, otherwise the resolved absolute path.

func SafeHTTPURL

func SafeHTTPURL(raw string) (string, bool)

SafeHTTPURL trims trailing prose punctuation and accepts only http(s) URLs with a host and no control characters.

func StripOSC8

func StripOSC8(line string) string

StripOSC8 removes source-supplied OSC sequences so only synthesized hyperlinks remain. Fail closed if a removal could concatenate a new introducer.

func WrapVisualRange

func WrapVisualRange(line string, startCol, endCol int, open, close string) string

WrapVisualRange wraps the inclusive visual-column range in open/close.

Types

type DiffResolver added in v1.0.0

type DiffResolver func(raw string) (value string, extra Extra, ok bool)

DiffResolver reports whether a git-spec token exists in the selected checkout. Extra.Raw is the matched token; Extra.Line is unused. ok=false drops the span. A nil DiffResolver emits no git-spec spans.

type Extra

type Extra struct {
	Line     int
	Raw      string
	Provider string
	Matcher  string
}

Extra holds kind-specific fields. Line is 1-based and zero when the token has no :line suffix. Raw is the original file or git-spec token when Value is rewritten by a Resolver. Provider and Matcher are set only for KindResource and, with the span's Value as the locator, form the resource reference a host activates.

func ResolveGitSpec added in v1.0.0

func ResolveGitSpec(workdir, raw string) (value string, extra Extra, ok bool)

ResolveGitSpec existence-gates a scanner token: a lowercase hex rev, "commit <rev>", or A..B / A...B. HEAD and branch names are refused here even if git would accept them — those are CLI-only.

type Kind

type Kind string

Kind classifies one detected span.

const (
	KindURL   Kind = "url"
	KindFile  Kind = "file"
	KindIssue Kind = "issue"
	KindDiff  Kind = "diff"
	// KindResource is the one generic kind every external provider shares.
	// Which provider and matcher produced it lives in Extra, so adding a
	// provider never adds a kind.
	KindResource Kind = "resource"
)

type Options added in v1.0.3

type Options struct {
	// Resolve existence-gates file tokens. Nil skips bare-file spans.
	Resolve Resolver
	// ResolveDiff existence-gates git specs. Nil emits no git-spec spans.
	ResolveDiff DiffResolver
	// Matchers are the live external matchers in precedence order. Empty
	// means no provider is ready, which must read as ordinary text.
	Matchers []ResourceMatcher
}

Options collects everything a scan may consult. The zero value scans only the kinds that need no host callback and no configuration.

type Resolver

type Resolver func(raw string) (value string, extra Extra, ok bool)

Resolver reports whether a file token exists. value is what the host should store (typically a path relative to the selected root). ok=false drops the span. A nil Resolver skips existence-gated file spans (bare paths).

type ResourceMatcher added in v1.0.3

type ResourceMatcher struct {
	// Provider is the configured provider instance ID.
	Provider string
	// ID is the provider-stable matcher ID.
	ID string
	// Re is the compiled RE2. A nil expression is skipped rather than
	// panicking: an unready provider must degrade to plain text.
	Re *regexp.Regexp
}

ResourceMatcher is one compiled provider-declared pattern the scanner may run. The whole match is the locator: there are no replacement templates and no provider code runs during a scan.

Hosts build these from an immutable resourceprovider snapshot, already in precedence order. This package deliberately knows nothing about providers, processes, or documents — only that a pattern maps a span to a reference.

type Span

type Span struct {
	Kind     Kind
	StartCol int
	EndCol   int
	Value    string
	Extra    Extra
}

Span is one non-overlapping match in visual columns of the stripped line. EndCol is inclusive.

func Scan

func Scan(line string, resolve Resolver, resolveDiff DiffResolver) []Span

Scan finds URL, file, issue, and git-spec spans in a terminal line. It is ScanWith without external matchers, which is what callers that have no provider snapshot want.

func ScanWith added in v1.0.3

func ScanWith(line string, opts Options) []Span

ScanWith finds every span kind in a terminal line.

line may still contain ANSI; it is stripped before matching. Overlaps are resolved first-kind-wins in this order: url, path:line, resolved bare files, issue, git spec, external resource. File tokens need a suffix. Bare files (and, when a Resolver is supplied, path:line) are existence-gated. Git specs are existence-gated by resolveDiff and scanned dotted → commit-word → rev.

External matchers run last so built-in precedence cannot be bid away by a provider's priority, and matching stays pure: no process starts and no I/O happens here.

Jump to

Keyboard shortcuts

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