2023-07-05 12:35:20 -06:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Class for creating and displaying alerts in the Curses TUI. This class also manages a little bit of concurrency
|
|
|
|
# We use mutex for sync so we don't break Curses, as Curses isn't thread safe
|
|
|
|
class Alert
|
2023-07-05 16:25:26 -06:00
|
|
|
attr_reader :message, :severity, :alert_window
|
2023-07-05 12:35:20 -06:00
|
|
|
|
|
|
|
def initialize(message, severity)
|
|
|
|
@message = message
|
|
|
|
@severity = severity
|
|
|
|
@curses_mutex = Mutex.new
|
|
|
|
end
|
|
|
|
|
2023-07-05 16:25:26 -06:00
|
|
|
def display # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
2023-07-05 12:35:20 -06:00
|
|
|
@curses_mutex.synchronize do
|
|
|
|
# Initialize color pairs
|
|
|
|
Curses.start_color
|
|
|
|
Curses.init_pair(1, Curses::COLOR_BLUE, Curses::COLOR_BLACK) # Info
|
|
|
|
Curses.init_pair(2, Curses::COLOR_RED, Curses::COLOR_BLACK) # Error
|
|
|
|
Curses.init_pair(3, Curses::COLOR_YELLOW, Curses::COLOR_BLACK) # Warning
|
|
|
|
|
|
|
|
# Create a new window for the alert at the bottom of the screen
|
2023-07-05 16:25:26 -06:00
|
|
|
@alert_window = Curses::Window.new(1, Curses.cols, Curses.lines - 1, 0)
|
2023-07-05 12:35:20 -06:00
|
|
|
|
|
|
|
# Set the color attribute based on the severity of the alert
|
|
|
|
case @severity
|
|
|
|
when :info
|
2023-07-05 16:25:26 -06:00
|
|
|
@alert_window.attron(Curses.color_pair(1) | Curses::A_NORMAL) # Blue color
|
2023-07-05 12:35:20 -06:00
|
|
|
when :warning
|
2023-07-05 16:25:26 -06:00
|
|
|
@alert_window.attron(Curses.color_pair(3) | Curses::A_NORMAL) # Yellow color
|
2023-07-05 12:35:20 -06:00
|
|
|
when :error
|
2023-07-05 16:25:26 -06:00
|
|
|
@alert_window.attron(Curses.color_pair(2) | Curses::A_NORMAL) # Red color
|
2023-07-05 12:35:20 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
# Add the message to the window and refresh it to display the message
|
2023-07-05 16:25:26 -06:00
|
|
|
@alert_window.addstr(@message)
|
|
|
|
@alert_window.refresh
|
|
|
|
end
|
|
|
|
end
|
2023-07-05 12:35:20 -06:00
|
|
|
|
2023-07-05 16:25:26 -06:00
|
|
|
def clear
|
|
|
|
@curses_mutex.synchronize do
|
|
|
|
# Clear the alert
|
|
|
|
@alert_window.clear
|
|
|
|
@alert_window.refresh
|
|
|
|
@alert_window.close
|
2023-07-05 12:35:20 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|