scrollbar

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Percent

func Percent(offset, total, viewport int) int

Percent returns the scroll position as a percentage in the style of less(1): the percentage of content above and including the bottom of the viewport. This means the value never reaches 0% when there is content, and reaches 100% exactly at the end.

func Position

func Position(start, end, total int) string

Position returns a formatted scroll position string like "1-10/42 (24%)". The start parameter is 0-indexed; it is displayed as 1-indexed in the output.

func ThumbMetrics

func ThumbMetrics(height, totalLines int, percent float64) (int, int)

ThumbMetrics returns the thumb position and size for a proportional scrollbar.

func ThumbMetricsWithConfig

func ThumbMetricsWithConfig(height, totalLines int, percent float64, cfg Config) (int, int)

ThumbMetricsWithConfig returns the thumb position and size for a proportional scrollbar using the supplied config.

func ThumbRange

func ThumbRange(height, totalLines int, percent float64, cfg Config) (int, int)

ThumbRange returns the start row, inclusive, and end row, exclusive, of the rendered thumb within a track of the given height.

Types

type Config

type Config struct {
	// ThumbSymbol is the cell rendered for the scrollbar thumb.
	ThumbSymbol string
	// TrackSymbol is the cell rendered for the scrollbar track.
	TrackSymbol string
	// MaxThumbDivisor caps the thumb height to Height/MaxThumbDivisor.
	// The default is 2. Set to 1 for a fully proportional thumb capped only
	// by the track height.
	MaxThumbDivisor int
}

Config controls scrollbar rendering and geometry.

Zero-valued fields preserve the package defaults.

type Drag

type Drag struct {
	Active bool
	// contains filtered or unexported fields
}

Drag tracks the state of a scrollbar drag interaction.

Typical usage in a Bubble Tea Update loop:

case tea.MouseClickMsg:
    offset := drag.Press(hitbox, msg.Y, vp.ScrollPercent())
    vp.SetYOffset(offset)

case tea.MouseMotionMsg:
    if offset, ok := drag.Motion(hitbox, msg.Y); ok {
        vp.SetYOffset(offset)
    }

case tea.MouseReleaseMsg:
    drag.Release()

func (*Drag) Motion

func (d *Drag) Motion(h Hitbox, mouseY int) (int, bool)

Motion updates the drag position and returns the new viewport offset. Returns ok=false if no drag is active.

func (*Drag) Press

func (d *Drag) Press(h Hitbox, mouseY int, scrollPercent float64) int

Press begins a drag at the given mouse position. It computes the grab offset (preserving relative position if clicking on the thumb, centering otherwise) and returns the viewport offset to scroll to.

The scrollPercent parameter is the viewport's current scroll position (0.0 to 1.0), used to determine the thumb's current position.

func (*Drag) Release

func (d *Drag) Release()

Release ends the drag interaction.

type Hitbox

type Hitbox struct {
	X          int // column position
	Y          int // top row
	Height     int // visible track height
	TotalLines int // total content lines
	Config     Config
}

Hitbox describes the screen region occupied by a vertical scrollbar.

func (Hitbox) Contains

func (h Hitbox) Contains(x, y int) bool

Contains reports whether the screen coordinate (x, y) falls within the hitbox.

func (Hitbox) ContainsThumb

func (h Hitbox) ContainsThumb(x, y int, scrollPercent float64) bool

ContainsThumb reports whether the screen coordinate (x, y) falls within the rendered scrollbar thumb.

type Model

type Model struct {
	Config     Config
	Height     int
	TotalLines int
	Percent    float64
	Styles     Styles
}

func (Model) Chars

func (m Model) Chars() []string

Chars returns the scrollbar as one rendered cell per line.

func (Model) Render

func (m Model) Render() string

Render returns the scrollbar as a multi-line string.

type Styles

type Styles struct {
	Thumb lg.Style
	Track lg.Style
}

Jump to

Keyboard shortcuts

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