scrollback

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package scrollback provides a scrollback browser that parses terminal history into structured command-output blocks.

Index

Constants

This section is empty.

Variables

View Source
var DebugLogFunc func(format string, args ...any)

DebugLogFunc can be set to capture parser diagnostic output.

Functions

This section is empty.

Types

type Browser

type Browser struct {
	Blocks        []CommandBlock
	FilteredIdx   []int // indices into Blocks matching search
	SelectedIdx   int   // cursor in filtered list
	ScrollOffset  int   // left pane scroll
	PreviewScroll int   // right pane scroll

	Mode         BrowserMode
	SearchQuery  string
	SearchActive bool
	MultiSelect  map[int]bool // selected block indices (for multi-yank)

	// Diagnostic info (displayed in header)
	ParseMethod string // "osc133" or "regex"
	MarkerCount int    // number of OSC 133 markers found

	// Extracted content for JSON/Paths modes
	JSONBlocks    []JSONBlock
	PathBlocks    []PathBlock
	FilteredJSON  []int
	FilteredPaths []int

	// Output pane navigation (vim mode)
	OutputMode     bool      // focus in output pane
	Vim            *VimState // vim navigation state (non-nil when OutputMode is true)
	LastPaneHeight int       // set by renderer, used for page movement

	// Help overlay
	ShowHelp bool // show keybinding help modal

	// Layout geometry populated each render for mouse input mapping.
	LayoutLeftW  int // left pane width
	LayoutRightW int // right pane width
	LayoutPaneH  int // pane height (rows)

	// Mouse drag state for visual selection.
	DragActive  bool // mouse button held in right pane
	DragOriginY int  // line index at drag start
	DragOriginX int  // column at drag start
}

Browser holds the state for the scrollback browser overlay.

func NewBrowser

func NewBrowser(blocks []CommandBlock) *Browser

NewBrowser creates a browser from parsed command blocks.

func (*Browser) CycleMode

func (b *Browser) CycleMode()

CycleMode cycles through browser modes.

func (*Browser) EnterOutputMode

func (b *Browser) EnterOutputMode()

EnterOutputMode enters the output pane with vim-style navigation.

func (*Browser) ExitOutputMode

func (b *Browser) ExitOutputMode()

ExitOutputMode returns focus to the command list.

func (*Browser) Next

func (b *Browser) Next()

Next moves the cursor down.

func (*Browser) PageDown

func (b *Browser) PageDown(pageSize int)

PageDown moves the cursor down by a page.

func (*Browser) PageUp

func (b *Browser) PageUp(pageSize int)

PageUp moves the cursor up by a page.

func (*Browser) Prev

func (b *Browser) Prev()

Prev moves the cursor up.

func (*Browser) ScrollPreviewDown

func (b *Browser) ScrollPreviewDown()

ScrollPreviewDown scrolls the preview pane down.

func (*Browser) ScrollPreviewUp

func (b *Browser) ScrollPreviewUp()

ScrollPreviewUp scrolls the preview pane up.

func (*Browser) SelectedCommand

func (b *Browser) SelectedCommand() *CommandBlock

SelectedCommand returns the currently selected command block, or nil.

func (*Browser) SelectedCommandText

func (b *Browser) SelectedCommandText() string

SelectedCommandText returns the command string for paste-back.

func (*Browser) SelectedJSON

func (b *Browser) SelectedJSON() *JSONBlock

SelectedJSON returns the currently selected JSON block, or nil.

func (*Browser) SelectedPath

func (b *Browser) SelectedPath() *PathBlock

SelectedPath returns the currently selected path block, or nil.

func (*Browser) SelectedStyledText

func (b *Browser) SelectedStyledText() string

SelectedStyledText returns the ANSI-styled output for the current selection.

func (*Browser) SelectedText

func (b *Browser) SelectedText() string

SelectedText returns the copyable text for the current selection.

func (*Browser) SetMode

func (b *Browser) SetMode(mode BrowserMode)

