Module: TuiTui::Widget::Progress

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

Overview

A stateless horizontal progress bar for a 1-row rect. The caller passes the ratio and Theme roles (e.g. bar_style: theme.selection, track_style: theme.muted); default glyphs are ASCII (README N7). If the rect is taller than one row, every row gets the bar.

Class Method Summary collapse

Class Method Details

.draw(canvas, rect, ratio, bar_style:, track_style: nil, char: "#", track_char: "-") ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tui_tui/widget/progress.rb', line 14

def draw(canvas, rect, ratio, bar_style:, track_style: nil, char: "#", track_char: "-")
  return canvas if rect.rows <= 0 || rect.cols <= 0

  filled = (rect.cols * ratio.to_f.clamp(0.0, 1.0)).round
  # char / track_char are meant to be 1 column wide (the ASCII defaults
  # are, per N7); a wider glyph is clipped to the cell budget so the bar
  # never overflows the rect.
  bar = DisplayText.new(char * filled).truncate(filled, marker: "").to_s
  track = DisplayText.new(track_char * (rect.cols - filled)).truncate(rect.cols - filled, marker: "").to_s
  rect.rows.times do |dr|
    row = rect.row + dr
    canvas.text(row, rect.col, bar, bar_style) if filled.positive?
    canvas.text(row, rect.col + filled, track, track_style) if filled < rect.cols
  end

  canvas
end