textselect

package
v1.11.0 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2026 License: MIT Imports: 8 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 textselect is Sidecar's one text-selection engine: the click/drag gesture state machine, the coordinate mapping under it, the highlight the selection is drawn with, and the copy that ends it.

It knows nothing about what it is selecting. A surface implements Source — where its content is drawn, what its rows say, how far it is scrolled — and hands its pointer and key events to a Surface, which answers with a Result naming what the host must do about them. Everything else is shared, so a new selectable pane is an adapter rather than another implementation.

The embedded terminal needs richer inputs than Source carries — absolute buffer coordinates, a clipped pane's column offset, lazily loaded scrollback — so it drives the lower level directly: Geometry, Pointer and the mapping functions are exported for it. Surface is the same engine composed for the simpler case of a list of rendered rows.

Index

Constants

View Source
const (
	AutoScrollInterval = 70 * time.Millisecond
	AutoScrollStep     = 5
	// A release lost off-window is only noticed when the pointer comes back, so
	// an unbounded chain would keep dragging the selection through all of
	// scrollback (and onto the clipboard under copy-on-select) in the meantime.
	// Roughly 1.5s of holding still without any fresh motion pauses the chain;
	// real motion re-arms it, so a user genuinely parked at the edge only has to
	// twitch to keep going.
	AutoScrollMaxRun = 20
)

A held pointer past an edge keeps scrolling on a timer, the way every real terminal does; motion events alone stop arriving the moment the hand stops.

View Source
const CopyNoticeDuration = 2 * time.Second

CopyNoticeDuration is how long a copy notice stays up: long enough to read a line count, short enough not to sit over the content it describes.

View Source
const DefaultTabWidth = 8

DefaultTabWidth is the tab stop every selectable surface expands against.

View Source
const DragScrollStep = 3

DragScrollStep bounds how far one motion event past an edge walks the window through scrollback. Unbounded, a pointer flicked to the top of the screen would skip hundreds of lines the user never saw.

View Source
const SuperCopyKey = "super+c"

SuperCopyKey is the platform copy chord — Cmd+C on macOS, Super+C elsewhere. It copies alongside the configured key rather than replacing it, because it is a platform convention rather than a sidecar binding: the surface owns the selection, so the emulator's own copy has nothing to act on and passes the chord through to us. Terminals that intercept it first (iTerm2) never deliver it, which is why the configurable chord stays.

Variables

This section is empty.

Functions

func AbsoluteLine

func AbsoluteLine(buf Buffer, lineIdx int) int

AbsoluteLine lifts a window-relative line index into the buffer's absolute coordinates when it keeps any.

func AutoScrollHoldExpired

func AutoScrollHoldExpired(ticks int) bool

AutoScrollHoldExpired reports when a run of ticks with no fresh drag motion behind it has gone on long enough to be treated as a lost release rather than a pointer deliberately parked past the edge.

func BufferAbsolute

func BufferAbsolute(buf Buffer) bool

BufferAbsolute reports that a buffer numbers its lines in absolute pane coordinates, which stay put as the pane produces more output.

func BufferBase

func BufferBase(buf Buffer) (base, total int)

BufferBase is the coordinate space a buffer keeps: the absolute line its first loaded row sits at, and how many lines the space runs to.

It is the one answer a host needs to draw what a gesture recorded. CellAt records selection points through AbsoluteLine, so a viewport told nothing about the base draws every highlight short by exactly it — off screen entirely once a pane has any scrollback. A relative buffer's own indices are already its coordinate space, so its base is zero.

func ColAt

func ColAt(g Geometry, buf Buffer, x, lineIdx int) (int, bool)

ColAt maps a screen X coordinate to a visual column in the given line. The returned column is in visual space (post-tab-expansion, accounting for multi-width chars). It reports false only for positions left of the content; a line that cannot be read still yields column zero, because a pointer there is over the surface.

func EdgeScrollDelta

func EdgeScrollDelta(g Geometry, y, limit int) int

EdgeScrollDelta reports how far one step should move the window for a pointer held at y, and zero once the pointer is back inside the content.

func EdgeScrollRows

func EdgeScrollRows(outputRow, rows, limit int) int

EdgeScrollRows turns a pointer row that has run past the content into the rows to scroll, negative above the top. Speed scales with the overshoot so a pointer parked just past the edge crawls while one thrown to the window edge moves quickly, bounded so no single step skips a screenful.

