tool

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: MIT Imports: 3 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 tool defines the contract every tool implements.

A tool is a declarative Spec plus a pure function Run(ctx, Request) → Result. The TUI and the CLI build their interface by reading the Spec; no tool writes UI code.

Contract rules:

  1. Run does no I/O, keeps no global state and honours ctx.
  2. Reversible tools use a "mode" option (Select) with the values "encode" and "decode".
  3. Input errors are returned as *InputError; any other error is considered a bug.
  4. Binary outputs are shown as Base64 or hex depending on the "output" option.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Position

func Position(s string, offset int) (line, col int)

Position converts a byte offset in s into a line and column (in runes), both starting at 1. Handy for building an InputError.

Types

type Category

type Category string
const (
	Converters Category = "Converters"
	Encoders   Category = "Encoders"
	Formatters Category = "Formatters"
	Generators Category = "Generators"
	Testers    Category = "Testers"
	Text       Category = "Text"
)

func Categories

func Categories() []Category

Categories returns the categories in display order.

type Detector

type Detector interface {
	Detect(sample string) float64 // 0..1
}

Detector is optional: it takes part in smart detection.

type DiffKind

type DiffKind int

DiffKind is the kind of a diff fragment.

const (
	Equal DiffKind = iota
	Insert
	Delete
)

type DiffOp

type DiffOp struct {
	Kind DiffKind
	Text string
}

DiffOp is a fragment of a diff.

type Exporter added in v0.2.0

type Exporter interface {
	Export(ctx context.Context, req Request) (data []byte, ext string, err error)
}

Exporter is optional: tools whose output also has a binary form (the PNG of a QR code) implement it. The TUI uses it when Ctrl+S saves to a path with that extension, and the CLI with --out. Like Run, it does no I/O and honours ctx; ext includes the dot (".png").

type InputError

type InputError struct {
	Input     string
	Line, Col int
	Msg       string
}

InputError is an input error with a position, so the TUI can underline the line and the CLI can report it. Line and Col start at 1; 0 means unknown.

func AsInputError

func AsInputError(err error) (*InputError, bool)

AsInputError extracts an *InputError from err's chain.

func (*InputError) Error

func (e *InputError) Error() string

type InputSpec

type InputSpec struct {
	Key, Label string
	Lang       string // highlighting: "json", "xml", ""…
	Multiline  bool
}

type KV

type KV struct {
	Key, Value string
}

KV is a key/value metadata pair for the statusbar.

type Option

type Option struct {
	Key, Label, Help string
	Kind             OptionKind
	Choices          []string
	Min, Max         int
	Default          any
}

type OptionKind

type OptionKind int
const (
	Toggle OptionKind = iota // bool
	Select                   // one of Choices
	Number                   // int within Min/Max
	String                   // short text (JWT secret, separator…)
)

type OutputKind

type OutputKind int
const (
	PlainText   OutputKind = iota
	Highlighted            // uses Result.Lang
	DiffView               // uses Result.Diff
	Matches                // uses Result.Spans (regex, JSONPath)
	Image                  // Unicode blocks (QR)
)

type Request

type Request struct {
	Inputs map[string]string
	Opts   Values
}

type Result

type Result struct {
	Text  string
	Lang  string
	Diff  []DiffOp
	Spans []Span // highlighted ranges in the input
	Meta  []KV   // "Size: 1.2 KB", "exp: 2026-12-01"… for the statusbar
}

type Span

type Span struct {
	Input      string // InputSpec key
	Start, End int
	Group      int // 0 = whole match, n = capture group
}

Span is a highlighted range in an input, as byte offsets [Start, End).

type Spec

type Spec struct {
	ID          string // "base64", "json-format"… (CLI subcommand name)
	Name        string
	Category    Category
	Description string
	Keywords    []string
	Inputs      []InputSpec // 0 (generators), 1 or 2 (diff, jwt)
	Options     []Option
	Output      OutputKind
	Live        bool // false = recompute only on F5 (expensive operations)
}

type Tool

type Tool interface {
	Spec() Spec
	Run(ctx context.Context, req Request) (Result, error)
}

type Values

type Values map[string]any

Values holds the option values of a Request, keyed by Option.Key. The getters return the zero value when the key is missing or the type does not match.

func (Values) Bool

func (v Values) Bool(key string) bool

func (Values) Int

func (v Values) Int(key string) int

func (Values) String

func (v Values) String(key string) string

func (Values) WithDefaults

func (v Values) WithDefaults(spec Spec) Values

WithDefaults returns a copy of v filled in with spec's Default values for the missing keys.

Jump to

Keyboard shortcuts

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