viewport

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 7 Imported by: ? 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 viewport is a scrollable window onto content taller than it, built on top of the root tui package the same way textinput is.

Example

A viewport shows a window onto longer content and scrolls it.

package main

import (
	"fmt"
	"strings"

	"github.com/ows4444/tui/viewport"
)

func main() {
	m := viewport.New(10, 3)
	m.SetContent(strings.Join([]string{"one", "two", "three", "four", "five"}, "\n"))
	fmt.Println(m.View())
	m.LineDown(2)
	fmt.Println("--")
	fmt.Println(m.View())
}
Output:
one
two
three
--
three
four
five

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type KeyMap

type KeyMap struct {
	Up       keymap.Binding // scroll up one line
	Down     keymap.Binding // scroll down one line
	PageUp   keymap.Binding // scroll up one page
	PageDown keymap.Binding // scroll down one page
	Top      keymap.Binding // scroll to the top
	Bottom   keymap.Binding // scroll to the bottom

	// NextMatch and PrevMatch move between search matches while a query is set
	// (see Search). When both are empty they default to "n" and "N". They are
	// not part of Bindings, which lists the scroll actions.
	NextMatch keymap.Binding
	PrevMatch keymap.Binding
}

KeyMap names the keys of each action of a Model.

func DefaultKeyMap

func DefaultKeyMap() KeyMap

DefaultKeyMap returns the keys a Model used before KeyMap existed.

type Model

type Model struct {
	Width  int
	Height int

	// Raw, when true, makes SetContent and AppendLine keep text unchanged.
	// By default they expand tabs and strip every escape sequence and
	// control character except SGR styling (ansi.SanitizeKeepSGR).
	Raw bool

	// KeyMap holds the keys for each scroll action. New fills it with
	// DefaultKeyMap; a Model built as a struct literal with a zero KeyMap
	// behaves as if it held DefaultKeyMap. Mouse wheel scrolling is not
	// key-driven and is not part of it.
	KeyMap KeyMap

	// SoftWrap, when true, wraps lines longer than Width onto further rows by
	// display width (never splitting a grapheme cluster) instead of clipping
	// them. Scrolling, offsets, AtBottom and ScrollPercent then count visual
	// rows; LineCount still counts content lines. Horizontal scrolling is off.
	// Toggling it or changing Width is picked up by the next Update or content
	// change; until then reads rebuild the wrap on the fly.
	SoftWrap bool
	// contains filtered or unexported fields
}

Model is a fixed-size scrollable window onto SetContent's lines.

func New

func New(width, height int) Model

New returns a Model sized to width x height. Content is set separately via SetContent.

func (*Model) AppendLine

func (m *Model) AppendLine(s string)

AppendLine adds s to the end of the content without touching the lines already there, so appending n lines costs O(n) in total, where calling SetContent with the whole text each time costs O(n^2). s may contain "\n"; each part becomes its own line, exactly as SetContent would split it. The scroll offsets are re-clamped like SetContent (a reader who is scrolled up stays where they are; use AtBottom and GotoBottom to follow the tail).

It grows the Model's line slice in place, like append: do not append to two copies of the same Model and expect them to stay independent.

func (Model) AtBottom

func (m Model) AtBottom() bool

AtBottom reports whether the viewport is scrolled to the bottom.

func (Model) AtTop

func (m Model) AtTop() bool

AtTop reports whether the viewport is scrolled to the top.

func (Model) Bindings

func (m Model) Bindings() []keymap.Binding

Bindings returns the scroll actions the viewport currently honours, with descriptions, for help text.

func (Model) CurrentMatch

func (m Model) CurrentMatch() int

CurrentMatch returns the 1-based index of the current match, or 0.

func (Model) DrawCells

func (m Model) DrawCells(buf *cellbuf.Buffer, r cellbuf.Rect)

DrawCells draws the viewport into r of buf, producing the screen View shows: the visible lines, scrolled by the offsets, clipped to Width x Height (and to r). It implements tui.CellDrawer and writes each line straight into the grid, without building a View string. Rows below the content are left as buf has them (blank on a fresh frame).

func (*Model) GotoBottom

func (m *Model) GotoBottom()