func LineIndexAt

func LineIndexAt(g Geometry, buf Buffer, y int) (int, bool)

LineIndexAt maps a screen row to the absolute buffer line drawn there, or false when the row is outside the output area.

func LineTextAt

func LineTextAt(buf Buffer, lineIdx int) (string, bool)

LineTextAt reads one line of a buffer, in whichever coordinate space that buffer keeps.

func OutputRowAt

func OutputRowAt(g Geometry, y int) int

OutputRowAt converts a screen row to a 0-indexed output row of the surface. The result is deliberately unbounded: a click needs to reject rows outside the output area, while a drag clamps them.

func PressLeavesTerminal

func PressLeavesTerminal(region string, terminalRegions ...string) bool

PressLeavesTerminal reports that a press landed away from every region that draws a terminal, which ends both the pointer gesture such a surface armed and the interactive mode it was armed in.

Which regions draw a terminal is the host's to name; the verdict is not. Ending only the gesture leaves a live pane holding the keyboard behind whatever the press started — a divider drag, a row selection — and ending only the mode fires the armed click under a selection the user has moved away from.

func PressesTerminal

func PressesTerminal(action mouse.ActionType) bool

PressesTerminal reports the actions that put a button down, which is where a press away from a terminal is decided.

func ScrollKeepsSelection

func ScrollKeepsSelection(buf Buffer) bool

ScrollKeepsSelection reports whether a selection survives a scroll made outside a pointer gesture — a wheel notch, a shift-scrollback key.

A selection in absolute coordinates names the same rows wherever the window moves, so scrolling away from it and back must leave it where the user made it. A buffer without them renumbers its lines as it grows, so the same anchor would come to cover rows the user never picked.

func SelectAllSpan

func SelectAllSpan(buf Buffer, tabWidth int) (ui.SelectionPoint, ui.SelectionPoint, bool)

SelectAllSpan returns the span covering every line a buffer holds, in whichever coordinate space it keeps.

func SelectedLines

func SelectedLines(buf Buffer, sel *ui.SelectionState, tabWidth int) []string

SelectedLines is the text a selection covers, clipped to the lines the buffer still holds.

func SelectionText

func SelectionText(lines []string) string

SelectionText is what selected lines read as off the screen: the styling they were drawn with removed, joined the way they were stacked. It is exactly what lands on the clipboard.

Trailing whitespace does not survive the trip. Every surface that draws into a pane pads its rows out to the pane's width — a glamour-rendered document most visibly — so a multi-row copy that kept the padding would paste ragged lines the user never saw a single column of. Leading and interior spacing is another matter entirely: it is the indentation and alignment of the text itself, and is kept exactly as drawn.

func UnitSpanAt

func UnitSpanAt(unit SelectionUnit, line string, lineIdx, col, tabWidth int) (ui.SelectionPoint, ui.SelectionPoint, bool)

UnitSpanAt returns the inclusive visual span of the unit covering col in line. Character gestures have no span to snap to.

func WordSpanAt

func WordSpanAt(line string, col int) (int, int, bool)

WordSpanAt returns the inclusive visual column span of the word covering col in a tab-expanded line. A run of word runes selects whole; anything else selects the single cell under the pointer, matching xterm.

Types

type AutoScrollTarget

type AutoScrollTarget struct {
	Geometry  func() Geometry
	Buffer    func() Buffer
	Selection *ui.SelectionState

	// Scroll moves the window by delta rendered rows, positive downwards, and
	// reports whether it actually moved. A window already against the end of the
	// buffer ends the chain instead of ticking forever.
	Scroll func(delta int) bool
}

AutoScrollTarget is the surface a held-pointer edge scroll walks. The host supplies the window as callbacks because the scroll moves it: everything after the step has to be read again.

type Buffer

type Buffer interface {
	// LineCount is how many lines the buffer holds.
	LineCount() int
	// LinesRange returns the lines in [start, end) of the buffer's own indices.
	LinesRange(start, end int) []string
	// LinesAbsoluteRange returns the lines in [start, end) of absolute
	// coordinates, and nothing for a buffer that keeps none.
	LinesAbsoluteRange(start, end int) []string
	// AbsoluteRange is the half-open absolute range the buffer covers. ok is
	// false for a buffer whose indices are its own coordinate space.
	AbsoluteRange() (start, end int, ok bool)
}

