Module: TuiTui::Scrollbar

Defined in:
lib/tui_tui/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: "|", thumb: " ", track_style: TRACK, thumb_style: THUMB) ⇒ Object



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

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

  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.



30
31
32
33
34
35
36
37
38
# File 'lib/tui_tui/scrollbar.rb', line 30

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