Class: RubyCurses::MultiTextView

Inherits:
TextView
  • Object
show all
Includes:
ListScrollable
Defined in:
lib/rbcurse/extras/widgets/rmultitextview.rb

Overview

A viewable read only box. Can scroll. Extends TextView with ability to load more than one file or content and switch between files (buffers). NOTE: ideally, i should be able to dynamically add this functionality to either Textview or TextArea or even ListBox or Table someday. Should then be a Module rather than a class.

Instance Method Summary collapse

Constructor Details

#initialize(form = nil, config = {}, &block) ⇒ MultiTextView

Returns a new instance of MultiTextView.



33
34
35
36
37
38
# File 'lib/rbcurse/extras/widgets/rmultitextview.rb', line 33

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

  super
  @bmanager = BufferManager.new self

end

Instance Method Details

#add(file, title) ⇒ Object

load a file into the textview. This is the preferred method since it lets us add a title too Shucks, this misses wrap_style which the other one has

Parameters:

  • filename (String)


170
171
172
173
174
175
176
177
178
179
180
# File 'lib/rbcurse/extras/widgets/rmultitextview.rb', line 170

def add file, title
  begin
    @current_buffer = @bmanager.add file, title
    $log.debug " file edit got cb : #{@current_buffer} "
    set_current_buffer
  rescue => err
    alert err.to_s
    Ncurses.beep
    return -1
  end
end

#buffer_at(index) ⇒ Object



181
182
183
184
185
# File 'lib/rbcurse/extras/widgets/rmultitextview.rb', line 181

def buffer_at index
  @current_buffer = @bmanager.element_at index
  $log.debug " buffer_last got #{@current_buffer} "
  set_current_buffer
end

#buffer_deleteObject



139
140
141
142
143
144
145
146
147
# File 'lib/rbcurse/extras/widgets/rmultitextview.rb', line 139

def buffer_delete
  if @bmanager.size > 1
    @bmanager.delete_at
    @current_buffer = @bmanager.previous
    set_current_buffer
  else
    perror "Only one buffer. Cannot delete."
  end
end

#buffer_firstObject



129
130
131
132
133
# File 'lib/rbcurse/extras/widgets/rmultitextview.rb', line 129

def buffer_first
  @current_buffer = @bmanager.first
  $log.debug " buffer_first got #{@current_buffer} "
  set_current_buffer
end

#buffer_lastObject



134
135
136
137
138
# File 'lib/rbcurse/extras/widgets/rmultitextview.rb', line 134

def buffer_last
  @current_buffer = @bmanager.last
  $log.debug " buffer_last got #{@current_buffer} "
  set_current_buffer
end

#buffer_menuObject

this is just a test of the simple “most” menu can use this for next, prev, first, last, new, delete, overwrite etc



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rbcurse/extras/widgets/rmultitextview.rb', line 89

def buffer_menu
  menu = PromptMenu.new self 
  menu.add(menu.create_mitem( 'e', "edit a file", "opened file ", :file_edit ))
  menu.add(menu.create_mitem( 'o', "overwrite file", "opened a file ", :file_overwrite ))
  menu.add(menu.create_mitem( 'l', "list buffers", "list buffers ", :buffers_list ))
  item = menu.create_mitem( 'b', "Buffer Options", "Buffer Options" )
  menu1 = PromptMenu.new( self, "Buffer Options")
  menu1.add(menu1.create_mitem( 'n', "Next", "Switched to next buffer", :buffer_next ))
  menu1.add(menu1.create_mitem( 'p', "Prev", "Switched to previous buffer", :buffer_previous ))
  menu1.add(menu1.create_mitem( 'f', "First", "Switched to first buffer", :buffer_first ))
  menu1.add(menu1.create_mitem( 'l', "Last", "Switched to last buffer", :buffer_last ))
  menu1.add(menu1.create_mitem( 'd', "Delete", "Deleted buffer", :buffer_delete ))
  item.action = menu1
  menu.add(item)
  # how do i know what's available. the application or window should know where to place
  menu.display @form.window, $error_message_row, $error_message_col, $datacolor #, menu
end

#buffer_nextObject



116
117
118
119
120
121
# File 'lib/rbcurse/extras/widgets/rmultitextview.rb', line 116

def buffer_next
  perror "No other buffer" and return if @bmanager.size < 2

  @current_buffer = @bmanager.next
  set_current_buffer