Buffer is the lines a gesture reads, in whichever coordinate space their owner keeps. A terminal's captured output numbers its lines absolutely so they stay put as the pane produces more; a plain list of rendered rows is its own coordinate space and says so by reporting no absolute range.

Every method must tolerate a nil receiver: hosts hand their buffer over whether or not they currently have one, and a typed nil arriving through this interface is not the nil the callers below test for.

type Cell

type Cell struct {
	Line int
	Col  int
}

Cell is a position in a buffer: a line index in whichever coordinate space that buffer keeps, and a visual column after tab expansion.

func CellAt

func CellAt(g Geometry, buf Buffer, x, y int) (Cell, bool)

CellAt maps screen coordinates to a buffer cell, refusing positions outside the output area: a click on the header or on padding is not a click on text.

func ClampedCellAt

func ClampedCellAt(g Geometry, buf Buffer, x, y int) (Cell, bool)

ClampedCellAt maps a pointer position onto the nearest visible cell instead of refusing positions outside the output area. A held pointer routinely leaves the pane mid-gesture — below the last row, left of the content, out over the sidebar — and a selection that stops tracking there reads as broken.

type ClickIntent

type ClickIntent struct {
	// Live reports that the surface is already forwarding keys to its pane.
	Live bool

	// MouseReporting reports that the application running in the pane has asked
	// for mouse events.
	MouseReporting bool

	// Modified is a shift- or alt-click, which is a selection gesture on every
	// terminal surface and never an activation.
	Modified bool

	// LinkClaimed reports that something with a stronger claim on the click — a
	// validated link — has already taken it.
	LinkClaimed bool
}

ClickIntent is everything a host knows about a mouse-down over a terminal surface at the moment it has to say what a release without motion will mean. It is deliberately not "where the keyboard is": focus follows the press rather than gating it, or a click from elsewhere into a passive terminal would need a second click to do what one click does anywhere else in the surface.

type ClickResolution

type ClickResolution uint8

ClickResolution is what a mouse-down over a terminal surface will mean if the gesture ends without motion. The two outcomes are mutually exclusive for one gesture — "activate and forward" is not a state — and every abort path (lost release, double-click, a click elsewhere) drops whichever one is pending.

const (
	ClickNone ClickResolution = iota
	// ClickActivate makes a passive terminal live.
	ClickActivate
	// ClickForward hands the click to the app running in a live terminal that
	// has asked for mouse reports.
	ClickForward
)

func ResolveClick

func ResolveClick(in ClickIntent) ClickResolution

ResolveClick decides what a release without motion means: make a passive terminal live, hand the click to the application running in a live one, or nothing at all.

type CopyNotice

type CopyNotice struct {
	Message  string
	IsError  bool
	Duration time.Duration
}

CopyNotice is what the user is told about a copy. Every selectable surface says the same three things — a copy needs a selection, a failure names its error, a success counts its lines — so the wording lives here and each host only wraps it in whatever its own notification type is.

type CopyResult

type CopyResult struct {
	Lines int
	// NativeErr is the system clipboard write's failure. It does not mean the
	// copy failed: the OSC 52 write to the terminal still happened, and over
	// SSH it is the half that was ever going to work — so the notice names
	// where the text was sent rather than claiming a failure.
	NativeErr error
	// Empty reports a copy asked for with nothing selected. A copy chord with no
	// selection must not replace the clipboard with a screen dump — cmd+c is
	// reflex, and the clipboard may hold something the user still needs.
	Empty bool
}

CopyResult is the outcome of a copy: how many lines reached the clipboard, and why none did. Hosts phrase their own notification from it.

type Geometry

type Geometry struct {
	Content   mouse.Rect
	Start     int
	End       int
	ColOffset int
	TabWidth  int
}

Geometry places a surface's drawn content on the screen, which is everything hit testing needs to know about it. Content is the absolute rect of the content area, so the first text cell sits at its origin — a host with chrome (a border, a header) subtracts that before building one. Start and End are the buffer lines drawn in it, and ColOffset is the content column drawn at Content.X when content wider than the viewport is clipped.

func (Geometry) Rows

func (g Geometry) Rows() int

Rows is the number of buffer lines the surface currently draws.

func (Geometry) Valid

func (g Geometry) Valid() bool

