Class: Canis::KeyLabelPrinter

Inherits:
Widget show all
Defined in:
lib/canis/core/widgets/keylabelprinter.rb

Overview

This paints labels for various keys at the bottom of the screen, in 2 rows. This is based on alpines last 2 rows. Modes are supported so that the labels change as you enter a widget. For an example, see dbdemo.rb or rfe.rb NOTE: applications using ‘App’ use a shortcut “dock” to create this.

The most minimal keylabel to print one label in first row, and none in second is:

[["F1", "Help"], nil]

To print 2 labels, one over the other:

[["F1", "Help"], ["F10", "Quit"]]

Instance Attribute Summary collapse

Attributes inherited from Widget

#_object_created, #col_offset, #config, #curpos, #focussed, #form, #handler, #id, #key_label, #parent_component, #row_offset, #state

Instance Method Summary collapse

Methods inherited from Widget

#action_manager, #bgcolor, #color, #color_pair, #command, #destroy, #focus, #focusable, #focusable?, #getvalue_for_paint, #handle_key, #hide, #init_vars, #modified?, #move, #on_enter, #on_leave, #override_graphic, #process_key, #property_set, #remove, #repaint_all, #repaint_required, #rowcol, #set_form, #set_form_col, #set_form_row, #set_modified, #setformrowcol, #setrowcol, #show, #unbind_key

Methods included from Io

#__create_footer_window, #clear_this, #get_file, #print_this, #rb_getchar, #rb_gets, #rb_getstr, #warn

Methods included from Utils

#ORIG_process_key, #ORIGbind_key, #ORIGkeycode_tos, #_process_key, #bind_composite_mapping, #bind_key, #bind_keys, #check_composite_mapping, #create_logger, #define_key, #define_prefix_command, #execute_mapping, #get_attrib, #get_color, #key, #key_tos, #print_key_bindings, #repeatm, #run_command, #shell_out, #shell_output, #suspend, #view, #xxxbind_composite_mapping

Methods included from ConfigSetup

#config_setup, #variable_set

Methods included from EventHandler

#bind, #event?, #event_list, #fire_handler, #fire_property_change, #register_events

Constructor Details

#initialize(form, key_labels, config = {}, &block) ⇒ KeyLabelPrinter

attr_accessor :row_relative # lets only advertise this when we’ve tested it out



28
29
30
31
32
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
# File 'lib/canis/core/widgets/keylabelprinter.rb', line 28

def initialize form, key_labels, config={}, &block

  @name = "dock"
  case key_labels
  when Hash
    raise "KeyLabelPrinter: KeyLabels cannot be a hash, Array of key labels required. Perhaps you did not pass labels"
  when Array
  else
    raise "KeyLabelPrinter: Array of key labels required. Perhaps you did not pass labels"
  end
  super form, config, &block
  @mode ||= :normal
  #@key_labels = key_labels
  @key_hash = {}
  @key_hash[@mode] = key_labels
  @editable = false
  @focusable = false
  unless @row
    @row_relative = -2
    @row = Ncurses.LINES + @row_relative
  end
  @col ||= 0
  # if negativ row passed we store as relative to bottom, so we can maintain that.
  if @row < 0
    @row_relative = @row
    @row = Ncurses.LINES - @row
  else
    @row_relative = (Ncurses.LINES - @row) * -1
  end
  @cols ||= Ncurses.COLS-1
  @repaint_required = true
  @footer_color_pair ||= $bottomcolor
  @footer_mnemonic_color_pair ||= $reversecolor #2
end

Instance Attribute Details

#key_labels(mode = @mode) ⇒ Object (readonly)

Returns the value of attribute key_labels.



18
19
20
# File 'lib/canis/core/widgets/keylabelprinter.rb', line 18

def key_labels
  @key_labels
end

Instance Method Details

#append_key_label(key, label, mode = @mode) ⇒ Object

?? does not use mode, i think key_labels is unused. a hash is now used 2011-10-11 XXX FIXME WARNING, i have not tested this after changing it.



96
97
98
99
100
# File 'lib/canis/core/widgets/keylabelprinter.rb', line 96

def append_key_label key, label, mode=@mode
  #@key_labels << [key, label] if !@key_labels.include? [key, label]
  @key_hash[mode] << [key, label] if !@key_hash[mode].include? [key, label]
  @repaint_required = true
end

#get_current_keysObject

returns the keys as printed. these may or may not help in validation depedign on what you passed as zeroth index



67
68
69
70
71
72
73
# File 'lib/canis/core/widgets/keylabelprinter.rb', line 67

def get_current_keys
  a = []
  @key_hash[@mode].each do |arr|
    a << arr[0] unless arr.nil?
  end
  return a
end

#getvalueObject



74
75
76
# File 'lib/canis/core/widgets/keylabelprinter.rb', line 74

def getvalue
  @key_hash