end

#buffer_previousObject



122
123
124
125
126
127
128
# File 'lib/rbcurse/extras/widgets/rmultitextview.rb', line 122

def buffer_previous
  perror "No other buffer" and return if @bmanager.size < 2

  @current_buffer = @bmanager.previous
  $log.debug " buffer_prev got #{@current_buffer} "
  set_current_buffer
end

#buffers_listObject



148
149
150
151
152
153
154
155
156
157
# File 'lib/rbcurse/extras/widgets/rmultitextview.rb', line 148

def buffers_list
  menu = PromptMenu.new self 
  @bmanager.each_with_index{ |b, ix|
    aproc = Proc.new { buffer_at(ix) }
    name = b.title
    num = ix + 1
    menu.add(menu.create_mitem( num.to_s, name, "Switched to buffer #{ix}", aproc ))
  }
  menu.display @form.window, $error_message_row, $error_message_col, $datacolor
end

#current_bufferRBuffer

returns current buffer

Returns:



51
52
53
# File 'lib/rbcurse/extras/widgets/rmultitextview.rb', line 51

def current_buffer
  @bmanager.current
end

#file_editObject

prompts user for filename and opens in buffer Like vim’s :e



160
161
162
163
164
165
# File 'lib/rbcurse/extras/widgets/rmultitextview.rb', line 160

def file_edit
  file = getfilename()
  $log.debug " got file_edit: #{file} "
  return if file == ""
  add file, file
end

#getfilename(prompt = "Enter filename: ", maxlen = 90) ⇒ Object

prompt user for a filename to read in



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rbcurse/extras/widgets/rmultitextview.rb', line 76

def getfilename prompt="Enter filename: ", maxlen=90
  tabc = Proc.new {|str| Dir.glob(str +"*") }
  config={}; config[:tab_completion] = tabc
  #config[:default] = "defaulT"
  $log.debug " inside getstr before call #{$error_message_row} +  #{$error_message_col}  "
  #ret, str = rbgetstr(@form.window, @row+@height-1, @col+1, prompt, maxlen, config)
  ret, str = rbgetstr(@form.window, $error_message_row, $error_message_col, prompt, maxlen, config)
  $log.debug " rbgetstr returned #{ret} , #{str} "
  return "" if ret != 0
  return str
end

#handle_key(ch) ⇒ Object

multi-textview



65
66
67
68
69
70
71
72
73
74
# File 'lib/rbcurse/extras/widgets/rmultitextview.rb', line 65

def handle_key ch
  # put list as current list and buffer too then super
  #@current_buffer = @bmanager.current
  #@list = @current_buffer.list
  @buffer = @list[@current_index]
  #@buffer = @bmanager.current
  ret = super
  # check for any keys not handled and check our own ones
  return ret # 
end

#init_varsObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/rbcurse/extras/widgets/rmultitextview.rb', line 39

def init_vars
  super
  bind_key(?:, :buffer_menu)
  bind_key(?e, :file_edit)
  bind_key([?\C-x, ?f], :file_edit)
  bind_key([?\C-x, ?k], :buffer_delete)
  bind_key([?\C-x, ?\C-b], :buffers_list)
  # easily cycle using p. n is used for next search.
  bind_key(?p, :buffer_previous)
end

#perror(errmess) ⇒ Object



192
193
194
195
# File 'lib/rbcurse/extras/widgets/rmultitextview.rb', line 192

def perror errmess
  #@form.window.print_error_message errmess
  alert errmess
end

#set_content(list, wrap = :WRAP_NONE) ⇒ Object

send in a list e.g. set_content File.open(“README.txt”,“r”).readlines set wrap at time of passing :WRAP_NONE :WRAP_WORD



59
60
61
62
63
# File 'lib/rbcurse/extras/widgets/rmultitextview.rb', line 59

def set_content list, wrap = :WRAP_NONE
  ret = super
#  buff = @bmanager.add_content @list
  return ret
end

#set_current_bufferObject



186
187
188
189
190
191
# File 'lib/rbcurse/extras/widgets/rmultitextview.rb', line 186

def set_current_buffer
  @current_index = @current_buffer.current_index
  @curpos = @current_buffer.curpos
  @title = @current_buffer.title
  @list = @current_buffer.list
end