class Canis::CommandWindow

Creates a window at the bottom of the screen for some operations. Used for some operations such as:

- display a menu
- display some interactive text
- display some text

Attributes

config[R]
layout[R]
window[R]

Public Class Methods

new(form=nil, aconfig={}) click to toggle source
# File lib/canis/core/util/rcommandwindow.rb, line 138
def initialize form=nil, aconfig={}, &block  # --- {{{
  @config = aconfig
  @config.each_pair { |k,v| instance_variable_set("@#{k}",v) }
  instance_eval &block if block_given?
  if @layout.nil? 
      set_layout(1,Ncurses.COLS, -1, 0) 
  end
  @height = @layout[:height]
  @width = @layout[:width]
  @window = Canis::Window.new(@layout)
  @start = 0 # row for display of text with paging
  @list = []
  draw_box
  @window.wrefresh
  @panel = @window.panel
  Ncurses::Panel.update_panels
  @window.wrefresh
  @row_offset = 0
  if @box
    @row_offset = 1
  end
end

Public Instance Methods

OLDdestroy() click to toggle source
# File lib/canis/core/util/rcommandwindow.rb, line 297
def OLDdestroy
  $log.debug "DESTROY : rcommandwindow"
  if @window
    begin
      panel = @window.panel
      Ncurses::Panel.del_panel(panel.pointer) if panel
      @window.delwin
    rescue => exc
    end
  end
end
cget(param) click to toggle source
# File lib/canis/core/util/rcommandwindow.rb, line 271
def cget param
  @config[param]
end
clear() click to toggle source

clears the window, leaving the title line as is, from row 1 onwards

# File lib/canis/core/util/rcommandwindow.rb, line 315
def clear
  @window.wmove 1,1
  @window.wclrtobot
  #@window.box 0,0 if @box == :border
  draw_box
  # lower line of border will get erased currently since we are writing to
  # last line FIXME
end
configure(*val , &block) click to toggle source

might as well add more keys for paging.

# File lib/canis/core/util/rcommandwindow.rb, line 261
def configure(*val , &block)
  case val.size
  when 1
    return @config[val[0]]
  when 2
    @config[val[0]] = val[1]
    instance_variable_set("@#{val[0]}", val[1]) 
  end
  instance_eval &block if block_given?
end
destroy() click to toggle source
# File lib/canis/core/util/rcommandwindow.rb, line 294
def destroy
  @window.destroy
end
display_menu(list, options={}) click to toggle source

Displays list in a window at bottom of screen, if large then 2 or 3 columns. @param [Array] list of string to be displayed @param [Hash] configuration options: indexing and indexcolor indexing - can be letter or number. Anything else will be ignored, however

it will result in first letter being highlighted in indexcolor

indexcolor - color of mnemonic, default green

# File lib/canis/core/util/rcommandwindow.rb, line 339
def display_menu list, options={}  # --- {{{
  indexing = options[:indexing]
  #indexcolor = options[:indexcolor] || get_color($normalcolor, :yellow, :black)
  indexcolor = $datacolor || 7 # XXX above line crashing on choose()
  indexatt = Ncurses::A_BOLD
  #
  # the index to start from (used when scrolling a long menu such as file list)
  startindex = options[:startindex] || 0

  max_cols = 3 #  maximum no of columns, we will reduce based on data size
  l_succ = "`"
  act_height = @height
  if @box
    act_height = @height - 2
  end
  lh = list.size
  if lh < act_height
    $log.debug "DDD inside one window" if $log.debug? 
    list.each_with_index { |e, i| 
      text = e
      case e
      when Array
        text = e.first + " ..."
      end
      if indexing == :number
        mnem = i+1
        text = "%d. %s" % [i+1, text] 
      elsif indexing == :letter
        mnem = l_succ.succ!
        text = "%s. %s" % [mnem, text] 
      end
      @window.printstring i+@row_offset, 1, text, $normalcolor  
      if indexing
        @window.mvchgat(y=i+@row_offset, x=1, max=1, indexatt, indexcolor, nil)
      end
    }
  else
    $log.debug "DDD inside two window" if $log.debug? 
    row = 0
    h = act_height
    cols = (lh*1.0 / h).ceil
    cols = max_cols if cols > max_cols
    # sometimes elements are large like directory paths, so check size
    datasize = list.first.length
    if datasize > @width/3 # keep safety margin since checking only first row
      cols = 1
    elsif datasize > @width/2
      cols = [2,cols].min
    end
    adv = (@width/cols).to_i
    colct = 0
    col = 1
    $log.debug "DDDcols #{cols}, adv #{adv} size: #{lh} h: #{act_height} w #{@width} " if $log.debug? 
    list.each_with_index { |e, i| 
      text = e
      # signify that there's a deeper level
      case e
      when Array
        text = e.first + "..."
      end
      if indexing == :number
        mnem = i+1
        text = "%d. %s" % [mnem, text] 
      elsif indexing == :letter
        mnem = l_succ.succ!
        text = "%s. %s" % [mnem, text] 
      end
      # print only within range and window height
      #if i >= startindex && row < @window.actual_height
      if i >= startindex && row < @window.height
        $log.debug "XXX: MENU #{i} > #{startindex} row #{row} col #{col} "
        @window.printstring row+@row_offset, col, text, $normalcolor  
        if indexing
          @window.mvchgat(y=row+@row_offset, x=col, max=1, indexatt, indexcolor, nil)
        end
      colct += 1
      if colct == cols
        col = 1
        row += 1
        colct = 0
      else
        col += adv
      end
      end # startindex
    }
  end
  Ncurses::Panel.update_panels();
  Ncurses.doupdate();
  @window.wrefresh
