viewport

package
v2.0.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package viewport provides a component for rendering a viewport in a Bubble Tea.

Index

Constants

This section is empty.

Variables

View Source
var NoGutter = func(GutterContext) string { return "" }

NoGutter is the default gutter used.

Functions

This section is empty.

Types

type GutterContext

type GutterContext struct {
	// Index is the line index of the line which the gutter is being rendered for.
	Index int

	// TotalLines is the total number of lines in the viewport.
	TotalLines int

	// Soft is whether or not the line is soft wrapped.
	Soft bool
}

GutterContext provides context to a GutterFunc.

type GutterFunc

type GutterFunc func(GutterContext) string

GutterFunc can be implemented and set into [Model.LeftGutterFunc].

Example implementation showing line numbers:

func(info GutterContext) string {
	if info.Soft {
		return "     │ "
	}
	if info.Index >= info.TotalLines {
		return "   ~ │ "
	}
	return fmt.Sprintf("%4d │ ", info.Index+1)
}

type KeyMap

type KeyMap struct {
	PageDown     key.Binding
	PageUp       key.Binding
	HalfPageUp   key.Binding
	HalfPageDown key.Binding
	Down         key.Binding
	Up           key.Binding
	Left         key.Binding
	Right        key.Binding
}

KeyMap defines the keybindings for the viewport. Note that you don't necessary need to use keybindings at all; the viewport can be controlled programmatically with methods like Model.LineDown(1). See the GoDocs for details.

func DefaultKeyMap

func DefaultKeyMap() KeyMap

DefaultKeyMap returns a set of pager-like default keybindings.

type Model

type Model struct {
	KeyMap KeyMap

	// Whether or not to wrap text. If false, it'll allow horizontal scrolling
	// instead.
	SoftWrap bool

	// Whether or not to fill to the height of the viewport with empty lines.
	FillHeight bool

	// Whether or not to respond to the mouse. The mouse must be enabled in
	// Bubble Tea for this to work. For details, see the Bubble Tea docs.
	MouseWheelEnabled bool

	// The number of lines the mouse wheel will scroll. By default, this is 3.
	MouseWheelDelta int

	// YPosition is the position of the viewport in relation to the terminal
	// window. It's used in high performance rendering only.
	YPosition int

	// Style applies a lipgloss style to the viewport. Realistically, it's most
	// useful for setting borders, margins and padding.
	Style lipgloss.Style

	// LeftGutterFunc allows to define a [GutterFunc] that adds a column into
	// the left of the viewport, which is kept when horizontal scrolling.
	// This can be used for things like line numbers, selection indicators,
	// show statuses, etc. It is expected that the real-width (as measured by
	// [lipgloss.Width]) of the returned value is always consistent, regardless
	// of index, soft wrapping, etc.
	LeftGutterFunc GutterFunc

	// HighlightStyle highlights the ranges set with [SetHighligths].
	HighlightStyle lipgloss.Style

	// SelectedHighlightStyle highlights the highlight range focused during
	// navigation.
	// Use [SetHighligths] to set the highlight ranges, and [HightlightNext]
	// and [HihglightPrevious] to navigate.
	SelectedHighlightStyle lipgloss.Style

	// StyleLineFunc allows to return a [lipgloss.Style] for each line.
	// The argument is the line index.
	StyleLineFunc func(int) lipgloss.Style
	// contains filtered or unexported fields
}

Model is the Bubble Tea model for this viewport element.

func New

func New(opts ...Option) (m Model)

New returns a new model with the given width and height as well as default key mappings.

func (Model) AtBottom

func (m Model) AtBottom() bool

AtBottom returns whether or not the viewport is at or past the very bottom position.

func (Model) AtTop

func (m Model) AtTop() bool

AtTop returns whether or not the viewport is at the very top position.

func (*Model) ClearHighlights

func (m *Model) ClearHighlights()

ClearHighlights clears previously set highlights.

func (*Model) EnsureVisible

func (m *Model) EnsureVisible(line, colstart, colend int)

EnsureVisible ensures that the given line and column are in the viewport.

func (Model) GetContent

func (m Model) GetContent() string

GetContent returns the entire content as a single string. Line endings are normalized to '\n'.

func (*Model) GotoBottom

func (m *Model) GotoBottom() (lines []string)

GotoBottom sets the viewport to the bottom position.

func (*Model) GotoTop

func (m *Model) GotoTop() (lines []string)

GotoTop sets the viewport to the top position.

func (*Model) HalfPageDown

func (m *Model) HalfPageDown()

HalfPageDown moves the view down by half the height of the viewport.

func (*Model) HalfPageUp

