Class: Legion::TTY::Components::Notification

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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 = message
  @level = LEVELS.include?(level) ? level : :info
  @ttl = ttl
  @created_at = Time.now
end

Instance Attribute Details

#created_atObject (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

#levelObject (readonly)

Returns the value of attribute level.



13
14
15
# File 'lib/legion/tty/components/notification.rb', line 13

def level
  @level
end

#messageObject (readonly)

Returns the value of attribute message.



13
14
15
# File 'lib/legion/tty/components/notification.rb', line 13

def message
  @message
end

Instance Method Details

#expired?Boolean

Returns:

  • (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