Class: Canis::ControlPHandler

Inherits:
Object
  • Object
show all
Includes:
KeyDispatcher
Defined in:
lib/canis/core/util/rcommandwindow.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from KeyDispatcher

#bind_key, #default_string_key_map, #process_key

Constructor Details

#initialize(source) ⇒ ControlPHandler

Returns a new instance of ControlPHandler.



775
776
777
778
779
780
781
782
783
784
785
# 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

Instance Attribute Details

#bufferObject

string the user is currently entering in (pattern to filter on)



766
767
768
# File 'lib/canis/core/util/rcommandwindow.rb', line 766

def buffer
  @buffer
end

#commandObject

specify command to requery data



771
772
773
# File 'lib/canis/core/util/rcommandwindow.rb', line 771

def command
  @command
end

#default_layoutObject

Returns the value of attribute default_layout.



764
765
766
# File 'lib/canis/core/util/rcommandwindow.rb', line 764

def default_layout
  @default_layout
end

#headerObject

application_header object whose text can be changed



768
769
770
# File 'lib/canis/core/util/rcommandwindow.rb', line 768

def header
  @header
end

#key_mapObject

Returns the value of attribute key_map.



769
770
771
# File 'lib/canis/core/util/rcommandwindow.rb', line 769

def key_map
  @key_map
end

#keychrObject (readonly)

Returns the value of attribute keychr.



774
775
776
# File 'lib/canis/core/util/rcommandwindow.rb', line 774

def keychr
  @keychr
end

#keyintObject (readonly)

Returns the value of attribute keyint.



773
774
775
# File 'lib/canis/core/util/rcommandwindow.rb', line 773

def keyint
  @keyint
end

#maxhtObject

Returns the value of attribute maxht.



763
764
765
# File 'lib/canis/core/util/rcommandwindow.rb', line 763

def maxht
  @maxht
end

#sourceObject (readonly)

Returns the value of attribute source.



772
773
774
# File 'lib/canis/core/util/rcommandwindow.rb', line 772

def source
  @source
end

Instance Method Details

#buffer_changedObject

signal that the user has added or deleted a char from the pattern and data should be requeried, etc



842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
# 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

#current_valueObject

the line on which focus is.



932
933
934
# File 'lib/canis/core/util/rcommandwindow.rb', line 932

def current_value
  @source.current_value
end

#data_changed(list) ⇒ Object

signal that the data has changed and should be redisplayed with window resizing etc.



800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
# 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

#default_key_mapObject

setting up some keys



903
904
905
906
907
# 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

#directory_key_mapObject

specific actions for directory listers currently for stepping into directory under cursor and going to parent dir.



912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
# 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

#handle_key(ch) ⇒ Object

key handler of Controlphandler which overrides KeyDispatcher since we need to intercept KEY_ENTER 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

Parameters:

  • ch (Integer)

    is key read by window.



873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
# 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

#recursive_search(glob = "**/*") ⇒ Object

a default proc to requery data based on glob supplied and the pattern user enters



788
789
790
# 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

#set_buffer(str) ⇒ Object

modify the pattern (used if some procs are trying to change using handle to self)



835
836
837
838
# File 'lib/canis/core/util/rcommandwindow.rb', line 835

def set_buffer str
  @buffer = str
  buffer_changed
end