Class: TuiTui::Pager

Inherits:
Modal
  • Object
show all
Defined in:
lib/tui_tui/pager.rb

Overview

Scrollable read-only text modal.

Constant Summary collapse

MARGIN =
2
WHEEL =
3

Constants inherited from Modal

Modal::PAD

Instance Method Summary collapse

Constructor Details

#initialize(title, lines, start: 0, close_keys: [], theme: Theme::DEFAULT) ⇒ Pager

Returns a new instance of Pager.



15
16
17
18
19
20
21
22
# File 'lib/tui_tui/pager.rb', line 15

def initialize(title, lines, start: 0, close_keys: [], theme: Theme::DEFAULT)
  @title = title
  @lines = lines.map { |line| DisplayText.new(line) }
  @top = start
  @page = 1
  @close_keys = close_keys
  @theme = theme
end

Instance Method Details

#draw(canvas, size) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/tui_tui/pager.rb', line 47

def draw(canvas, size)
  width = [size.cols - (MARGIN * 2), 20].max
  height = [size.rows - (MARGIN * 2), 5].max
  rect = Rect.centered(size, cols: width, rows: height)
  canvas.frame(rect, style: theme.frame)

  inner = width - 4
  body = [height - 4, 1].max
  @page = body
  clamp(body)

  canvas.text(rect.row + 1, rect.col + 2, DisplayText.new(title_line(body)).truncate(inner), theme.title)
  body.times do |offset|
    line = @lines[@top + offset]
    next if line.nil?

    canvas.text(rect.row + 3 + offset, rect.col + 2, line.truncate(inner), theme.muted)
  end

  canvas
end

#handle(key) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tui_tui/pager.rb', line 24

def handle(key)
  return :close if @close_keys.include?(key)

  case KeyIntent.for(key)
  when :up
    scroll(-1)
  when :down
    scroll(1)
  when :top
    scroll(-@lines.size)
  when :bottom
    scroll(@lines.size)
  when :cancel
    :close
  else
    paginate(key)
  end
end

#handle_mouse(event) ⇒ Object



43
44
45
# File 'lib/tui_tui/pager.rb', line 43

def handle_mouse(event)
  scroll(event.button == :wheel_up ? -WHEEL : WHEEL) if event.action == :wheel
end