Documentation
¶
Overview ¶
Package viewport provides a component for rendering a viewport in a Bubble Tea.
Index ¶
- Variables
- type GutterContext
- type GutterFunc
- type KeyMap
- type Model
- func (m Model) AtBottom() bool
- func (m Model) AtTop() bool
- func (m *Model) ClearHighlights()
- func (m *Model) EnsureVisible(line, colstart, colend int)
- func (m Model) GetContent() string
- func (m *Model) GotoBottom() (lines []string)
- func (m *Model) GotoTop() (lines []string)
- func (m *Model) HalfPageDown()
- func (m *Model) HalfPageUp()
- func (m Model) Height() int
- func (m *Model) HighlightNext()
- func (m *Model) HighlightPrevious()
- func (m Model) HorizontalScrollPercent() float64
- func (m Model) Init() tea.Cmd
- func (m *Model) PageDown()
- func (m *Model) PageUp()
- func (m Model) PastBottom() bool
- func (m *Model) ScrollDown(n int)
- func (m *Model) ScrollLeft(n int)
- func (m Model) ScrollPercent() float64
- func (m *Model) ScrollRight(n int)
- func (m *Model) ScrollUp(n int)
- func (m *Model) SetContent(s string)
- func (m *Model) SetContentLines(lines []string)
- func (m *Model) SetHeight(h int)
- func (m *Model) SetHighlights(matches [][]int)
- func (m *Model) SetHorizontalStep(n int)
- func (m *Model) SetWidth(w int)
- func (m *Model) SetXOffset(n int)
- func (m *Model) SetYOffset(n int)
- func (m Model) TotalLineCount() int
- func (m Model) Update(msg tea.Msg) (Model, tea.Cmd)
- func (m Model) View() string
- func (m Model) VisibleLineCount() int
- func (m Model) Width() int
- func (m *Model) XOffset() int
- func (m *Model) YOffset() int
- type Option
Constants ¶
This section is empty.
Variables ¶
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 (Model) AtBottom ¶
AtBottom returns whether or not the viewport is at or past the very bottom position.
func (*Model) ClearHighlights ¶
func (m *Model) ClearHighlights()
ClearHighlights clears previously set highlights.
func (*Model) EnsureVisible ¶
EnsureVisible ensures that the given line and column are in the viewport.
func (Model) GetContent ¶
GetContent returns the entire content as a single string. Line endings are normalized to '\n'.
func (*Model) GotoBottom ¶
GotoBottom sets the viewport to the bottom 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) 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 ¶
HorizontalScrollPercent returns the amount horizontally scrolled as a float between 0 and 1.
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 ¶
PastBottom returns whether or not the viewport is scrolled beyond the last line. This can happen when adjusting the viewport height.
func (*Model) ScrollDown ¶
ScrollDown moves the view down by the given number of lines.
func (*Model) ScrollLeft ¶
ScrollLeft moves the viewport to the left by the given number of columns.
func (Model) ScrollPercent ¶
ScrollPercent returns the amount scrolled as a float between 0 and 1.
func (*Model) ScrollRight ¶
ScrollRight moves viewport to the right by the given number of columns.
func (*Model) SetContent ¶
SetContent set the pager's text content. Line endings will be normalized to '\n'.
func (*Model) SetContentLines ¶
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) SetHighlights ¶
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 ¶
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) SetXOffset ¶
SetXOffset sets the X offset. No-op when soft wrap is enabled.
func (Model) TotalLineCount ¶
TotalLineCount returns the total number of lines (both hidden and visible) within the viewport.
func (Model) VisibleLineCount ¶
VisibleLineCount returns the number of the visible lines within the viewport.