Class: TUI::Flash

Inherits:
Object
  • Object
show all
Defined in:
lib/tui/flash.rb

Overview

Ephemeral notification system for the TUI, modeled after Rails flash messages. Notifications render as a colored bar at the top of the chat pane and auto-dismiss after a configurable timeout or on any keypress.

Reusable beyond Bounce Back — useful for connection status changes, background task notifications, and any transient user feedback that doesn’t belong in the chat stream.

Examples:

Adding a flash

flash = TUI::Flash.new
flash.error("Message not delivered: API token not configured")
flash.warning("Rate limited, retry in 30s")
flash.info("Reconnected to server")

Rendering (returns height consumed)

flash_height = flash.render(frame, area, tui)

Dismissing

flash.dismiss!

Defined Under Namespace

Classes: Entry

Constant Summary collapse

LEVEL_ICONS =
{error: " \u2718 ", warning: " \u26A0 ", info: " \u2139 "}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFlash

Returns a new instance of Flash.



41
42
43
# File 'lib/tui/flash.rb', line 41

def initialize
  @entries = []
end

Class Method Details

.level_stylesObject

Builds level styles from current theme settings. Called per-render so hot-reloaded theme changes take effect immediately.



33
34
35
36
37
38
39
# File 'lib/tui/flash.rb', line 33

def self.level_styles
  {
    error: {fg: Settings.theme_flash_error_fg, bg: Settings.theme_flash_error_bg},
    warning: {fg: Settings.theme_flash_warning_fg, bg: Settings.theme_flash_warning_bg},
    info: {fg: Settings.theme_flash_info_fg, bg: Settings.theme_flash_info_bg}
  }
end

Instance Method Details

#any?Boolean

Removes expired entries and returns true if any remain.

Returns:

  • (Boolean)


61
62
63
64
# File 'lib/tui/flash.rb', line 61

def any?
  expire!
  @entries.any?
end

#dismiss!Object

Removes all entries immediately (e.g. on keypress).



72
73
74
# File 'lib/tui/flash.rb', line 72

def dismiss!
  @entries.clear
end

#empty?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/tui/flash.rb', line 67

def empty?
  !any?
end

#error(message) ⇒ Object

Parameters:

  • message (String)


46
47
48
# File 'lib/tui/flash.rb', line 46

def error(message)
  push(message, :error)
end

#info(message) ⇒ Object

Parameters:

  • message (String)


56
57
58
# File 'lib/tui/flash.rb', line 56

def info(message)
  push(message, :info)
end

#render(frame, area, tui) ⇒ Integer

Renders flash entries as colored bars at the top of the given area. Returns the height consumed so the caller can adjust layout.

Parameters:

  • frame (RatatuiRuby::Frame)
  • area (RatatuiRuby::Rect)

    full chat area (flash renders at top)

  • tui (RatatuiRuby::TUI)

Returns:

  • (Integer)

    number of rows consumed



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/tui/flash.rb', line 83

def render(frame, area, tui)
  expire!
  return 0 if @entries.empty?

  height = [@entries.size, area.height / Settings.flash_max_height_fraction].min

  flash_area, _ = tui.split(
    area,
    direction: :vertical,
    constraints: [
      tui.constraint_length(height),
      tui.constraint_fill(1)
    ]
  )

  @entries.each_with_index do |entry, index|
    break if index >= height

    row_area = row_rect(flash_area, index, tui)
    render_entry(frame, row_area, entry, tui)
  end

  height
end

#warning(message) ⇒ Object

Parameters:

  • message (String)


51
52
53
# File 'lib/tui/flash.rb', line 51

def warning(message)
  push(message, :warning)
end