Valid reports whether the surface has a buffer window to map onto. Whether the surface is on screen at all is the host's gate: it owns the rect.

type Keys

type Keys struct {
	// Copy puts the current selection on the clipboard (default "alt+c").
	Copy string
	// SelectAll selects every line the surface is showing (default "ctrl+a").
	SelectAll string
}

Keys are the chords a selectable surface answers. They are one value because the empty-copy notice names the select-all key: a surface that carried the two separately could tell the user to press a key it does not bind.

func (Keys) CopySelectionCmd

func (k Keys) CopySelectionCmd(lines []string, wrap func(CopyNotice) tea.Msg) tea.Cmd

CopySelectionCmd copies selected lines to every clipboard within reach — the system clipboard natively, the terminal's over OSC 52 — and reports the outcome as the host's own notification. wrap is the only part of a copy a surface owns: which toast type carries the notice. A host that phrased the notice itself would be a second wording of the same outcomes.

func (Keys) IsCopyChord

func (k Keys) IsCopyChord(msg tea.KeyPressMsg) bool

IsCopyChord reports whether a key press asks to copy the selection: the configured copy key, or the platform copy chord.

func (Keys) IsSelectAllChord

func (k Keys) IsSelectAllChord(msg tea.KeyPressMsg) bool

IsSelectAllChord reports whether a key press asks to select every line.

func (Keys) Notice

func (k Keys) Notice(r CopyResult) CopyNotice

Notice phrases a copy result. The empty-selection half names the select-all key only when the surface binds one — a surface that answers just the platform copy chord would otherwise point at a key that does nothing — and the wording is a pane's rather than a terminal's, because every selectable surface shows it.

func (Keys) ResolveSurfaceChord

func (k Keys) ResolveSurfaceChord(msg tea.KeyPressMsg, chords SurfaceChords) (tea.Cmd, bool)

ResolveSurfaceChord answers a key on behalf of the surface, in one order everywhere: the two chords that act on the selection first, then scrollback. The order is stated rather than incidental — the sets do not overlap today, and a host that reordered them would be deciding that for every host.

type Pointer

type Pointer struct {
	// Resolution is what a release without motion will do.
	Resolution ClickResolution
	// contains filtered or unexported fields
}

Pointer is the click/drag state machine over a terminal surface: what a release without motion will mean, the granularity a drag extends by, and the held-pointer edge scroll. It owns no selection — the surface it edits is passed in — and no transport, so a headless caller can drive a whole gesture.

func (*Pointer) Abandon

func (pt *Pointer) Abandon()

Abandon ends a gesture whose release will never arrive — the pointer left the window, focus changed, a modal swallowed the button-up. Neither activation nor a forwarded click survives a release the app never saw.

func (*Pointer) AdoptSurface

func (pt *Pointer) AdoptSurface(sel *ui.SelectionState, r mouse.Rect)

AdoptSurface names the surface a gesture belongs to, for the pointer and for the selection it edits at once. Both must describe the same rect: the pointer's is what a drag whose press landed off the text anchors against, the selection's is what the host hit-tests with, and a caller that wrote only one of them left the two able to disagree.

func (*Pointer) AdvanceAutoScroll

func (pt *Pointer) AdvanceAutoScroll(generation uint64, target AutoScrollTarget) bool

AdvanceAutoScroll runs one step of the held-pointer edge scroll and reports whether another is owed. It stops when the tick belongs to an ended gesture, when there is no selection to extend, when the run is long enough to be a lost release, when the pointer is back inside the content, or when the window has no more rows in that direction.

func (*Pointer) AnchorFrom

func (pt *Pointer) AnchorFrom(g Geometry, buf Buffer, sel *ui.SelectionState, originX, originY int, rectangular bool) bool

AnchorFrom starts a selection for a drag whose mouse-down landed off the text — on the header, on the padding below the last row, in the border. The gesture is unambiguously a selection by the time it is moving, so anchor it at the nearest cell to where the button actually went down rather than letting the whole drag do nothing.

func (*Pointer) Arm

func (pt *Pointer) Arm(resolution ClickResolution, x, y int)

Arm records what a release without motion will do, and where the button went down — a forwarded click is reported at the press position, never the release.

func (*Pointer) Begin

func (pt *Pointer) Begin()

Begin closes whatever gesture was running: any auto-scroll tick still in flight belongs to the old generation and is dropped.