GotoBottom scrolls to the very bottom of the content.

func (*Model) GotoTop

func (m *Model) GotoTop()

GotoTop scrolls to the very top of the content.

func (*Model) HalfPageDown

func (m *Model) HalfPageDown()

HalfPageDown scrolls down half a Height's worth of lines.

func (*Model) HalfPageUp

func (m *Model) HalfPageUp()

HalfPageUp scrolls up half a Height's worth of lines.

func (Model) LayoutNode

func (m Model) LayoutNode() layout.Node

LayoutNode adapts the viewport to a layout.Node. Measure reports the content's natural size (widest line by number of lines). Render shows exactly the allotted Size: it uses that size in place of Width and Height, keeps the current scroll offsets (re-clamped to the new size), and does not change the Model, so the size a parent gives it never overwrites the Model's own Width and Height.

func (Model) LineCount

func (m Model) LineCount() int

LineCount returns the number of content lines currently held.

func (*Model) LineDown

func (m *Model) LineDown(n int)

LineDown scrolls down n lines, clamped to the bottom.

func (*Model) LineLeft

func (m *Model) LineLeft(n int)

LineLeft scrolls left n columns, clamped to the left edge.

func (*Model) LineRight

func (m *Model) LineRight(n int)

LineRight scrolls right n columns, clamped to the rightmost column that still shows content (see maxXOffset).

func (*Model) LineUp

func (m *Model) LineUp(n int)

LineUp scrolls up n lines, clamped to the top.

func (Model) Linearize

func (m Model) Linearize() string

Linearize renders the whole content as plain text for accessible output (see tui.Linearizer): every line, escape sequences removed, with none of the windowing. Scrolling has no meaning in a linear transcript, and the content is what a reader wants. There is no header, so appending content only adds new lines to the transcript.

func (Model) MatchCount

func (m Model) MatchCount() int

MatchCount returns how many matches the active query has in the content.

func (*Model) NextMatch

func (m *Model) NextMatch() bool

NextMatch moves to the next match, wrapping from the last to the first, and scrolls it into view. It reports whether there was one.

func (*Model) PageDown

func (m *Model) PageDown()

PageDown scrolls down one Height's worth of lines.

func (*Model) PageUp

func (m *Model) PageUp()

PageUp scrolls up one Height's worth of lines.

func (*Model) PrevMatch

func (m *Model) PrevMatch() bool

PrevMatch moves to the previous match, wrapping from the first to the last.

func (Model) ScrollPercent

func (m Model) ScrollPercent() float64

ScrollPercent returns how far scrolled through the content [0,1] is, or 1 if all content already fits (nothing to scroll).

func (*Model) Search

func (m *Model) Search(q string) int

Search sets the query: every case-insensitive occurrence is highlighted and the first match at or below the top visible line becomes the current one and is scrolled into view. It returns the number of matches. An empty q clears the search. While a query is set, the NextMatch and PrevMatch keys ("n" and "N" by default) move between matches.

func (Model) SearchQuery

func (m Model) SearchQuery() string

SearchQuery returns the active query, "" if none.

func (*Model) SetContent

func (m *Model) SetContent(s string)

SetContent replaces the content, re-clamping both scroll offsets if the new content is shorter, or narrower, than before.

func (*Model) TrimFront

func (m *Model) TrimFront(max int) int

TrimFront drops the oldest lines so at most max remain, and returns how many were dropped (0 if max is negative or the content already fits). The vertical offset moves up by the dropped count, clamped at 0, so a reader scrolled back keeps seeing the same content; a view pinned to the bottom stays pinned. Dropping is amortised O(1) per line: lines are skipped by advancing an index, and the backing slice is compacted only once the skipped part outgrows the live part. At compaction the widest-line cache is recomputed; between compactions it may still count a dropped line, so horizontal scroll range can briefly be a little wider than the content.

func (Model) Update

func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)

Update handles arrow/page/home-end keys and mouse wheel scrolling.

func (Model) View

func (m Model) View() string

View renders exactly Height rows: the visible slice of content, padded with blank rows if there's less content than Height, each truncated to Width without breaking embedded ansi.Style codes.

Jump to

Keyboard shortcuts

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