func (m *Model) HalfPageUp()

HalfPageUp moves the view up by half the height of the viewport.

func (Model) Height

func (m Model) Height() int

Height returns the height of the viewport.

func (*Model) HighlightNext

func (m *Model) HighlightNext()

HighlightNext highlights the next match.

func (*Model) HighlightPrevious

func (m *Model) HighlightPrevious()

HighlightPrevious highlights the previous match.

func (Model) HorizontalScrollPercent

func (m Model) HorizontalScrollPercent() float64

HorizontalScrollPercent returns the amount horizontally scrolled as a float between 0 and 1.

func (Model) Init

func (m Model) Init() tea.Cmd

Init exists to satisfy the tea.Model interface for composability purposes.

func (*Model) PageDown

func (m *Model) PageDown()

PageDown moves the view down by the number of lines in the viewport.

func (*Model) PageUp

func (m *Model) PageUp()

PageUp moves the view up by one height of the viewport.

func (Model) PastBottom

func (m Model) PastBottom() bool

PastBottom returns whether or not the viewport is scrolled beyond the last line. This can happen when adjusting the viewport height.

func (*Model) ScrollDown

func (m *Model) ScrollDown(n int)

ScrollDown moves the view down by the given number of lines.

func (*Model) ScrollLeft

func (m *Model) ScrollLeft(n int)

ScrollLeft moves the viewport to the left by the given number of columns.

func (Model) ScrollPercent

func (m Model) ScrollPercent() float64

ScrollPercent returns the amount scrolled as a float between 0 and 1.

func (*Model) ScrollRight

func (m *Model) ScrollRight(n int)

ScrollRight moves viewport to the right by the given number of columns.

func (*Model) ScrollUp

func (m *Model) ScrollUp(n int)

ScrollUp moves the view up by the given number of lines.

func (*Model) SetContent

func (m *Model) SetContent(s string)

SetContent set the pager's text content. Line endings will be normalized to '\n'.

func (*Model) SetContentLines

func (m *Model) SetContentLines(lines []string)

SetContentLines allows to set the lines to be shown instead of the content. If a given line has a \n in it, it will still be split into multiple lines similar to that of Model.SetContent. See also Model.SetContent.

func (*Model) SetHeight

func (m *Model) SetHeight(h int)

SetHeight sets the height of the viewport.

func (*Model) SetHighlights

func (m *Model) SetHighlights(matches [][]int)

SetHighlights sets ranges of characters to highlight. For instance, `[]int{[]int{2, 10}, []int{20, 30}}` will highlight characters 2 to 10 and 20 to 30. Note that highlights are not expected to transpose each other, and are also expected to be in order. Use Model.SetHighlights to set the highlight ranges, and Model.HighlightNext and Model.HighlightPrevious to navigate. Use Model.ClearHighlights to remove all highlights.

func (*Model) SetHorizontalStep

func (m *Model) SetHorizontalStep(n int)

SetHorizontalStep sets the amount of cells that the viewport moves in the default viewport keymapping. If set to 0 or less, horizontal scrolling is disabled.

func (*Model) SetWidth

func (m *Model) SetWidth(w int)

SetWidth sets the width of the viewport.

func (*Model) SetXOffset

func (m *Model) SetXOffset(n int)

SetXOffset sets the X offset. No-op when soft wrap is enabled.

func (*Model) SetYOffset

func (m *Model) SetYOffset(n int)

SetYOffset sets the Y offset.

func (Model) TotalLineCount

func (m Model) TotalLineCount() int

TotalLineCount returns the total number of lines (both hidden and visible) within the viewport.

func (Model) Update

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

Update handles standard message-based viewport updates.

func (Model) View

func (m Model) View() string

View renders the viewport into a string.

func (Model) VisibleLineCount

func (m Model) VisibleLineCount() int

VisibleLineCount returns the number of the visible lines within the viewport.

func (Model) Width

func (m Model) Width() int

Width returns the width of the viewport.

func (*Model) XOffset

func (m *Model) XOffset() int

XOffset returns the current X offset - the horizontal scroll position.

func (*Model) YOffset

func (m *Model) YOffset() int

YOffset returns the current Y offset - the vertical scroll position.

type Option

type Option func(*Model)

Option is a configuration option that works in conjunction with New. For example:

timer := New(WithWidth(10, WithHeight(5)))

func WithHeight

func WithHeight(h int) Option

WithHeight is an initialization option that sets the height of the viewport. Pass as an argument to New.

func WithWidth

func WithWidth(w int) Option

WithWidth is an initialization option that sets the width of the viewport. Pass as an argument to New.

Jump to

Keyboard shortcuts

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