Class: TuiTui::Confirm

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

Overview

OK / Cancel confirmation modal.

Constant Summary collapse

GAP =
2

Constants inherited from Modal

Modal::PAD

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, ok: "OK", cancel: "Cancel", default: :cancel, theme: Theme::DEFAULT) ⇒ Confirm

Returns a new instance of Confirm.



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

def initialize(message, ok: "OK", cancel: "Cancel", default: :cancel, theme: Theme::DEFAULT)
  @message = DisplayText.new(message)
  @ok = button_text(ok)
  @cancel = button_text(cancel)
  @focus = default
  @theme = theme
end

Instance Attribute Details

#focusObject (readonly)

Returns the value of attribute focus.



13
14
15
# File 'lib/tui_tui/confirm.rb', line 13

def focus
  @focus
end

Instance Method Details

#draw(canvas, size) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/tui_tui/confirm.rb', line 45

def draw(canvas, size)
  inner = [@message.width, buttons_width].max
  rect, col = panel(canvas, inner: inner, body_rows: 3)
  canvas.text(rect.row + 1, col, @message.center(inner), theme.text)
  draw_buttons(canvas, rect.row + 3, col, inner)
  canvas
end

#handle(key) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/tui_tui/confirm.rb', line 23

def handle(key)
  case key
  when :left, :right, "\t", "h", "l"
    toggle
  when "\r", " "
    @focus
  when "y", "Y"
    :ok
  when "n", "N", :escape, KeyCode::CTRL_C
    :cancel
  end
end

#handle_mouse(event) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/tui_tui/confirm.rb', line 36

def handle_mouse(event)
  return nil unless event.action == :press && @buttons_row == event.row

  return :ok if hit?(event.col, @ok_at, @ok.width)
  return :cancel if hit?(event.col, @cancel_at, @cancel.width)

  nil
end