class Umbra::Dialog

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`

Attributes

border_attr[RW]
border_color_pair[RW]
buttons[RW]
text[RW]
title[RW]
title_attr[RW]
title_color_pair[RW]
window_attr[RW]
window_color_pair[RW]

Public Class Methods

new(config={}) { |self| ... } click to toggle source

currently color and attr for text is missing. I think it should be what window has.

# 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

Public Instance Methods

key(ch) click to toggle source

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)

# File lib/umbra/dialog.rb, line 128
def key ch
  ch.getbyte(0)
end
paint_buttons(win, buttons, active_index) click to toggle source
# 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
run() click to toggle source
# 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

Private Instance Methods

_create_window() click to toggle source
# File lib/umbra/dialog.rb, line 54
        def _create_window
  text = @text || "Warning! Did not get text"
  #h = 7
  # increase height to 9 so we can put a fake button below text
  h = 9
  w = text.size + 20
  # don't exceed max
  w = [FFI::NCurses.COLS-10, w].min
  @window_color_pair ||= CP_BLACK
  @window_attr       ||= REVERSE
  win = create_centered_window h, w, @window_color_pair, @window_attr

  ## ---- border section --- {{{
  row = 1
  col = 2
  borderatt   = @border_attr || NORMAL
  bordercolor = @border_color_pair || CP_BLACK
  win.wattron(bordercolor | borderatt)
  print_border_mb win, row, col, win.height, win.width, nil, nil
  win.wattroff(bordercolor | borderatt)
  ## ---- border section --- }}}

  ## ---- title section ---- {{{
  @title            ||= "No title"
  @title_color_pair ||= CP_CYAN
  @title_attr       ||= REVERSE
  title = " "+@title+" "
  # normalcolor gives a white on black stark title like links and elinks
  # You can also do 'acolor' to give you a sober title that does not take attention away, like mc
  win.printstring(row=1,col=(w-title.length)/2,title, color=@title_color_pair, @title_attr)
  ## ---- title section ---- }}}

  # text can be longer than window. truncate or wrap
  # tet.size can be longer than w
  _col = (w-text.size)/2
  _col = 3 if _col < 3
  win.printstring 3, _col, text
  ## ---- button section ---- {{{
  paint_buttons win, @buttons, 0
  ## ---- button section ---- }}}
  @window = win
  win.wrefresh
end
variable_set(var, val) click to toggle source
# File lib/umbra/dialog.rb, line 51
        def variable_set var, val
  send("#{var}=", val) 
end