Module: TuiTui::Widget::Scrollbar

Defined in:
lib/tui_tui/widget/scrollbar.rb

Overview

A vertical scroll indicator for a 1-column gutter. It sizes a thumb from top/visible/total and draws ASCII-only chrome.

Constant Summary collapse

TRACK =
Theme::DEFAULT.scroll_track
THUMB =
Theme::DEFAULT.scroll_thumb

Class Method Summary collapse

Class Method Details

.draw(canvas, rect, top:, visible:, total:, track: nil, thumb: " ", track_style: TRACK, thumb_style: THUMB) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/tui_tui/widget/scrollbar.rb', line 17

def draw(canvas, rect, top:, visible:, total:, track: nil, thumb: " ", track_style: TRACK, thumb_style: THUMB)
  return canvas if rect.rows <= 0

  track ||= canvas.chrome.track
  length, offset = geometry(rect.rows, top, visible, total)
  rect.rows.times do |i|
    in_thumb = i >= offset && i < offset + length
    canvas.text(rect.row + i, rect.col, in_thumb ? thumb : track, in_thumb ? thumb_style : track_style)
  end

  canvas
end

.geometry(height, top, visible, total) ⇒ Object

The thumb's [length, offset] in rows for a height-row track. Returns [0, 0] (no thumb, track only) when everything fits — nothing to scroll.



32
33
34
35
36
37
38
39
40
# File 'lib/tui_tui/widget/scrollbar.rb', line 32

def geometry(height, top, visible, total)
  visible = [visible, 1].max
  total = [total, visible].max
  return [0, 0] if total <= visible

  length = [(height * visible / total.to_f).round, 1].max.clamp(1, height)
  offset = ((height - length) * top.to_f / (total - visible)).round
  [length, offset.clamp(0, height - length)]
end