SetMode switches to a specific mode.

func (*Browser) SetSearch

func (b *Browser) SetSearch(query string)

SetSearch updates the search query and rebuilds the filtered list.

func (*Browser) ToggleSelect

func (b *Browser) ToggleSelect()

ToggleSelect toggles multi-selection on the current item.

type BrowserMode

type BrowserMode int

BrowserMode selects what the browser displays.

const (
	// ModeCommands shows command/output blocks.
	ModeCommands BrowserMode = iota
	// ModeJSON shows extracted JSON fragments.
	ModeJSON
	// ModePaths shows extracted file paths and URLs.
	ModePaths
)

type CommandBlock

type CommandBlock struct {
	Command      string // command text (what the user typed)
	Output       string // plain text output
	StyledOutput string // ANSI-colored output (preserves terminal styling)
	ExitCode     int    // -1 if unknown
	StartLine    int    // absolute line index
	EndLine      int    // absolute line index (inclusive)
	Method       string // "osc133" or "regex" — how this block was parsed
}

CommandBlock represents a single command and its output extracted from scrollback.

func ParseBlocks

func ParseBlocks(term *vt.Emulator) []CommandBlock

ParseBlocks extracts command blocks from the terminal's scrollback and screen. It uses OSC 133 markers when available, falling back to regex prompt detection.

type JSONBlock

type JSONBlock struct {
	Name   string // description (e.g., "object" or "array")
	Pretty string // pretty-printed JSON
	Raw    string // raw JSON string
}

JSONBlock represents a JSON object or array found in command output.

func ExtractJSON

func ExtractJSON(output string) []JSONBlock

ExtractJSON finds valid JSON objects and arrays in the output text using bracket matching.

type PathBlock

type PathBlock struct {
	Raw   string
	Path  string
	Line  int // extracted line number, 0 if none
	Col   int // extracted column number, 0 if none
	IsURL bool
}

PathBlock represents a file path or URL found in command output.

func ExtractPaths

func ExtractPaths(output string) []PathBlock

ExtractPaths finds file paths and URLs in the output text.

type VimMode

type VimMode int

VimMode represents the current editing mode.

const (
	VimNormal     VimMode = iota
	VimVisualChar         // character-wise visual
	VimVisualLine         // line-wise visual
	VimSearch             // typing search query
)

type VimSearchMatch

type VimSearchMatch struct {
	Line   int
	StartX int // rune index (inclusive)
	EndX   int // rune index (exclusive)
}

VimSearchMatch represents a search match position within a line.

type VimState

type VimState struct {
	// Cursor position (rune indices)
	CursorX int
	CursorY int

	// Viewport
	ScrollY    int // first visible line index
	ViewHeight int // visible rows (set by renderer)

	// Mode
	Mode VimMode

	// Visual selection anchor
	VisualStartX int
	VisualStartY int

	// Search
	SearchQuery   string
	SearchMatches []VimSearchMatch
	SearchCurrent int

	// Count prefix
	PendingCount int

	// Character search (f/F/t/T)
	PendingCharSearch  bool
	LastCharSearch     rune
	LastCharSearchDir  int  // 1=forward, -1=backward
	LastCharSearchTill bool // true for t/T

	// gg detection
	PendingG  bool
	LastGTime time.Time

	// Text data
	Lines       []string // plain text (used for cursor/search logic)
	StyledLines []string // ANSI-styled text (used for rendering with colors)
}

VimState holds cursor, selection, and search state for vim-style navigation over plain text lines. Used by the scrollback browser output mode.

func NewVimState

func NewVimState(lines, styledLines []string, startY int) *VimState

NewVimState creates a VimState for the given text lines, with cursor at startY. styledLines may be nil — if provided, they are used for colored rendering.

func (*VimState) CenterView

func (v *VimState) CenterView()

CenterView centers the viewport on the cursor.

func (*VimState) ConsumeCount

func (v *VimState) ConsumeCount() int

ConsumeCount returns the pending count (minimum 1) and resets it.

