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

AUTO_DISMISS_SECONDS =
20.0
MAX_HEIGHT_FRACTION =

Flash area occupies at most 1/3 of the chat pane height.

3
LEVEL_STYLES =
{
  error: {fg: "white", bg: "red", icon: " \u2718 "},
  warning: {fg: "black", bg: "yellow", icon: " \u26A0 "},
  info: {fg: "white", bg: "blue", icon: " \u2139 "}
}.freeze

Instance Method Summary collapse

Constructor Details

#initializeFlash

Returns a new instance of Flash.



38
39
40
# File 'lib/tui/flash.rb', line 38

def initialize
  @entries = []
end

Instance Method Details

#any?Boolean

Removes expired entries and returns true if any remain.

Returns:

  • (Boolean)


58
59
60
61
# File 'lib/tui/flash.rb', line 58

def any?
  expire!
  @entries.any?
end

#dismiss!Object

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



69
70
71
# File 'lib/tui/flash.rb', line 69

def dismiss!
  @entries.clear
end

#empty?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/tui/flash.rb', line 64

def empty?
  !any?
end

#error(message) ⇒ Object

Parameters:

  • message (String)


43
44
45
# File 'lib/tui/flash.rb', line 43

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

#info(message) ⇒ Object

Parameters:

  • message (String)


53
54
55
# File 'lib/tui/flash.rb', line 53

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



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

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

  height = [@entries.size, area.height / 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)


48
49
50
# File 'lib/tui/flash.rb', line 48

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