Class: Umbra::Dialog

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

Instance Method Summary collapse

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_attrObject

attribute of border



33
34
35
# File 'lib/umbra/dialog.rb', line 33

def border_attr
  @border_attr
end

#border_color_pairObject

color pair of border



32
33
34
# File 'lib/umbra/dialog.rb', line 32

def border_color_pair
  @border_color_pair
end

#buttonsObject

button text array



34
35
36
# File 'lib/umbra/dialog.rb', line 34

def buttons
  @buttons
end

#textObject

text or message to print centered



26
27
28
# File 'lib/umbra/dialog.rb', line 26

def text
  @text
end

#titleObject

title of dialog



27
28
29
# File 'lib/umbra/dialog.rb', line 27

def title
  @title
end

#title_attrObject

attribute of title



29
30
31
# File 'lib/umbra/dialog.rb', line 29

def title_attr
  @title_attr
end

#title_color_pairObject

color pair of title



28
29
30
# File 'lib/umbra/dialog.rb', line 28

def title_color_pair
  @title_color_pair
end

#window_attrObject

attribute of window



31
32
33
# File 'lib/umbra/dialog.rb', line 31

def window_attr
  @window_attr
end

#window_color_pairObject

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 paint_buttons win, buttons, active_index
  brow   = 6
  bcol   = (win.width-(buttons.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
  buttons.each_with_index do |button, ix|
    button_attr  = NORMAL
    button_color = @button_color
    _button = "[ #{button} ]"
    if ix == active_index
      button_attr = BOLD
      button_color = active_color
      active_col = bcol
      _button = "> #{button} <"
    end
    win.printstring brow, bcol, _button, button_color, button_attr
    bcol += 10
  end
  FFI::NCurses.wmove(win.pointer, brow, active_col+2)
end

#runObject

}}}



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
  buttoncount = @buttons.count
  buttonindex = 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
          buttonindex += 1
        elsif ch == FFI::NCurses::KEY_LEFT or FFI::NCurses::KEY_UP
          buttonindex -= 1
        else
          # should check against first char of buttons TODO
          #puts "Don't know #{ch}"
        end
        buttonindex = 0 if buttonindex > buttoncount-1
        buttonindex = buttoncount-1 if buttonindex < 0
        paint_buttons win, @buttons, buttonindex
      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 buttonindex
end