Class: Potty::Widgets::FlashMessage

Inherits:
Base
  • Object
show all
Defined in:
lib/potty/widgets/flash_message.rb

Overview

Temporary notification message

Instance Attribute Summary collapse

Attributes inherited from Base

#app, #focused, #parent, #rect

Instance Method Summary collapse

Methods inherited from Base

#activate, #blur, #can_focus?, #deactivate, #focus, #handle_escape, #handle_key, #hide, #layout, #on_blur, #on_focus, #on_layout, #theme, #tick, #visible=, #visible?

Methods included from Events

#emit, #listeners?, #off, #on

Constructor Details

#initialize(app) ⇒ FlashMessage

Returns a new instance of FlashMessage.



11
12
13
14
15
16
# File 'lib/potty/widgets/flash_message.rb', line 11

def initialize(app)
  super
  @message = nil
  @type = :info
  @timeout = nil
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



9
10
11
# File 'lib/potty/widgets/flash_message.rb', line 9

def message
  @message
end

#typeObject (readonly)

Returns the value of attribute type.



9
10
11
# File 'lib/potty/widgets/flash_message.rb', line 9

def type
  @type
end

Instance Method Details

#clearObject



24
25
26
27
# File 'lib/potty/widgets/flash_message.rb', line 24

def clear
  @message = nil
  @timeout = nil
end

#preferred_height(width) ⇒ Object



29
30
31
# File 'lib/potty/widgets/flash_message.rb', line 29

def preferred_height(width)
  1
end

#render(window) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/potty/widgets/flash_message.rb', line 33

def render(window)
  return unless @rect

  # Auto-clear if timed out
  if @message && @timeout && Time.now > @timeout
    clear
  end

  # Clear the line first
  window.setpos(@rect.y, @rect.x)
  window.addstr(" " * @rect.width)

  # Show message if present
  if @message
    attr = case @type
           when :success then theme[:success]
           when :error then theme[:error]
           when :warning then theme[:warning]
           else theme[:info]
           end

    window.setpos(@rect.y, @rect.x)
    window.attron(attr) do
      prefix = case @type
               when :success then "\u2713 "
               when :error then "\u2717 "
               when :warning then "\u26A0 "
               else "\u2139 "
               end
      text = "#{prefix}#{@message}"[0, @rect.width]
      window.addstr(text)
    end
  end
end

#show(message, type: :info, timeout: 5) ⇒ Object



18
19
20
21
22
# File 'lib/potty/widgets/flash_message.rb', line 18

def show(message, type: :info, timeout: 5)
  @message = message
  @type = type
  @timeout = Time.now + timeout if timeout
end