preview

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: May 27, 2026 License: MIT Imports: 28 Imported by: 0

Documentation

Overview

editor.go provides inline edit-mode logic for the preview panel. It manages cursor movement, text input, undo/redo dispatch, and the atomic save flow. Functions here are called from Preview.Update when editMode is true.

editor_selection.go provides text selection support for edit mode. It manages selection anchors, range extraction, and selected-text retrieval using buffer coordinates (not display-line coordinates).

In edit mode the existing selAnchor / selEnd fields on Preview are reused (read-mode mouse selection is inactive while editing).

Package preview implements the file preview panel for the grut TUI. It provides syntax-highlighted code display, markdown rendering, binary file detection, and a scrollable viewport.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config added in v0.2.0

type Config interface {
	GetTheme() string
	GetMaxFileSize() int
	GetSyntaxHighlighting() bool
	GetLineNumbers() bool
	GetWordWrap() bool
	GetRenderMarkdown() bool
}

Config defines the configuration subset needed by the preview panel. The concrete config.PreviewConfig satisfies this interface, but tests and embedders can supply lightweight stubs without importing the config package.

This follows the narrow-interface injection pattern described in CONTRIBUTING.md § "Config Interface Pattern".

type Preview

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

Preview is the file preview panel. It displays syntax-highlighted source code, rendered markdown, or file metadata for binary/oversized files.

func New

func New(cfg Config, editorCfg config.EditorConfig, th *theme.Theme) *Preview

New creates a new Preview panel with the given configuration.

func (*Preview) Blur

func (p *Preview) Blur()

Blur implements panels.Panel.

func (*Preview) CopySelection added in v0.1.0

func (p *Preview) CopySelection() (panels.Panel, tea.Cmd)

CopySelection implements panels.SelectionCopier.

func (*Preview) FilePath

func (p *Preview) FilePath() string

FilePath returns the path of the currently displayed file (empty if none).

func (*Preview) Focus

func (p *Preview) Focus()

Focus implements panels.Panel.

func (*Preview) HasSelection added in v0.1.0

func (p *Preview) HasSelection() bool

HasSelection implements panels.SelectionCopier.

func (*Preview) Init

func (p *Preview) Init(_ context.Context) tea.Cmd

Init implements panels.Panel.

func (*Preview) KeyBindings

func (p *Preview) KeyBindings() []panels.KeyBinding

KeyBindings implements panels.Panel.

func (*Preview) SetGitClient

func (p *Preview) SetGitClient(gc git.StatusReader)

SetGitClient configures the git client for diff-aware preview.

func (*Preview) SetSize

func (p *Preview) SetSize(width, height int)

SetSize implements panels.Panel.

func (*Preview) Title

func (p *Preview) Title() string

Title implements panels.Panel. Returns the filename when a file is loaded, the GitHub item title when in ghMode, or "preview" when nothing is selected.

func (*Preview) Update

func (p *Preview) Update(msg tea.Msg) (panels.Panel, tea.Cmd)

Update implements panels.Panel. It handles file selection messages and keyboard events for scrolling and toggling display options.

func (*Preview) View

func (p *Preview) View(width, height int) string

View implements panels.Panel. It renders the preview content into the given width×height area.

type TextBuffer added in v0.1.0

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

TextBuffer is a line-oriented editable text buffer with undo/redo support. It stores raw text lines (no ANSI) for in-place editing within the preview panel.

func NewTextBuffer added in v0.1.0

func NewTextBuffer(lines []string) *TextBuffer

NewTextBuffer creates a buffer from the given lines, deep-copying the input. An empty or nil input produces a single empty line.

func (*TextBuffer) Dedent added in v0.1.0

func (b *TextBuffer) Dedent(line int, tabSize int)

Dedent removes up to tabSize leading spaces from the line.

func (*TextBuffer) DeleteForward added in v0.1.0

func (b *TextBuffer) DeleteForward(line, col int)

DeleteForward performs the delete-key at (line, col): removes the character at the cursor position. If the cursor is at end-of-line, the next line is joined onto the current one.

func (*TextBuffer) DeleteLine added in v0.3.0

