Module: Canis::MultiBuffers

Extended by:
MultiBuffers
Included in:
MultiBuffers
Defined in:
lib/canis/core/include/multibuffer.rb

Overview

TODO ?? allow setting of a limit, so in some cases where we keep adding programatically, the TODO: maintain cursor and line number so user can revert to same point. this will have to be

updated by buffer_next and others.

Done: need to be able to set multiple file names. which are read in only when

buffer is accessed. filename to be maintained and used as title.
== CHANGE: 
allow filename to be sent, rather than array. Array was very limiting since it 
 did not have a name to list or goto a buffer with. Also, now we can add file names that
 are read only if the buffer is selected.

Instance Method Summary collapse

Instance Method Details

#add_content(text, config = {}) ⇒ Object

add content to buffers of a textview

Parameters:

  • text, (Array)

    or String (filename)

  • options, (Hash)

    typically :content_type => :ansi or :tmux, and :title



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/canis/core/include/multibuffer.rb', line 26

def add_content text, config={}
  unless @_buffers
    bind_key(?\M-n, :buffer_next)
    bind_key(?\M-p, :buffer_prev)
    bind_key(KEY_BACKSPACE, :buffer_prev) # backspace, already hardcoded in textview !
    bind_key(?:, :buffer_menu)
  end
  @_buffers ||= []
  @_buffers_conf ||= []
  @_buffers << text
  if text.is_a? String
    config[:filename] = text
    config[:title] ||= text
  end
  @_buffers_conf << config
  @_buffer_ctr ||= 0
  $log.debug "XXX:  HELP adding text #{@_buffers.size} "
end

#add_files(filearray, config = {}) ⇒ Object

supply an array of files to the multibuffer. These will be read

as the user presses next or last etc.


47
48
49
# File 'lib/canis/core/include/multibuffer.rb', line 47

def add_files filearray, config={}
  filearray.each do |e| add_content(e, config.dup); end
end

#buffer_at(index) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/canis/core/include/multibuffer.rb', line 112

def buffer_at index
  buffer_update_info
  @_buffer_ctr = index
  l = @_buffers[index]
  if l
    populate_buffer_from_filename index
    l = @_buffers[index]
    set_content l, @_buffers_conf[index]
    buffer_update_position


  end
end

#buffer_lastObject



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/canis/core/include/multibuffer.rb', line 99

def buffer_last
  buffer_update_info
  @_buffer_ctr = @_buffers.count - 1
  x = @_buffer_ctr
  l = @_buffers.last
  if l
    populate_buffer_from_filename x
    l = @_buffers[x]
    $log.debug "  calling set_content with #{l.class} "
    set_content l, @_buffers_conf.last
    buffer_update_position
  end
end

#buffer_menuObject

display a menu so user can do buffer management However, how can application add to these. Or disable, such as when we add buffer delete or buffer insert or edit



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/canis/core/include/multibuffer.rb', line 132

def buffer_menu
  menu = PromptMenu.new self do
    item :n, :buffer_next
    item :p, :buffer_prev
    item :b, :scroll_backward
    item :f, :scroll_forward
    item :l, :list_buffers
    item :q, :close
    submenu :m, "submenu..." do
      item :p, :goto_last_position
      item :r, :scroll_right
      item :l, :scroll_left
    end
  end
  menu.display_new :title => "Buffer Menu"
end

#buffer_nextObject

display next buffer



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/canis/core/include/multibuffer.rb', line 52

def buffer_next
  buffer_update_info
  @_buffer_ctr += 1
  x = @_buffer_ctr
  l = @_buffers[x]
  if l
    populate_buffer_from_filename x
  else
    @_buffer_ctr = 0 
  end
  set_content @_buffers[@_buffer_ctr], @_buffers_conf[@_buffer_ctr]
  buffer_update_position
end

#buffer_prevObject

display previous buffer if any



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/canis/core/include/multibuffer.rb', line 82

def buffer_prev
  buffer_update_info
  if @_buffer_ctr < 1
    buffer_last
    return
  end
  @_buffer_ctr -= 1 if @_buffer_ctr > 0
  x = @_buffer_ctr
  l = @_buffers[x]
  if l
    populate_buffer_from_filename x
    l = @_buffers[x]
    $log.debug "bp calling set_content with #{l.class} "
    set_content l, @_buffers_conf[x]
    buffer_update_position
  end
end

#buffer_update_infoObject



160
161
162
163
164
# File 'lib/canis/core/include/multibuffer.rb', line 160

def buffer_update_info
  x = @_buffer_ctr || 0
  @_buffers_conf[x][:current_index] = @current_index || 0
  @_buffers_conf[x][:curpos] = @curpos || 0
end

#buffer_update_positionObject



165
166
167
168
169
170
# File 'lib/canis/core/include/multibuffer.rb', line 165

def buffer_update_position
  x = @_buffer_ctr || 0
  ci = (@_buffers_conf[x][:current_index] || 0)
  goto_line ci
  @curpos = (@_buffers_conf[x][:curpos] || 0)
end

#closeObject

close window, a bit clever, we really don’t know what the CLOSE_KEY is



126
127
128
# File 'lib/canis/core/include/multibuffer.rb', line 126

def close
  @graphic.ungetch(?q.ord)
end

#list_buffersObject

pops up a list of buffers using titles allowing the user to select Based on selection, that buffer is displayed.



150
151
152
153
154
155
156
157
158
159
# File 'lib/canis/core/include/multibuffer.rb', line 150

def list_buffers
  arr = []
  @_buffers_conf.each_with_index do |e, i|
    t = e[:title] || "no title for #{i}"
    #$log.debug "  TITLE is #{e.title} , t is #{t} "
    arr << t
  end
  ix = popuplist arr
  buffer_at ix
end

#populate_buffer_from_filename(x) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/canis/core/include/multibuffer.rb', line 65

def populate_buffer_from_filename x
  l = @_buffers[x]
  if l
    if l.is_a? String
      if File.directory? l
        Dir.chdir(l)
        arr = Dir.entries(".")
        @_buffers[x] = arr
      else
        arr = File.open(l,"r").read.split("\n")
        @_buffers[x] = arr
      end
    end
  end
end