end

#insert_application_key_label(index, display_code, text) ⇒ Object

inserts an application label at given index to add the key, use create_datakeys to add bindings remember to call restore_application_key_labels after updating/inserting



187
188
189
190
191
# File 'lib/canis/core/widgets/keylabelprinter.rb', line 187

def insert_application_key_label(index, display_code, text)
  @repaint_required = true
  labels = key_labels()
  labels.insert(index, [display_code , text] )
end


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/canis/core/widgets/keylabelprinter.rb', line 101

def print_key_labels(arr = key_labels(), mode=@mode)
  #return if !@show_key_labels # XXX
  @win ||= @form.window
  #$log.debug "XXX: PKL #{arr.length}, #{arr}"
  @padding = @cols / (arr.length/2)
  posx = 0
  even = []
  odd = []
  arr.each_index { |i|
    if i % 2 == 0
      #arr[i+1] = ['',''] if arr[i+1].nil?
      nextarr = arr[i+1] || ['', '']
      keyw = [arr[i][0].length, nextarr[0].length].max
      labelw = [arr[i][1].length, nextarr[1].length].max

      even << [ sprintf("%*s", keyw,  arr[i][0]), sprintf("%-*s", labelw,  arr[i][1]) ]
      odd << [ sprintf("%*s", keyw,  nextarr[0]), sprintf("%-*s", labelw,  nextarr[1]) ]
      #$log.debug("loop even: #{even.inspect}")
    else
    end
  }
  #$log.debug("even: #{even.inspect}")
  #$log.debug("odd : #{odd.inspect}")
  #posy = @barrow-1
  posy = @row
  print_key_labels_row(posy, posx, even)
  posy = @row+1
  print_key_labels_row(posy, posx, odd)
  # uncommented next line after ffi-ncurses else not showing till key press FFI 2011-09-17 
  @win.wrefresh   # needed else secod row not shown after askchoice XXX 
end

needed else secod row not shown after askchoice XXX



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
# File 'lib/canis/core/widgets/keylabelprinter.rb', line 132

def print_key_labels_row(posy, posx, arr)
  # FIXME: this logic of padding needs to take into account
  # width of window
  padding = 8
  padding = 4 if arr.length > 5
  padding = 2 if arr.length > 7
  padding = 0 if arr.length > 9
  #padding = @padding # XXX 2008-11-13 23:01 
  my_form_win = @win
  @win.printstring(posy,0, "%-*s" % [@cols," "], @footer_color_pair, @attr)
  arr.each do |kl|
    key = kl[0]
    lab = kl[1]
    if key !="" # don't print that white blank space for fillers
      color_pair= @footer_mnemonic_color_pair # $reversecolor #2
      x = posx +  (key.length - key.strip.length)
      my_form_win.attron(Ncurses.COLOR_PAIR(color_pair))
      my_form_win.mvprintw(posy, x, "%s" % kl[0].strip );
      my_form_win.attroff(Ncurses.COLOR_PAIR(color_pair))
    end
    color_pair=@footer_color_pair
    posx = posx + kl[0].length 
    my_form_win.attron(Ncurses.COLOR_PAIR(color_pair))

    #lab = sprintf(" %s %*s" , kl[1], padding, " ");
    lab = sprintf(" %s %s" , kl[1], " "*padding);
    my_form_win.mvprintw(posy, posx, lab)
    my_form_win.attroff(Ncurses.COLOR_PAIR(color_pair))
    posx = posx +  lab.length
  end
end

#repaintObject

XXX need to move wrapping etc up and done once.



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/canis/core/widgets/keylabelprinter.rb', line 83

def repaint
  return unless @repaint_required
  r,c = rowcol
  # this should only happen if there's a change in window
  if @row_relative
    @row = Ncurses.LINES+@row_relative
  end
  arr = key_labels()
  print_key_labels(arr, mode=@mode)
  @repaint_required = false
end

#set_key_labels(_key_labels, mode = :normal) ⇒ Object



77
78
79
# File 'lib/canis/core/widgets/keylabelprinter.rb', line 77

def set_key_labels _key_labels, mode=:normal
  @key_hash[mode] = _key_labels
end

#update_application_key_label(display_code, new_display_code, text) ⇒ Object Also known as: update

updates existing label with a new one.

Examples:

update “C-x”, “C-x”, “Disable”

Returns:

  • true if updated, else false



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/canis/core/widgets/keylabelprinter.rb', line 167

def update_application_key_label(display_code, new_display_code, text)
  @repaint_required = true
  labels = key_labels()
  raise "labels are nil !!!" unless labels
  labels.each_index do |ix|
    lab = labels[ix]
    next if lab.nil?
    if lab[0] == display_code
      labels[ix] = [new_display_code , text]
      $log.debug("updated #{labels[ix]}")
      return true
    end
  end
  return false
end