func (b *TextBuffer) DeleteLine(line int)

DeleteLine deletes the entire line at the given index. If it is the last remaining line, the line is cleared to "" instead of being removed (the buffer must always contain at least one line). Always forces an undo break.

func (*TextBuffer) DeleteRange added in v0.3.0

func (b *TextBuffer) DeleteRange(startLine, startCol, endLine, endCol int) (newLine, newCol int)

DeleteRange deletes text from (startLine, startCol) to (endLine, endCol). The range is [startCol, endCol) on the respective lines. If start == end, this is a no-op. Always forces an undo break. Returns the cursor position after deletion.

func (*TextBuffer) DeleteRune added in v0.1.0

func (b *TextBuffer) DeleteRune(line, col int) (newLine, newCol int)

DeleteRune performs backspace at (line, col): deletes the character before the cursor. If col==0 and line>0, the current line joins with the previous one. Returns the new cursor position.

func (*TextBuffer) Dirty added in v0.1.0

func (b *TextBuffer) Dirty() bool

Dirty reports whether the buffer has unsaved changes.

func (*TextBuffer) DuplicateLine added in v0.1.0

func (b *TextBuffer) DuplicateLine(line int)

DuplicateLine duplicates the line at the given index, inserting the copy below it. Always forces an undo break.

func (*TextBuffer) InsertRune added in v0.1.0

func (b *TextBuffer) InsertRune(line, col int, r rune)

InsertRune inserts a rune at (line, col). Out-of-bounds values are clamped. Undo snapshots are coalesced for rapid consecutive inserts.

func (*TextBuffer) InsertTab added in v0.1.0

func (b *TextBuffer) InsertTab(line, col int, tabSize int) int

InsertTab inserts spaces to the next tab stop at (line, col) and returns the new column. tabSize is clamped to >= 1.

func (*TextBuffer) InsertText added in v0.3.0

func (b *TextBuffer) InsertText(line, col int, text string) (newLine, newCol int)

InsertText inserts a (possibly multi-line) text string at (line, col). Used for paste operations. Always forces an undo break. Returns the cursor position at the end of the inserted text. Empty text is a no-op.

func (*TextBuffer) Line added in v0.1.0

func (b *TextBuffer) Line(n int) string

Line returns line n. Returns "" if n is out of range.

func (*TextBuffer) LineCount added in v0.1.0

func (b *TextBuffer) LineCount() int

LineCount returns the number of lines in the buffer.

func (*TextBuffer) Lines added in v0.1.0

func (b *TextBuffer) Lines() []string

Lines returns the current lines (read-only view).

func (*TextBuffer) MarkClean added in v0.1.0

func (b *TextBuffer) MarkClean()

MarkClean clears the dirty flag, typically called after a successful save.

func (*TextBuffer) MoveLine added in v0.3.0

func (b *TextBuffer) MoveLine(line, delta int) bool

MoveLine moves the line at the given index by delta positions (e.g. -1 for up, +1 for down). Returns true if the move happened, false if the line is already at the boundary. Always forces an undo break when a move occurs.

func (*TextBuffer) Redo added in v0.1.0

func (b *TextBuffer) Redo(cursorLine, cursorCol int) (newLine, newCol int, ok bool)

Redo restores the next buffer snapshot. Returns the restored cursor position and true, or the original position and false if nothing to redo.

func (*TextBuffer) SetLines added in v0.1.0

func (b *TextBuffer) SetLines(lines []string)

SetLines replaces all content, marks the buffer dirty, and clears undo/redo.

func (*TextBuffer) SplitLine added in v0.1.0

func (b *TextBuffer) SplitLine(line, col int, autoIndent bool)

SplitLine splits the line at col (Enter key). If autoIndent is true the new line inherits the leading whitespace of the current line.

func (*TextBuffer) Undo added in v0.1.0

func (b *TextBuffer) Undo(cursorLine, cursorCol int) (newLine, newCol int, ok bool)

Undo restores the previous buffer snapshot. Returns the restored cursor position and true, or the original position and false if nothing to undo.

Jump to

Keyboard shortcuts

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