func (*Pointer) BeginAutoScrollTick

func (pt *Pointer) BeginAutoScrollTick(generation uint64) bool

BeginAutoScrollTick clears the in-flight tick and reports whether it still belongs to the live gesture.

func (*Pointer) ConsumeAutoScrollTick

func (pt *Pointer) ConsumeAutoScrollTick() bool

ConsumeAutoScrollTick counts one tick without fresh motion behind it, reporting false once the run is long enough to be a lost release.

func (*Pointer) DragPoint

func (pt *Pointer) DragPoint() (int, int)

DragPoint is the last pointer position of the live drag, which the edge auto-scroll tick re-reads.

func (*Pointer) DragTo

func (pt *Pointer) DragTo(g Geometry, buf Buffer, sel *ui.SelectionState, x, y int) bool

DragTo moves the live selection to the cell nearest the pointer, snapped to the gesture's unit.

func (*Pointer) ExtendMappedUnit added in v1.2.0

func (pt *Pointer) ExtendMappedUnit(sel *ui.SelectionState, start, end ui.SelectionPoint) bool

ExtendMappedUnit extends a word or line gesture whose coordinates were mapped by the host. This is the same granularity rule as ExtendToUnit, but it lets a rendered surface keep its selection in source coordinates rather than pretending wrapped visual rows are logical source lines.

func (*Pointer) ExtendTo

func (pt *Pointer) ExtendTo(g Geometry, buf Buffer, sel *ui.SelectionState, cell Cell)

ExtendTo grows an existing selection to a cell, snapped to the gesture's unit when one is in flight.

func (*Pointer) ExtendToUnit

func (pt *Pointer) ExtendToUnit(g Geometry, buf Buffer, sel *ui.SelectionState, cell Cell) bool

ExtendToUnit extends a word or line gesture to the unit under the pointer. The anchor unit stays whole in either direction: dragging backwards pins the far edge of the anchor, which is what makes word-drag feel like a terminal rather than a character drag that happens to start on a word.

func (*Pointer) Generation

func (pt *Pointer) Generation() uint64

Generation identifies the live gesture, so a timer scheduled by an older one can be discarded.

func (*Pointer) NoteDragMotion

func (pt *Pointer) NoteDragMotion(x, y int)

NoteDragMotion records fresh pointer motion, which restarts the hold budget that bounds an auto-scroll chain running on a lost release.

func (*Pointer) Press

func (pt *Pointer) Press(g Geometry, buf Buffer, sel *ui.SelectionState, ev PressEvent)

Press arms a gesture at the pointer. Selection only activates on actual drag motion, so a press over text prepares a drag rather than starting a selection.

func (*Pointer) PressPoint

func (pt *Pointer) PressPoint() (int, int)

PressPoint is where the button went down.

func (*Pointer) Rect

func (pt *Pointer) Rect() mouse.Rect

Rect is the surface the live gesture belongs to.

func (*Pointer) Release

func (pt *Pointer) Release(sel *ui.SelectionState) (ClickResolution, bool)

Release ends the gesture and reports what it meant: the armed resolution when nothing was selected, and whether a selection survives.

A character gesture that never left its anchor cell is a click that jittered, not a selection. Without this, a twitch during a click leaves a one-cell selection, silently copies it under copy-on-select, and swallows the activation the user was asking for. A word gesture on a one-character word legitimately ends here, so it is exempt.

func (*Pointer) ResetUnit

func (pt *Pointer) ResetUnit()

ResetUnit drops the anchor unit. A word span left over from an old double-click would otherwise redefine where the next shift-click extends from.

func (*Pointer) ScheduleAutoScroll

func (pt *Pointer) ScheduleAutoScroll(tick func(generation uint64) tea.Msg) tea.Cmd

ScheduleAutoScroll queues the next step of the held-pointer scroll, at most one tick in flight at a time. The tick message is the host's, built with the generation that pins it to this gesture.

func (*Pointer) SelectMappedUnit added in v1.2.0

func (pt *Pointer) SelectMappedUnit(sel *ui.SelectionState, start, end ui.SelectionPoint, unit SelectionUnit) bool

SelectMappedUnit installs a host-mapped word or line span and records it as the gesture anchor. Hosts use this when their drawn rows map back to a different coordinate space, such as soft-wrapped source text.

func (*Pointer) SelectUnitAt

