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 ¶
- type KeyMap
- type Model
- func (m *Model) AppendLine(s string)
- func (m Model) AtBottom() bool
- func (m Model) AtTop() bool
- func (m Model) Bindings() []keymap.Binding
- func (m Model) CurrentMatch() int
- func (m Model) DrawCells(buf *cellbuf.Buffer, r cellbuf.Rect)
- func (m *Model) GotoBottom()
- func (m *Model) GotoTop()
- func (m *Model) HalfPageDown()
- func (m *Model) HalfPageUp()
- func (m Model) LayoutNode() layout.Node
- func (m Model) LineCount() int
- func (m *Model) LineDown(n int)
- func (m *Model) LineLeft(n int)
- func (m *Model) LineRight(n int)
- func (m *Model) LineUp(n int)
- func (m Model) Linearize() string
- func (m Model) MatchCount() int
- func (m *Model) NextMatch() bool
- func (m *Model) PageDown()
- func (m *Model) PageUp()
- func (m *Model) PrevMatch() bool
- func (m Model) ScrollPercent() float64
- func (m *Model) Search(q string) int
- func (m Model) SearchQuery() string
- func (m *Model) SetContent(s string)
- func (m *Model) TrimFront(max int) int
- func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)
- func (m Model) View() string
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 (*Model) AppendLine ¶
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) Bindings ¶
Bindings returns the scroll actions the viewport currently honours, with descriptions, for help text.
func (Model) CurrentMatch ¶
CurrentMatch returns the 1-based index of the current match, or 0.
func (Model) DrawCells ¶
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) 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 ¶
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) LineRight ¶
LineRight scrolls right n columns, clamped to the rightmost column that still shows content (see maxXOffset).
func (Model) Linearize ¶
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 ¶
MatchCount returns how many matches the active query has in the content.
func (*Model) NextMatch ¶
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) PrevMatch ¶
PrevMatch moves to the previous match, wrapping from the first to the last.
func (Model) ScrollPercent ¶
ScrollPercent returns how far scrolled through the content [0,1] is, or 1 if all content already fits (nothing to scroll).
func (*Model) Search ¶
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 ¶
SearchQuery returns the active query, "" if none.
func (*Model) SetContent ¶
SetContent replaces the content, re-clamping both scroll offsets if the new content is shorter, or narrower, than before.
func (*Model) TrimFront ¶
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.