Class: TuiTui::Toast
- Inherits:
-
Object
- Object
- TuiTui::Toast
- Defined in:
- lib/tui_tui/toast.rb
Overview
A transient notification overlay. The clock is injectable so expiry is testable without sleeping.
Constant Summary collapse
- DEFAULT_SECONDS =
2.0- DEFAULT_STYLE =
Style.new(attrs: [:reverse])
- DEFAULT_POSITION =
:bottom_center- POSITIONS =
{ top_left: [:top, :left], top_center: [:top, :center], top_right: [:top, :right], middle_left: [:middle, :left], middle_center: [:middle, :center], middle_right: [:middle, :right], bottom_left: [:bottom, :left], bottom_center: [:bottom, :center], bottom_right: [:bottom, :right], center: [:middle, :center] }.freeze
- MONOTONIC =
-> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }
Instance Method Summary collapse
- #draw(canvas, size, style: DEFAULT_STYLE, position: @position) ⇒ Object
- #expired? ⇒ Boolean
-
#initialize(message, seconds: DEFAULT_SECONDS, position: DEFAULT_POSITION, clock: MONOTONIC) ⇒ Toast
constructor
A new instance of Toast.
Constructor Details
#initialize(message, seconds: DEFAULT_SECONDS, position: DEFAULT_POSITION, clock: MONOTONIC) ⇒ Toast
Returns a new instance of Toast.
27 28 29 30 31 32 33 |
# File 'lib/tui_tui/toast.rb', line 27 def initialize(, seconds: DEFAULT_SECONDS, position: DEFAULT_POSITION, clock: MONOTONIC) @message = DisplayText.new() @position = position @clock = clock @expires_at = clock.call + seconds validate_position! end |
Instance Method Details
#draw(canvas, size, style: DEFAULT_STYLE, position: @position) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/tui_tui/toast.rb', line 37 def draw(canvas, size, style: DEFAULT_STYLE, position: @position) return canvas if expired? label = DisplayText.new(" #{@message} ").truncate(size.cols) vertical, horizontal = position_parts(position) row = row_for(size, vertical) col = col_for(size, label.width, horizontal) canvas.text(row, col, label, style) canvas end |
#expired? ⇒ Boolean
35 |
# File 'lib/tui_tui/toast.rb', line 35 def expired? = @clock.call >= @expires_at |