Class: TUI::App

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

Constant Summary collapse

SCREENS =
%i[chat].freeze
COMMAND_KEYS =
{
  "n" => :new_session,
  "v" => :view_mode,
  "q" => :quit
}.freeze
COMMAND_KEYS.map { |key, action| "[#{key}] #{action.to_s.tr("_", " ").capitalize}" }.freeze
28
STATUS_STYLES =

Connection status display styles

{
  disconnected: {label: " DISCONNECTED ", fg: "white", bg: "red"},
  connecting: {label: " CONNECTING ", fg: "black", bg: "yellow"},
  connected: {label: " CONNECTED ", fg: "black", bg: "yellow"},
  subscribed: {label: " CONNECTED ", fg: "black", bg: "green"},
  reconnecting: {label: " RECONNECTING ", fg: "black", bg: "yellow"}
}.freeze
SHUTDOWN_SIGNALS =

Signals that trigger graceful shutdown when received from the OS.

%w[HUP TERM INT].freeze
TERMINAL_CHECK_INTERVAL =

How often the watchdog thread checks if the controlling terminal is alive.

See Also:

  • #terminal_watchdog_loop
0.5
CONTROLLING_TERMINAL =

Unix controlling terminal device path.

See Also:

  • #terminal_watchdog_loop
"/dev/tty"
WATCHDOG_SHUTDOWN_TIMEOUT =

Grace period for watchdog thread to exit before force-killing it.

1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cable_client:) ⇒ App

Returns a new instance of App.

Parameters:



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tui/app.rb', line 49

def initialize(cable_client:)
  @cable_client = cable_client
  @current_screen = :chat
  @command_mode = false
  @shutdown_requested = false
  @previous_signal_handlers = {}
  @watchdog_thread = nil
  @screens = {
    chat: Screens::Chat.new(cable_client: cable_client)
  }
end

Instance Attribute Details

#command_modeObject (readonly)

Returns the value of attribute command_mode.



44
45
46
# File 'lib/tui/app.rb', line 44

def command_mode
  @command_mode
end

#current_screenObject (readonly)

Returns the value of attribute current_screen.



44
45
46
# File 'lib/tui/app.rb', line 44

def current_screen
  @current_screen
end

#shutdown_requestedBoolean (readonly)

Returns true when graceful shutdown has been requested via signal.

Returns:

  • (Boolean)

    true when graceful shutdown has been requested via signal



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

def shutdown_requested
  @shutdown_requested
end

Instance Method Details

#runObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/tui/app.rb', line 61

def run
  install_signal_handlers
  start_terminal_watchdog
  RatatuiRuby.run do |tui|
    loop do
      break if @shutdown_requested

      tui.draw { |frame| render(frame, tui) }

      event = tui.poll_event(timeout: 0.1)
      break if @shutdown_requested
      next if event.nil? || event.none?
      break if handle_event(event) == :quit
    end
  end
ensure
  stop_terminal_watchdog
  restore_signal_handlers
  @cable_client.disconnect
end