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



41
42
43
44
45
46
47
48
49
50
# File 'lib/tui_tui/text_view.rb', line 41

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) ⇒ Object



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

def draw(canvas, rect, lines = nil, top: 0, style: nil, scrollbar: nil, total: nil)
  body, gutter = scrollbar ? 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



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tui_tui/text_view.rb', line 27

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