end
draw_box() click to toggle source

draw the box, needed to redo this upon clear since clearing of windows was removing the top border 2014-05-04 - 20:14

# File lib/canis/core/util/rcommandwindow.rb, line 163
def draw_box
  if @box == :border
    @window.box 0,0
  elsif @box
    @window.attron(Ncurses.COLOR_PAIR($normalcolor) | Ncurses::A_REVERSE)

    # 2016-01-14 - replacing 1 with space since junk is showing up in some cases.
    space_char = " ".codepoints.first
    #@window.mvhline 0,0,1,@width
    @window.mvhline 0,0,space_char,@width
    @window.printstring 0,0,@title, $normalcolor #, 'normal' if @title
    @window.attroff(Ncurses.COLOR_PAIR($normalcolor) | Ncurses::A_REVERSE)
  else
    #@window.printstring 0,0,@title, $normalcolor,  'reverse' if @title
    title @title
  end
end
handle_keys() { |ch| ... } click to toggle source

todo handle mappings, so user can map keys TODO

# File lib/canis/core/util/rcommandwindow.rb, line 207
def handle_keys
  begin
    while((ch = @window.getchar()) != 999 )
      case ch
      when -1
        next
      else
        press ch
        break if @stop
        yield ch if block_given?
      end
    end
  ensure
    destroy  
  end
  return #@selected_index
end
hide() click to toggle source

this really helps if we are creating another window over this and we find the lower window still showing through. destroy does not often work so this clears current window. However, lower window may still have a black region. FIXME

# File lib/canis/core/util/rcommandwindow.rb, line 290
def hide
  @window.hide
  Window.refresh_all
end
press(ch) click to toggle source

handles a key, commandline

# File lib/canis/core/util/rcommandwindow.rb, line 226
def press ch 
  ch = ch.getbyte(0) if ch.class==String ## 1.9
  $log.debug " XXX press #{ch} " if $log.debug? 
  case ch
  when -1
    return
  when KEY_F1, 27, ?\C-q.getbyte(0)   
    @stop = true
    return
  when KEY_ENTER, 10, 13
    #$log.debug "popup ENTER : #{@selected_index} "
    #$log.debug "popup ENTER :  #{field.name}" if !field.nil?
    @stop = true
    return
  when ?\C-d.getbyte(0)
    @start += @height-1
    bounds_check
  when KEY_UP
    @start -= 1
    @start = 0 if @start < 0
  when KEY_DOWN
    @start += 1
    bounds_check
  when ?\C-b.getbyte(0)
    @start -= @height-1
    @start = 0 if @start < 0
  when 0
    @start = 0
  end
  Ncurses::Panel.update_panels();
  Ncurses.doupdate();
  @window.wrefresh
end
print_str(text, config={}) click to toggle source

not sure if this is really required. print_string is just fine. print a string. config can be :x :y :color_pair

refresh() click to toggle source

refresh whatevers painted onto the window

# File lib/canis/core/util/rcommandwindow.rb, line 309
def refresh
  Ncurses::Panel.update_panels();
  Ncurses.doupdate();
  @window.wrefresh
end
set_layout(height=0, width=0, top=0, left=0) click to toggle source
# File lib/canis/core/util/rcommandwindow.rb, line 275
def set_layout(height=0, width=0, top=0, left=0)
  # negative means top should be n rows from last line. -1 is last line
  if top < 0
    top = Ncurses.LINES-top
  end
  @layout = { :height => height, :width => width, :top => top, :left => left } 
  @height = height
  @width = width
end
show() click to toggle source
# File lib/canis/core/util/rcommandwindow.rb, line 284
def show
  @window.show
end
stopping?() click to toggle source

—- windowing functions {{{

message box
# File lib/canis/core/util/rcommandwindow.rb, line 202
def stopping?
  @stop
end
title(t=nil) click to toggle source

modify the window title, or get it if no params passed.

# File lib/canis/core/util/rcommandwindow.rb, line 326
def title t=nil  # --- {{{
  return @title unless t
  @title = t
  @window.printstring 0,0,@title, $normalcolor,  'reverse' if @title
end