Module: TuiTui::TextView

Defined in:
lib/tui_tui/text_view.rb

Overview

A scrolled read-only text window. Lines may be Strings, Lines, or Span arrays, supplied eagerly or lazily.

Class Method Summary collapse

Class Method Details

.as_line(content, style) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/tui_tui/text_view.rb', line 45

def as_line(content, style)
  case content
  when Line
    content
  when Array
    Line.new(content)
  else
    Line[Span[content.to_s, style]]
  end
end

.draw(canvas, rect, lines = nil, top: 0, style: nil, scrollbar: nil, total: nil, auto: false) ⇒ Object



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

def draw(canvas, rect, lines = nil, top: 0, style: nil, scrollbar: nil, total: nil, auto: false)
  # With auto:, reserve the gutter only when the content overflows the rect.
  # When the total is unknown (lazy block, no total:), the bar is kept.
  count = total || lines&.length
  show_bar = scrollbar && !(auto && count && count <= rect.rows)
  body, gutter = show_bar ? rect.split_gutter : [rect, nil]
  body.rows.times do |offset|
    index = top + offset
    content = lines ? lines[index] : yield(index)
    next if content.nil?

    canvas.line(body.row + offset, body.col, as_line(content, style).truncate(body.cols))
  end

  draw_scrollbar(canvas, gutter, top, total || lines&.length, body.rows, scrollbar) if gutter
  canvas
end

.draw_scrollbar(canvas, gutter, top, total, visible, theme) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/tui_tui/text_view.rb', line 31

def draw_scrollbar(canvas, gutter, top, total, visible, theme)
  return unless total

  Scrollbar.draw(
    canvas,
    gutter,
    top: top,
    visible: visible,
    total: total,
    track_style: theme.scroll_track,
    thumb_style: theme.scroll_thumb
  )
end