scrollbar

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: 9 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 scrollbar is a scrollbar widget: a track with a thumb whose size and position show how much of some content is visible and where. It does not own the content. The app gives it the content's Total size, how much is Visible and the current Offset (the numbers viewport, logview and virtuallist already keep), draws it beside the content, and applies the ScrolledMsg it sends back when the user scrolls it by key, wheel, click or drag.

The bar is vertical by default (one column wide) or horizontal (one row tall). It has a LayoutNode, draws straight into a cell grid (layout.CellNode), and reads its colours from a theme.Theme.

Example

A scrollbar for 100 lines with 10 showing; the down arrow scrolls one line.

package main

import (
	"fmt"

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

func main() {
	m := scrollbar.New(100, 10)
	m, _ = m.Update(tui.Key{Type: tui.KeyDown})
	fmt.Println(m.Offset)
	fmt.Println(m.Linearize())
}
Output:
1
Scrollbar, vertical, 1% scrolled, showing 10 of 100

Index

Examples

Constants

View Source
const WheelStep = 3

WheelStep is how many content units one wheel notch scrolls when the Model's Wheel field is 0.

Variables

This section is empty.

Functions

This section is empty.

Types

type KeyMap

type KeyMap struct {
	Up       keymap.Binding
	Down     keymap.Binding
	PageUp   keymap.Binding
	PageDown keymap.Binding
	Start    keymap.Binding
	End      keymap.Binding
}

KeyMap is the set of keys Update reacts to.

func DefaultKeyMap

func DefaultKeyMap() KeyMap

DefaultKeyMap returns the default bindings: up/down (k/j) scroll one unit, pgup/pgdown a page, home/end jump to the ends. Left and right also scroll a Horizontal bar.

type Model

type Model struct {
	// Total is the size of the whole content, Visible how much of it shows
	// at once, and Offset where the visible window starts (0 to
	// Total-Visible).
	Total, Visible, Offset int
	// Orientation is Vertical (the zero value) or Horizontal.
	Orientation Orientation
	// Length is the track length in cells. When Bounds is not empty the
	// track is as long as Bounds on the bar's axis and Length is ignored.
	Length int
	// Wheel is the content units per wheel notch; 0 means WheelStep.
	Wheel int
	Theme theme.Theme

	// KeyMap holds the key bindings Update obeys. New fills it with
	// DefaultKeyMap; a Model built as a literal with no bindings set behaves
	// as if it held DefaultKeyMap.
	KeyMap KeyMap

	// Mouse, when true, makes Update handle tui.MouseEvent: a left press on
	// the thumb starts a drag, a press on the track pages toward it, and the
	// wheel over Bounds scrolls. Off (the default) ignores the mouse.
	Mouse bool
	// Bounds is the screen rectangle where the app draws the bar; mouse
	// events outside it are ignored (a drag in progress keeps following the
	// pointer wherever it goes).
	Bounds hittest.Rect
	// contains filtered or unexported fields
}

Model is a scrollbar. Total, Visible and Offset are in the content's own units (lines or columns); the thumb is Visible/Total of the track long and sits at Offset/(Total-Visible) of the way along.

func New

func New(total, visible int) Model

New builds a vertical scrollbar for content of total units with visible shown, at offset 0.

func (Model) Bindings

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

Bindings returns the active bindings, for help text.

func (Model) Dragging

func (m Model) Dragging() bool

Dragging reports whether the thumb is being dragged.

func (Model) LayoutNode

func (m Model) LayoutNode() layout.Node

LayoutNode adapts the scrollbar to a layout.Node (also a layout.CellNode). Measure reports one cell across and Length along the bar's axis; Render and DrawCells fit the allotted Size, so the track is as long as the space the layout gives it (Length and Bounds are ignored there). The Model is not changed.

func (Model) Linearize

func (m Model) Linearize() string

Linearize describes the bar as plain text for accessible output (see tui.Linearizer), e.g. "Scrollbar, vertical, 25% scrolled, showing 10 of 40" or "Scrollbar, horizontal, all content visible".

func (Model) MaxOffset

func (m Model) MaxOffset() int

MaxOffset is the largest Offset: Total-Visible, or 0 when everything is visible.

func (Model) Percent

func (m Model) Percent() float64

Percent is the scroll position from 0 (top) to 1 (end); it is 0 when nothing can scroll.

func (Model) Scrollable

func (m Model) Scrollable() bool

Scrollable reports whether there is content beyond the visible window.

func (*Model) SetContent

func (m *Model) SetContent(total, visible int)

SetContent sets Total and Visible together and re-clamps the offset.

func (*Model) SetOffset

func (m *Model) SetOffset(o int)

SetOffset sets the offset to o, clamped to 0..MaxOffset.

func (Model) SetTheme

func (m Model) SetTheme(t theme.Theme) Model

SetTheme returns m with t applied. It makes Model a tui.ThemeSetter, so a root model can forward the Program's theme (see tui.WithTheme).

func (Model) Thumb

func (m Model) Thumb(n int) (start, size int)

Thumb returns the thumb's start and size in cells along a track of length n: the size is n*Visible/Total (at least 1, at most n) and the start moves from 0 to n-size as Offset goes from 0 to MaxOffset. When nothing can scroll the thumb fills the track.

func (Model) Tokens

func (m Model) Tokens() theme.Tokens

Tokens returns the colour tokens the widget renders with: its theme's roles, overridden by any theme.WithTokens(theme.ComponentScrollbar, ...) and then by WithTokens.

func (Model) Update

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

Update scrolls on the KeyMap bindings and, with Mouse on, on mouse events (see Model.Mouse). It returns a Cmd delivering ScrolledMsg when the offset changed. Any other Msg is a no-op.

func (Model) View

func (m Model) View() string

View renders the bar over its track (Bounds, or Length when Bounds is empty): one column of rows for Vertical, one row for Horizontal.

func (Model) WithTokens

func (m Model) WithTokens(tok theme.Tokens) Model

WithTokens returns m with tok as its per-instance colour override. Nil fields inherit from the theme, so only the roles tok names change; the theme and every other widget are untouched. A second call replaces the first.

type Orientation

type Orientation int

Orientation is the axis a scrollbar runs along.

const (
	// Vertical is a one-column bar along the right edge of the content. It
	// is the zero value.
	Vertical Orientation = iota
	// Horizontal is a one-row bar along the bottom edge of the content.
	Horizontal
)

type ScrolledMsg

type ScrolledMsg struct{ Offset int }

ScrolledMsg is delivered (via the Cmd Update returns) when the user moves the offset. The app applies Offset to its content.

Jump to

Keyboard shortcuts

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