Class: Umbra::Dialog
- Inherits:
-
Object
- Object
- Umbra::Dialog
- Defined in:
- lib/umbra/dialog.rb
Overview
A simple dialog box that only displays a line of text, centered. It can take an array of button labels (just strings) and display them, and return the index of the button pressed, when closed. If no buttons are supplied, an “Ok” button is displayed. Minimum requirements are ‘text` and `title`
Instance Attribute Summary collapse
-
#border_attr ⇒ Object
attribute of border.
-
#border_color_pair ⇒ Object
color pair of border.
-
#buttons ⇒ Object
button text array.
-
#text ⇒ Object
text or message to print centered.
-
#title ⇒ Object
title of dialog.
-
#title_attr ⇒ Object
attribute of title.
-
#title_color_pair ⇒ Object
color pair of title.
-
#window_attr ⇒ Object
attribute of window.
-
#window_color_pair ⇒ Object
color pair of window.
Instance Method Summary collapse
-
#initialize(config = {}, &block) ⇒ Dialog
constructor
currently color and attr for text is missing.
-
#key(ch) ⇒ Object
convenience func to get int value of a key {{{ added 2014-05-05 instead of ?C-a.getbyte(0) use key(?C-a) or key(?a) or key(?M-x).
- #paint_buttons(win, buttons, active_index) ⇒ Object
-
#run ⇒ Object
}}}.
Constructor Details
#initialize(config = {}, &block) ⇒ Dialog
currently color and attr for text is missing. I think it should be what window has.
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/umbra/dialog.rb', line 38 def initialize config={}, &block config.each_pair { |k,v| variable_set(k,v) } if block_given? if block.arity > 0 yield self else self.instance_eval(&block) end end @title ||= "Alert" @buttons ||= ["Ok"] end |
Instance Attribute Details
#border_attr ⇒ Object
attribute of border
33 34 35 |
# File 'lib/umbra/dialog.rb', line 33 def border_attr @border_attr end |
#border_color_pair ⇒ Object
color pair of border
32 33 34 |
# File 'lib/umbra/dialog.rb', line 32 def border_color_pair @border_color_pair end |
#buttons ⇒ Object
button text array
34 35 36 |
# File 'lib/umbra/dialog.rb', line 34 def @buttons end |
#text ⇒ Object
text or message to print centered
26 27 28 |
# File 'lib/umbra/dialog.rb', line 26 def text @text end |
#title ⇒ Object
title of dialog
27 28 29 |
# File 'lib/umbra/dialog.rb', line 27 def title @title end |
#title_attr ⇒ Object
attribute of title
29 30 31 |
# File 'lib/umbra/dialog.rb', line 29 def title_attr @title_attr end |
#title_color_pair ⇒ Object
color pair of title
28 29 30 |
# File 'lib/umbra/dialog.rb', line 28 def title_color_pair @title_color_pair end |
#window_attr ⇒ Object
attribute of window
31 32 33 |
# File 'lib/umbra/dialog.rb', line 31 def window_attr @window_attr end |
#window_color_pair ⇒ Object
color pair of window
30 31 32 |
# File 'lib/umbra/dialog.rb', line 30 def window_color_pair @window_color_pair end |
Instance Method Details
#key(ch) ⇒ Object
convenience func to get int value of a key {{{ added 2014-05-05 instead of ?C-a.getbyte(0) use key(?C-a) or key(?a) or key(?M-x)
128 129 130 |
# File 'lib/umbra/dialog.rb', line 128 def key ch ch.getbyte(0) end |
#paint_buttons(win, buttons, active_index) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/umbra/dialog.rb', line 97 def win, , active_index brow = 6 bcol = (win.width-(.size*10))/2 origbcol = bcol FFI::NCurses.mvwhline(win.pointer, brow-1, 3, FFI::NCurses::ACS_HLINE, win.width-6) #@button_color ||= create_color_pair(COLOR_BLACK, COLOR_MAGENTA) @button_color ||= CP_BLACK active_color = create_color_pair(COLOR_BLACK, COLOR_MAGENTA) #active_color = create_color_pair(COLOR_MAGENTA, COLOR_BLACK) active_col = bcol .each_with_index do |, ix| = NORMAL = @button_color = "[ #{} ]" if ix == active_index = BOLD = active_color active_col = bcol = "> #{} <" end win.printstring brow, bcol, , , bcol += 10 end FFI::NCurses.wmove(win.pointer, brow, active_col+2) end |
#run ⇒ Object
}}}
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/umbra/dialog.rb', line 131 def run _create_window unless @window win = @window = @buttons.count = 0 begin while (ch = win.getkey) != FFI::NCurses::KEY_RETURN begin next if ch == -1 break if ch == 32 or key(?q) == ch # go to next button if right or down or TAB pressed if ch == FFI::NCurses::KEY_TAB or ch == FFI::NCurses::KEY_RIGHT or FFI::NCurses::KEY_DOWN += 1 elsif ch == FFI::NCurses::KEY_LEFT or FFI::NCurses::KEY_UP -= 1 else # should check against first char of buttons TODO #puts "Don't know #{ch}" end = 0 if > -1 = -1 if < 0 win, @buttons, rescue => e puts e puts e.backtrace.join("\n") end win.wrefresh end ensure win.destroy end #FFI::NCurses.endwin # don't think this should be here if popped up by another window return end |