func (pt *Pointer) SelectUnitAt(g Geometry, buf Buffer, sel *ui.SelectionState, x, y int, unit SelectionUnit) bool

SelectUnitAt installs the word or line under the pointer and records it as the gesture's anchor unit, so a button still held extends by that unit and never eats into the unit the gesture started on. The mouse-down that opened the gesture asked for the terminal, or for the app's click; a double or triple click withdraws that, or the release would fire it under the selection the user just made.

func (*Pointer) Unit

func (pt *Pointer) Unit() SelectionUnit

Unit is the granularity the live gesture extends by.

type PointerIntent

type PointerIntent uint8

PointerIntent is what a pointer action over a terminal means, independent of what any one host does about it.

const (
	// PointerIgnore is an action the terminal has no answer for.
	PointerIgnore PointerIntent = iota
	// PointerPress arms a gesture at the pressed cell, which the release
	// resolves into an activation, a forwarded click, or nothing.
	PointerPress
	// PointerSelectWord and PointerSelectLine are the double and triple click.
	PointerSelectWord
	PointerSelectLine
	// PointerWheel is a notch over the surface.
	PointerWheel
	// PointerDrag extends the selection the press anchored.
	PointerDrag
	// PointerFinish is the release that resolves the gesture.
	PointerFinish
	// PointerAbandon is the release the surface never saw — the pointer left the
	// window, or focus changed — which ends the gesture where it stands.
	PointerAbandon
)

func PointerIntentFor

func PointerIntentFor(in PointerIntentInput) PointerIntent

PointerIntentFor maps a pointer action over a terminal onto the gesture it means. The bodies are each host's — one arms a tmux click, another moves a browser's window — but which of them an action asks for is one rule, or the two surfaces answer the same gesture differently.

type PointerIntentInput

type PointerIntentInput struct {
	Action mouse.ActionType

	// OverTerminal reports that the action landed on a region drawing a
	// terminal.
	OverTerminal bool

	// FromTerminal reports that the gesture in flight began on one. A drag and
	// its release are answered by where they started, never by where the pointer
	// has since travelled — a selection dragged off the pane is still that
	// pane's selection.
	FromTerminal bool

	// LostRelease reports a button-less motion that ended a drag no release was
	// ever seen for.
	LostRelease bool
}

PointerIntentInput is what a host knows about one pointer action: what the action was, whether it landed on a terminal, and whether the gesture in flight started on one.

type PressEvent

type PressEvent struct {
	X, Y       int
	Shift, Alt bool
	Rect       mouse.Rect
	Want       ClickResolution
	SameSource bool
}

PressEvent is a mouse-down over a terminal surface.

SameSource reports whether the press landed on the surface the current selection was made in; a host that draws only one terminal always sets it. Want is what a release without motion should do, which only the host can decide: it knows whether the terminal is already live and whether something with a stronger claim on the click (a link) has already refused it.

type Result

type Result struct {
	// Handled reports that the surface answered the event, so the host must not
	// also act on it.
	Handled bool

	// Changed reports that the selection moved, so the host re-renders.
	Changed bool

	// Copy is the text a copy asks to put on the clipboard, and CopyAsked that
	// one was asked for at all. A copy chord with nothing selected asks and
	// supplies nothing, which is the case [Keys.Notice] speaks to, so a host
	// passes Copy to [Keys.CopySelectionCmd] whenever CopyAsked is set.
	Copy      []string
	CopyAsked bool

	// ClickThrough reports a gesture that resolved to a click rather than a
	// selection: the host runs whatever that click normally does. A drag never
	// sets it, which is the whole of "click activates, drag selects".
	ClickThrough bool

	// AutoScroll is how far the host should scroll its own offset, in rows,
	// positive downwards — a drag has run past an edge and is asking for more
	// content to select. Only the host can move its own window, so the rows the
	// scroll reveals are selected by the next motion event rather than by this
	// one.
	AutoScroll int
}

Result is what a host must act on after handing an event to a Surface.

type SelectionUnit

type SelectionUnit uint8

SelectionUnit is the granularity a gesture extends by. Double- and triple-clicks keep dragging in whole words and whole lines, as xterm and iTerm2 do, until the next plain click.

const (
	SelectUnitChar SelectionUnit = iota
	SelectUnitWord
	SelectUnitLine
)

type Source

