class Canis::TextView
A viewable read only box. Can scroll. Intention is to be able to change content dynamically - the entire list. Use set_content
to set content, or just update the list attrib TODO -
- goto line - DONE
Attributes
Public Class Methods
# File lib/canis/core/widgets/deprecated/rtextview.rb, line 45 def initialize form = nil, config={}, &block @focusable = true @editable = false @sanitization_required = true @suppress_borders = false @row_offset = @col_offset = 1 @row = 0 @col = 0 @show_focus = false # don't highlight row under focus @list = [] map_keys super # ideally this should have been 2 to take care of borders, but that would break # too much stuff ! @win = @graphic @_events.push :CHANGE # thru vieditable @_events << :PRESS # new, in case we want to use this for lists and allow ENTER @_events << :ENTER_ROW # new, should be there in listscrollable ?? install_keys # do something about this nonsense FIXME bordertitle_init init_vars init_actions end
Public Instance Methods
supply with a color parser, if you supplied formatted text
# File lib/canis/core/widgets/deprecated/rtextview.rb, line 452 def color_parser f $log.debug "XXX: parser setting color_parser to #{f} " #@window.color_parser f @color_parser = f end
# File lib/canis/core/widgets/deprecated/rtextview.rb, line 243 def current_value @list[@current_index] end
edit content of textview in EDITOR and bring back NOTE: does not maintain content_type, so if you edit ansi text, it will come back in as normal text
# File lib/canis/core/widgets/deprecated/rtextview.rb, line 712 def edit_external require 'canis/core/include/appmethods' require 'tempfile' f = Tempfile.new("canis") l = self.text l.each { |line| f.puts line } fp = f.path f.flush editor = ENV['EDITOR'] || 'vi' vimp = %x[which #{editor}].chomp ret = shell_out "#{vimp} #{fp}" if ret lines = File.open(f,'r').readlines set_content(lines, :content_type => @old_content_type) end end
on pressing ENTER we send user some info, the calling program would bind :PRESS
# File lib/canis/core/widgets/deprecated/rtextview.rb, line 640 def fire_action_event return if @list.nil? || @list.size == 0 require 'canis/core/include/ractionevent' aev = TextActionEvent.new self, :PRESS, current_value().to_s, @current_index, @curpos fire_handler :PRESS, aev end
# File lib/canis/core/widgets/deprecated/rtextview.rb, line 147 def formatted_text text, fmt require 'canis/core/include/chunk' @formatted_text = text @color_parser = fmt remove_all end
FOR scrollable ###
# File lib/canis/core/widgets/deprecated/rtextview.rb, line 218 def get_content @list end
this sucks and should not be used but is everywhere, should use text()
# File lib/canis/core/widgets/deprecated/rtextview.rb, line 240 def getvalue @list end
# File lib/canis/core/widgets/deprecated/rtextview.rb, line 729 def init_actions editor = ENV['EDITOR'] || 'vi' am = action_manager() am.add_action( Action.new("&Edit in #{editor} ") { edit_external } ) am.add_action( Action.new("&Saveas") { saveas() }) end
dynamically load a module and execute init method. Hopefully, we can get behavior like this such as vieditable or multibuffers
# File lib/canis/core/widgets/deprecated/rtextview.rb, line 630 def load_module requirename, includename require "canis/#{requirename}" extend Object.const_get("#{includename}") send("#{requirename}_init") #if respond_to? "#{includename}_init" end
# File lib/canis/core/widgets/deprecated/rtextview.rb, line 87 def map_keys require 'canis/core/include/deprecated/listbindings' bindings() #bind_key([?\C-x, ?\C-s], :saveas) #bind_key([?\C-x, ?e], :edit_external) bind_key(32, 'scroll forward'){ scroll_forward() } # have placedhere so multi-bufer can override BS to prev buffer bind_keys([KEY_BACKSPACE,KEY_BSPACE,KEY_DELETE], :cursor_backward) #bind_key(?r) { getstr("Enter a word: ") } if $log.debug? #bind_key(?m, :disp_menu) if $log.debug? end
added 2010-09-30 18:48 so standard with other components, esp on enter NOTE: the on_enter
repaint required causes this to be repainted 2 times if its the first object, once with the entire form, then with on_enter.
# File lib/canis/core/widgets/deprecated/rtextview.rb, line 654 def on_enter if @list.nil? || @list.size == 0 Ncurses.beep return :UNHANDLED end on_enter_row @current_index set_form_row @repaint_required = true super true end
called by listscrollable, used by scrollbar ENTER_ROW
# File lib/canis/core/widgets/deprecated/rtextview.rb, line 647 def on_enter_row arow fire_handler :ENTER_ROW, self @repaint_required = true end
# File lib/canis/core/widgets/deprecated/rtextview.rb, line 665 def pipe_file # TODO ask process name from user output = pipe_output 'munpack', @list if output && !output.empty? set_content output end end
returns array of lines after running command on string passed TODO: need to close pipe other's we'll have a process lying around forever.
# File lib/canis/core/widgets/deprecated/rtextview.rb, line 675 def pipe_output (pipeto, str) case str when String #str = str.split "\n" # okay when Array str = str.join "\n" end #pipeto = '/usr/sbin/sendmail -t' #pipeto = %q{mail -s "my title" rahul} if pipeto != nil # i was taking pipeto from a hash, so checking proc = IO.popen(pipeto, "w+") proc.puts str proc.close_write proc.readlines end end
# File lib/canis/core/widgets/deprecated/rtextview.rb, line 166 def remove_all @list = [] init_vars @repaint_required = true end
# File lib/canis/core/widgets/deprecated/rtextview.rb, line 188 def row_count @list.length end
determine length of row since we have chunks now. Since chunk implements length, so not required except for the old cases of demos that use an array.
# File lib/canis/core/widgets/deprecated/rtextview.rb, line 250 def row_length case @buffer when String @buffer.length when Chunks::ChunkLine return @buffer.length when Array # this is for those old cases like rfe.rb which sent in an array # (before we moved to chunks) # line is an array of arrays if @buffer[0].is_a? Array result = 0 @buffer.each {|e| result += e[1].length } return result end # line is one single chunk return @buffer[1].length end end
# File lib/canis/core/widgets/deprecated/rtextview.rb, line 692 def saveas name=nil, config={} unless name name = rb_gets "File to save as: " return if name.nil? || name == "" end exists = File.exists? name if exists # need to prompt return unless rb_confirm("Overwrite existing file? ") end l = getvalue File.open(name, "w"){ |f| l.each { |line| f.puts line } #l.each { |line| f.write line.gsub(/\r/,"\n") } } rb_puts "#{name} written." end
send in a list e.g. set_content
File.open(“README.txt”,“r”).readlines set wrap at time of passing :WRAP_NONE :WRAP_WORD XXX if we widen the textview later, as in a vimsplit that data will sti1ll be wrapped at this width !!
2011-12-3 changed wrap to hash, so we can use content_type :ansi, :tmux
# File lib/canis/core/widgets/deprecated/rtextview.rb, line 105 def set_content list, config = {} #wrap = :WRAP_NONE @content_type = config[:content_type] _title = config[:title] self.title = _title if _title if @content_type formatted_text list, @content_type return end # please note that content type is lost once formatted text does it's work @wrap_policy = config[:wrap] if list.is_a? String if @wrap_policy == :WRAP_WORD data = wrap_text list @list = data.split("\n") else @list = list.split("\n") end elsif list.is_a? Array if @wrap_policy == :WRAP_WORD data = wrap_text list.join(" ") @list = data.split("\n") else @list = list end else raise "set_content expects Array not #{list.class}" end init_vars end
for consistency with other objects that respect text
alias :text :set_content
# File lib/canis/core/widgets/deprecated/rtextview.rb, line 136 def text(*val) if val.empty? return @list end set_content(*val) self end
# File lib/canis/core/widgets/deprecated/rtextview.rb, line 143 def text=(val) return unless val # added 2010-11-17 20:11, dup will fail on nil set_content(val) end