class Canis::ControlPHandler
This is a keyhandler that traps some keys, much like control-p which filters down a list based on some alpha numeric chars. The rest, such as arrow-keys, are passed to the key_map
Attributes
string the user is currently entering in (pattern to filter on)
specify command to requery data
application_header object whose text can be changed
Public Class Methods
# File lib/canis/core/util/rcommandwindow.rb, line 775 def initialize source @source = source @list = source.text # backup of data to refilter from if no command given to update @__list = @list @buffer = "" @maxht ||=15 default_string_key_map default_key_map @no_match = false end
Public Instance Methods
signal that the user has added or deleted a char from the pattern and data should be requeried, etc
# File lib/canis/core/util/rcommandwindow.rb, line 842 def buffer_changed # display the pattern on the header @header.text1(">>>#{@buffer}_") if @header @header.text_right(Dir.pwd) if @header @no_match = false if @command @list = @command.call(@buffer) else @list = @__list.select do |line| line.index @buffer end end sz = @list.size if sz == 0 Ncurses.beep #return 1 #this should make ENTER and arrow keys unusable except for BS or Esc, @list = ["No entries"] @no_match = true end data_changed @list 0 end
the line on which focus is.
# File lib/canis/core/util/rcommandwindow.rb, line 932 def current_value @source.current_value end
signal that the data has changed and should be redisplayed with window resizing etc.
# File lib/canis/core/util/rcommandwindow.rb, line 800 def data_changed list sz = list.size @source.text(list) wh = @source.form.window.height @source.form.window.hide th = @source.height sh = Ncurses.LINES-1 if sz < @maxht # rows is less than tp size so reduce tp and window @source.height = sz nl = _new_layout sz+1 $log.debug "XXX: adjust ht to #{sz} layout is #{nl} size is #{sz}" @source.form.window.resize_with(nl) #Window.refresh_all else # expand the window ht to maxht tt = @maxht-1 @source.height = tt nl = _new_layout tt+1 $log.debug "XXX: increase ht to #{tt} def layout is #{nl} size is #{sz}" @source.form.window.resize_with(nl) end @source.fire_dimension_changed @source.init_vars # do if rows is less than current_index. @source.set_form_row @source.form.window.show #Window.refresh_all @source.form.window.wrefresh Ncurses::Panel.update_panels(); Ncurses.doupdate(); end
setting up some keys
# File lib/canis/core/util/rcommandwindow.rb, line 903 def default_key_map tp = source source.bind_key(?\M-n.getbyte(0), 'goto_end'){ tp.goto_end } source.bind_key(?\M-p.getbyte(0), 'goto_start'){ tp.goto_start } end
specific actions for directory listers currently for stepping into directory under cursor and going to parent dir.
# File lib/canis/core/util/rcommandwindow.rb, line 912 def directory_key_map @key_map["<"] = Action.new("Goto Parent Dir") { |obj| # go to parent dir $log.debug "KKK: called proc for <" Dir.chdir("..") obj.buffer_changed } @key_map[">"] = Action.new("Change Dir"){ |obj| $log.debug "KKK: called proc for > : #{obj.current_value} " # step into directory dir = obj.current_value if File.directory? dir Dir.chdir dir obj.buffer_changed end } end
key handler of Controlphandler which overrides KeyDispatcher
since we need to intercept KEY_ENTER @param [Integer] ch is key read by window. WARNING: Please note that if this is used in Viewer.view
, that view
has already trapped CLOSE_KEY which is KEY_ENTER/13 for closing, so we won't get 13 anywhere
# File lib/canis/core/util/rcommandwindow.rb, line 873 def handle_key ch $log.debug " HANDLER GOT KEY #{ch} " @keyint = ch @keychr = nil # accumulate keys in a string # need to track insertion point if user uses left and right arrow @buffer ||= "" chr = nil chr = ch.chr if ch > 47 and ch < 127 @keychr = chr # Don't let user hit enter or keys if no match if [13,10, KEY_ENTER, KEY_UP, KEY_DOWN].include? ch if @no_match $log.warn "XXX: KEY GOT WAS #{ch}, #{chr} " # viewer has already blocked KEY_ENTER ! return 0 if [13,10, KEY_ENTER, KEY_UP, KEY_DOWN].include? ch else if [13,10, KEY_ENTER].include? ch @source.form.window.ungetch(1001) return 0 end end end ret = process_key ch # revert to the basic handling of key_map and refreshing pad. # but this will rerun the keys and may once again run a mapping. @source._handle_key(ch) if ret == :UNHANDLED end
a default proc to requery data based on glob supplied and the pattern user enters
# File lib/canis/core/util/rcommandwindow.rb, line 788 def recursive_search glob="**/*" @command = Proc.new {|str| Dir.glob(glob).select do |p| p.index str; end } end
modify the pattern (used if some procs are trying to change using handle to self)
# File lib/canis/core/util/rcommandwindow.rb, line 835 def set_buffer str @buffer = str buffer_changed end