type Source interface {
	// ContentRect is the screen-space rect of the selectable content, inside
	// whatever chrome or gutter the surface draws, in the coordinate space its
	// mouse events arrive in.
	ContentRect() mouse.Rect
	// Line is visual row i as it was laid out — post-wrap, so the engine never
	// sees wrapping — and LineCount how many such rows there are. Selection
	// coordinates are visual rows: what the user sees is what they copy.
	Line(i int) string
	LineCount() int
	// Scroll is the index of the first visible row.
	Scroll() int
	// TabWidth is the tab stop the rows are expanded against. 0 means the rows
	// hold no tabs.
	TabWidth() int
}

Source is the surface-specific half of a selection, and the whole of what a binding has to write.

type Surface

type Surface struct {
	// Keys are the chords this surface answers. The zero value answers only the
	// platform copy chord.
	Keys Keys

	// CopyOnSelect copies a finished selection without a copy chord, the way
	// xterm does. It is off by default: copying unasked is the single
	// most-complained-about behaviour in the editors that ship it.
	CopyOnSelect bool
	// contains filtered or unexported fields
}

Surface is one selectable region: the selection it holds and the gesture editing it. A host keeps one per pane and hands it events; the pane's own Source is passed in rather than stored, because the rows and the rect are the host's to answer freshly on every frame.

func (*Surface) Abandon

func (s *Surface) Abandon() Result

Abandon ends a gesture whose release never arrived — the pointer left the window, a modal opened, focus moved. A host detects it the way the terminal hosts do, by noticing that its mouse handler has stopped dragging without having reported a mouse.ActionDragEnd, and says so here: a surface left holding a live gesture answers the next unrelated drag anywhere on screen as an extension of a selection the user finished long ago.

func (*Surface) Clear

func (s *Surface) Clear()

Clear drops the selection and ends any gesture editing it.

func (*Surface) DecorateRow

func (s *Surface) DecorateRow(row string, visualRow int) string

DecorateRow paints the selection onto one drawn row, identified by its visual row index — the same index Source.Line answers to, not its position on screen. It is called at slice time, on the row about to be drawn, never into anything the surface caches: the highlight belongs to this frame only.

The row must already be tab-expanded, as the surface's own layout leaves it, or the columns the selection names will not be the columns it is drawn at.

func (*Surface) HandleKey

func (s *Surface) HandleKey(msg tea.KeyMsg, src Source) Result

HandleKey answers the chords that act on the selection: copy, and select-all. A release is not one of them.

func (*Surface) HandleMouse

func (s *Surface) HandleMouse(action mouse.MouseAction, src Source) Result

HandleMouse advances the gesture over src and reports what the host owes.

The host is expected to have started a drag on the press (mouse.Handler's StartDrag), because that is what turns the release into a mouse.ActionDragEnd this can resolve; without it a press arms a gesture no release ever ends. A release that is lost rather than late is Surface.Abandon's; nothing in a mouse action says one happened.

func (*Surface) HasSelection

func (s *Surface) HasSelection() bool

HasSelection reports whether anything is selected.

func (*Surface) SelectedText

func (s *Surface) SelectedText(src Source) []string

SelectedText is the selection as the user sees it: the rows it covers, stripped of the styling they were drawn with, ready for the clipboard.

func (*Surface) Selection

func (s *Surface) Selection() *ui.SelectionState

Selection is the state the host renders from, for a surface that needs more than DecorateRow — a scrollbar marker, a status line, its own extraction.

type SurfaceChords

type SurfaceChords struct {
	// Copy puts the current selection on the clipboard.
	Copy func() tea.Cmd
	// SelectAll selects every line the surface is showing.
	SelectAll func() tea.Cmd
	// Scrollback walks the window through history. It answers for itself
	// because which keys move a window is the shared scrollback rule's, and a
	// surface with no window scrolled back declines them.
	Scrollback func(tea.KeyPressMsg) (tea.Cmd, bool)
}

SurfaceChords are the acts that belong to the surface drawn around selectable content rather than to the content itself. A host registers them as one value so the set and the order are the component's, not each host's: two surfaces that each decided where copy sits relative to scrollback would answer the same key differently.

A host with chords genuinely its own — a search over its own buffer, a panel its layout owns — answers those before calling here. Nothing in this set may be one of them: they act on the content, which every host has.

Jump to

Keyboard shortcuts

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