Class: Legion::TTY::Components::Notification
- Inherits:
-
Object
- Object
- Legion::TTY::Components::Notification
- Defined in:
- lib/legion/tty/components/notification.rb
Constant Summary collapse
- LEVELS =
%i[info success warning error].freeze
- ICONS =
{ info: 'i', success: '+', warning: '!', error: 'x' }.freeze
- COLORS =
{ info: :info, success: :success, warning: :warning, error: :error }.freeze
Instance Attribute Summary collapse
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
Instance Method Summary collapse
- #expired? ⇒ Boolean
-
#initialize(message:, level: :info, ttl: 5) ⇒ Notification
constructor
A new instance of Notification.
- #render(width: 80) ⇒ Object
Constructor Details
#initialize(message:, level: :info, ttl: 5) ⇒ Notification
Returns a new instance of Notification.
15 16 17 18 19 20 |
# File 'lib/legion/tty/components/notification.rb', line 15 def initialize(message:, level: :info, ttl: 5) @message = @level = LEVELS.include?(level) ? level : :info @ttl = ttl @created_at = Time.now end |
Instance Attribute Details
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
13 14 15 |
# File 'lib/legion/tty/components/notification.rb', line 13 def created_at @created_at end |
#level ⇒ Object (readonly)
Returns the value of attribute level.
13 14 15 |
# File 'lib/legion/tty/components/notification.rb', line 13 def level @level end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
13 14 15 |
# File 'lib/legion/tty/components/notification.rb', line 13 def @message end |
Instance Method Details
#expired? ⇒ Boolean
22 23 24 |
# File 'lib/legion/tty/components/notification.rb', line 22 def expired? Time.now - @created_at > @ttl end |
#render(width: 80) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/legion/tty/components/notification.rb', line 26 def render(width: 80) icon = Theme.c(COLORS[@level], ICONS[@level]) text = Theme.c(COLORS[@level], @message) line = "#{icon} #{text}" plain_len = strip_ansi(line).length line + (' ' * [width - plain_len, 0].max) end |