func (*VimState) EnsureVisible

func (v *VimState) EnsureVisible()

EnsureVisible adjusts ScrollY to keep cursor in viewport.

func (*VimState) EnterVisualChar

func (v *VimState) EnterVisualChar()

EnterVisualChar enters character-wise visual mode.

func (*VimState) EnterVisualLine

func (v *VimState) EnterVisualLine()

EnterVisualLine enters line-wise visual mode.

func (*VimState) ExitVisual

func (v *VimState) ExitVisual()

ExitVisual returns to normal mode.

func (*VimState) FindChar

func (v *VimState) FindChar(ch rune, dir int, till bool) bool

FindChar searches for a character, crossing line boundaries. dir: 1=forward, -1=backward. till: true=stop before char.

func (*VimState) HalfPageDown

func (v *VimState) HalfPageDown()

func (*VimState) HalfPageUp

func (v *VimState) HalfPageUp()

func (*VimState) MatchBracket

func (v *VimState) MatchBracket()

func (*VimState) MoveDown

func (v *VimState) MoveDown()

func (*VimState) MoveLeft

func (v *VimState) MoveLeft()

func (*VimState) MoveRight

func (v *VimState) MoveRight()

func (*VimState) MoveToBottom

func (v *VimState) MoveToBottom()

func (*VimState) MoveToFirstNonBlank

func (v *VimState) MoveToFirstNonBlank()

func (*VimState) MoveToLine

func (v *VimState) MoveToLine(n int)

func (*VimState) MoveToLineEnd

func (v *VimState) MoveToLineEnd()

func (*VimState) MoveToLineStart

func (v *VimState) MoveToLineStart()

func (*VimState) MoveToScreenBottom

func (v *VimState) MoveToScreenBottom()

func (*VimState) MoveToScreenMiddle

func (v *VimState) MoveToScreenMiddle()

func (*VimState) MoveToScreenTop

func (v *VimState) MoveToScreenTop()

func (*VimState) MoveToTop

func (v *VimState) MoveToTop()

func (*VimState) MoveUp

func (v *VimState) MoveUp()

func (*VimState) PageDown

func (v *VimState) PageDown()

func (*VimState) PageUp

func (v *VimState) PageUp()

func (*VimState) ParagraphDown

func (v *VimState) ParagraphDown()

func (*VimState) ParagraphUp

func (v *VimState) ParagraphUp()

func (*VimState) RepeatCharSearch

func (v *VimState) RepeatCharSearch(reverse bool) bool

RepeatCharSearch repeats the last f/F/t/T search. reverse flips direction.

func (*VimState) SearchExecute

func (v *VimState) SearchExecute()

SearchExecute runs the search query and jumps to the first match at or after cursor.

func (*VimState) SearchMatchesOnLine

func (v *VimState) SearchMatchesOnLine(y int) []VimSearchMatch

SearchMatchesOnLine returns all search matches on the given line.

func (*VimState) SearchNext

func (v *VimState) SearchNext()

SearchNext jumps to the next search match.

func (*VimState) SearchPrev

func (v *VimState) SearchPrev()

SearchPrev jumps to the previous search match.

func (*VimState) SelectedText

func (v *VimState) SelectedText() string

SelectedText returns the text of the current selection (visual) or current line (normal).

func (*VimState) VisualBounds

func (v *VimState) VisualBounds() (int, int, int, int)

VisualBounds returns the normalized selection bounds for rendering. Returns (startX, startY, endX, endY) where start <= end.

func (*VimState) WORDBackward

func (v *VimState) WORDBackward()

func (*VimState) WORDEnd

func (v *VimState) WORDEnd()

func (*VimState) WORDForward

func (v *VimState) WORDForward()

func (*VimState) WordBackward

func (v *VimState) WordBackward()

func (*VimState) WordEnd

func (v *VimState) WordEnd()

func (*VimState) WordForward

func (v *VimState) WordForward()

Jump to

Keyboard shortcuts

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