rbcurse-core 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -98,7 +98,7 @@ module RubyCurses
98
98
  rescue => err
99
99
  $log.error " VIEWER ERROR #{err} "
100
100
  $log.debug(err.backtrace.join("\n"))
101
- alert "Viewer:" + err.to_s
101
+ textdialog ["Error in viewer: #{err} ", *err.backtrace], :title => "Exception"
102
102
  ensure
103
103
  v_window.destroy if !v_window.nil?
104
104
  end
@@ -168,8 +168,8 @@ module RubyCurses
168
168
  # A textview object is created and yielded.
169
169
  #
170
170
  def text message
171
- @suggested_w = @width || (FFI::NCurses.COLS * 0.70).floor
172
- @suggested_h = @height || (FFI::NCurses.LINES * 0.70).floor
171
+ @suggested_w = @width || (FFI::NCurses.COLS * 0.80).floor
172
+ @suggested_h = @height || (FFI::NCurses.LINES * 0.80).floor
173
173
 
174
174
  message_col = 3
175
175
  r = 2
@@ -179,11 +179,21 @@ module RubyCurses
179
179
  bgclr = @bgcolor || :black
180
180
 
181
181
  if message.is_a? Array
182
+ l = longest_in_list message
183
+ if l > @suggested_w
184
+ if l < FFI::NCurses.COLS
185
+ #@suggested_w = l
186
+ @suggested_w = FFI::NCurses.COLS-2
187
+ else
188
+ @suggested_w = FFI::NCurses.COLS-2
189
+ end
190
+ display_length = @suggested_w-6
191
+ end
182
192
  # reduce width and height if you can based on array contents
183
193
  else
184
194
  message = wrap_text(message, display_length).split("\n")
185
195
  end
186
- message_height = message.size
196
+ message_height = message.size + 8
187
197
  # reduce if possible if its not required.
188
198
  #
189
199
  r1 = (FFI::NCurses.LINES-@suggested_h)/2
@@ -217,7 +227,7 @@ module RubyCurses
217
227
  rescue => err
218
228
  $log.debug( err) if err
219
229
  $log.debug(err.backtrace.join("\n")) if err
220
- alert "Exception in Messagebox: #{err}. Check log"
230
+ textdialog ["Error in Messagebox: #{err} ", *err.backtrace], :title => "Exception"
221
231
  @window.refresh # otherwise the window keeps showing (new FFI-ncurses issue)
222
232
  $error_message.value = ""
223
233
  ensure
@@ -54,7 +54,7 @@ class TabbedWindow
54
54
  rescue => err
55
55
  $log.debug( err) if err
56
56
  $log.debug(err.backtrace.join("\n")) if err
57
- alert "Got an exception in NewTabbedWindow: #{err}. Check log"
57
+ textdialog ["Error in TabbedWindow: #{err} ", *err.backtrace], :title => "Exception"
58
58
  $error_message.value = ""
59
59
  ensure
60
60
  end
@@ -234,7 +234,8 @@ module RubyCurses
234
234
  # Added on 2011-10-10
235
235
  # @since 1.4.0
236
236
  # @param [String, Array] String is an existing filename, Array is content to be replaced
237
- def set_content lines
237
+ # Added config for compatibility with textview
238
+ def set_content lines, config={}
238
239
  case lines
239
240
  when String
240
241
  if File.exists? lines
@@ -249,7 +250,20 @@ module RubyCurses
249
250
  @list.replace lines
250
251
  @repaint_required = true
251
252
  end
252
- alias :text :set_content
253
+ #alias :text :set_content
254
+ # set text
255
+ # Added for consistency with other widgets
256
+ def text(*val)
257
+ if val.empty?
258
+ return @list
259
+ end
260
+ set_content(*val)
261
+ self
262
+ end
263
+ def text=(val)
264
+ return unless val # added 2010-11-17 20:11, dup will fail on nil
265
+ set_content(val)
266
+ end
253
267
  def get_window
254
268
  @graphic
255
269
  end
@@ -271,12 +285,12 @@ module RubyCurses
271
285
  # the next was irritating if user wanted to add a row ! 2011-10-10
272
286
  #bind_key(Ncurses::KEY_DOWN){ ret = down ; get_window.ungetch(KEY_TAB) if ret == :NO_NEXT_ROW }
273
287
  bind_key(Ncurses::KEY_DOWN){ ret = down ; }
274
- bind_key(?\C-a){ cursor_bol }
275
- bind_key(?\C-e){ cursor_eol }
276
- bind_key(?\C-n) { scroll_forward }
277
- bind_key(?\C-p) { scroll_backward }
278
- bind_key(?\C-[) { goto_start }
279
- bind_key(?\C-]) { goto_end }
288
+ bind_key(?\C-a, 'start of line'){ cursor_bol }
289
+ bind_key(?\C-e, 'end of line'){ cursor_eol }
290
+ bind_key(?\C-d, 'scroll forward') { scroll_forward }
291
+ bind_key(?\C-b, 'scroll backward') { scroll_backward }
292
+ bind_key(?\C-[, 'goto start') { goto_start }
293
+ bind_key(?\C-], 'goto end') { goto_end }
280
294
 
281
295
  bind_key(KEY_BACKSPACE){ delete_prev_char if @editable }
282
296
  bind_key(KEY_BSPACE){ delete_prev_char if @editable}
@@ -284,10 +298,12 @@ module RubyCurses
284
298
  bind_key(?\M-f, :forward_word)
285
299
 
286
300
  #bind_key(127){ delete_prev_char }
287
- bind_key(330){ delete_curr_char if @editable }
301
+ bind_key(330, 'delete current char'){ delete_curr_char if @editable }
288
302
  #bind_key(?\C-k){ delete_eol }
289
303
  #bind_key(?\C-_){ undo_delete_eol }
290
304
  #bind_key(27){ set_buffer @original_value }
305
+ bind_key([?\C-x, ?e], :edit_external)
306
+ bind_key([?\C-x, ?\C-s], :saveas)
291
307
  @keys_mapped = true
292
308
  end
293
309
 
@@ -915,6 +931,40 @@ module RubyCurses
915
931
  return unless @undo_handler
916
932
  @undo_handler.redo
917
933
  end
934
+
935
+ def edit_external
936
+ require 'rbcurse/core/include/appmethods'
937
+ require 'tempfile'
938
+ f = Tempfile.new("rbcurse")
939
+ l = self.text
940
+ l.each { |line| f.puts line }
941
+ fp = f.path
942
+ f.flush
943
+
944
+ editor = ENV['EDITOR'] || 'vi'
945
+ vimp = %x[which #{editor}].chomp
946
+ ret = shell_out "#{vimp} #{fp}"
947
+ if ret
948
+ lines = File.open(f,'r').readlines
949
+ set_content(lines, :content_type => @old_content_type)
950
+ end
951
+ end
952
+ def saveas name=nil, config={}
953
+ unless name
954
+ name = rb_gets "File to save as: "
955
+ return if name.nil? || name == ""
956
+ end
957
+ exists = File.exists? name
958
+ if exists # need to prompt
959
+ return unless rb_confirm("Overwrite existing file? ")
960
+ end
961
+ l = getvalue
962
+ File.open(name, "w"){ |f|
963
+ l.each { |line| f.puts line }
964
+ #l.each { |line| f.write line.gsub(/\r/,"\n") }
965
+ }
966
+ rb_puts "#{name} written."
967
+ end
918
968
  end # class textarea
919
969
  ##
920
970
  end # modul
@@ -95,12 +95,13 @@ module RubyCurses
95
95
  bind_key(?\M-l, :scroll_right)
96
96
  bind_key(?\M-h, :scroll_left)
97
97
  bind_key([?\C-x, ?\C-s], :saveas)
98
+ bind_key([?\C-x, ?e], :edit_external)
98
99
  bind_keys([?\C-d, 32], 'scroll forward'){ scroll_forward() }
99
100
  bind_key(?\C-b, 'scroll backward'){ scroll_backward() }
100
101
  # have placedhere so multi-bufer can override BS to prev buffer
101
102
  bind_keys([KEY_BACKSPACE,KEY_BSPACE,KEY_DELETE], :cursor_backward)
102
- #bind_key(?r) { getstr("Enter a word: ") }
103
- bind_key(?m, :disp_menu)
103
+ bind_key(?r) { getstr("Enter a word: ") } if $log.debug?
104
+ bind_key(?m, :disp_menu) if $log.debug?
104
105
  end
105
106
  ##
106
107
  # send in a list
@@ -117,6 +118,7 @@ module RubyCurses
117
118
  formatted_text list, @content_type
118
119
  return
119
120
  end
121
+ # please note that content type is lost once formatted text does it's work
120
122
  @wrap_policy = config[:wrap]
121
123
  if list.is_a? String
122
124
  if @wrap_policy == :WRAP_WORD
@@ -138,7 +140,18 @@ module RubyCurses
138
140
  init_vars
139
141
  end
140
142
  # for consistency with other objects that respect text
141
- alias :text :set_content
143
+ #alias :text :set_content
144
+ def text(*val)
145
+ if val.empty?
146
+ return @list
147
+ end
148
+ set_content(*val)
149
+ self
150
+ end
151
+ def text=(val)
152
+ return unless val # added 2010-11-17 20:11, dup will fail on nil
153
+ set_content(val)
154
+ end
142
155
  def formatted_text text, fmt
143
156
  require 'rbcurse/core/include/chunk'
144
157
  @formatted_text = text
@@ -257,6 +270,8 @@ module RubyCurses
257
270
  @repaint_footer_required = true if @oldrow != @current_index # 2011-10-15
258
271
  print_foot if @print_footer && !@suppress_borders && @repaint_footer_required
259
272
  end
273
+ # this sucks and should not be used but is everywhere, should
274
+ # use text()
260
275
  def getvalue
261
276
  @list
262
277
  end
@@ -369,7 +384,7 @@ module RubyCurses
369
384
  rescue => err
370
385
  $log.error " TEXTVIEW ERROR #{err} "
371
386
  $log.debug(err.backtrace.join("\n"))
372
- alert err.to_s
387
+ textdialog [err.to_s, *err.backtrace], :title => "Exception"
373
388
  end
374
389
  return :UNHANDLED if ret == :UNHANDLED
375
390
  end
@@ -510,6 +525,7 @@ module RubyCurses
510
525
  #l = []
511
526
  #@formatted_text.each { |e| l << cp.convert_to_chunk(e) }
512
527
 
528
+ @old_content_type = @content_type
513
529
  text(l)
514
530
  @formatted_text = nil
515
531
 
@@ -635,13 +651,14 @@ module RubyCurses
635
651
  end
636
652
  ## this is just a test of prompting user for a string
637
653
  #+ as an alternative to the dialog.
638
- def getstr prompt, maxlen=10 #:nodoc:
654
+ def getstr prompt, maxlen=80 #:nodoc:
639
655
  tabc = Proc.new {|str| Dir.glob(str +"*") }
640
656
  config={}; config[:tab_completion] = tabc
641
- config[:default] = "default"
657
+ config[:default] = "test"
658
+ config[:display_length] = 11
642
659
  $log.debug " inside getstr before call "
643
660
  ret, str = rbgetstr(@form.window, @row+@height-1, @col+1, prompt, maxlen, config)
644
- $log.debug " rbgetstr returned #{ret} , #{str} "
661
+ $log.debug " rbgetstr returned #{ret} ,#{str}."
645
662
  return "" if ret != 0
646
663
  return str
647
664
  end
@@ -759,19 +776,40 @@ module RubyCurses
759
776
  end
760
777
  def saveas name=nil, config={}
761
778
  unless name
762
- name = ask "File to save as: "
779
+ name = rb_gets "File to save as: "
763
780
  return if name.nil? || name == ""
764
781
  end
765
782
  exists = File.exists? name
766
783
  if exists # need to prompt
767
- return unless agree("Overwrite existing file? ", true)
784
+ return unless rb_confirm("Overwrite existing file? ")
768
785
  end
769
786
  l = getvalue
770
787
  File.open(name, "w"){ |f|
771
788
  l.each { |line| f.puts line }
772
789
  #l.each { |line| f.write line.gsub(/\r/,"\n") }
773
790
  }
774
- say_with_wait "#{name} written."
791
+ rb_puts "#{name} written."
792
+ end
793
+
794
+ # edit content of textview in EDITOR and bring back
795
+ # NOTE: does not maintain content_type, so if you edit ansi text,
796
+ # it will come back in as normal text
797
+ def edit_external
798
+ require 'rbcurse/core/include/appmethods'
799
+ require 'tempfile'
800
+ f = Tempfile.new("rbcurse")
801
+ l = self.text
802
+ l.each { |line| f.puts line }
803
+ fp = f.path
804
+ f.flush
805
+
806
+ editor = ENV['EDITOR'] || 'vi'
807
+ vimp = %x[which #{editor}].chomp
808
+ ret = shell_out "#{vimp} #{fp}"
809
+ if ret
810
+ lines = File.open(f,'r').readlines
811
+ set_content(lines, :content_type => @old_content_type)
812
+ end
775
813
  end
776
814
 
777
815
 
@@ -439,7 +439,7 @@ module RubyCurses
439
439
  $log.debug "KEY: #{name} : #{val} "
440
440
  }
441
441
  }
442
- textdialog arr
442
+ textdialog arr, :title => "Key Bindings"
443
443
  end
444
444
  def bind_keys keycodes, *args, &blk
445
445
  keycodes.each { |k| bind_key k, *args, &blk }
@@ -1902,6 +1902,9 @@ module RubyCurses
1902
1902
  attr_reader :original_value # value on entering field
1903
1903
  attr_accessor :overwrite_mode # true or false INSERT OVERWRITE MODE
1904
1904
 
1905
+ attr_reader :field_col # column on which field is printed
1906
+ # required due to labels. Is updated after printing
1907
+ # # so can be nil if accessed early 2011-12-8
1905
1908
  # For consistency, now width equates to display_length
1906
1909
  alias :width :display_length
1907
1910
  alias :width= :display_length=
@@ -1928,6 +1931,8 @@ module RubyCurses
1928
1931
  def init_vars
1929
1932
  @pcol = 0 # needed for horiz scrolling
1930
1933
  @curpos = 0 # current cursor position in buffer
1934
+ # this is the index where characters are put or deleted
1935
+ # # when user edits
1931
1936
  @modified = false
1932
1937
  @repaint_required = true
1933
1938
  end
@@ -2039,6 +2044,8 @@ module RubyCurses
2039
2044
  #$log.debug " FIELD DATA #{@datatype}"
2040
2045
  @delete_buffer = @buffer.dup
2041
2046
  @buffer = value.to_s.dup
2047
+ # don't allow setting of value greater than maxlen
2048
+ @buffer = @buffer[0,@maxlen] if @maxlen && @buffer.length > @maxlen
2042
2049
  @curpos = 0
2043
2050
  # hope @delete_buffer is not overwritten
2044
2051
  fire_handler :CHANGE, InputDataEvent.new(@curpos,@curpos, self, :DELETE, 0, @delete_buffer) # 2010-09-11 13:01
@@ -2111,7 +2118,7 @@ module RubyCurses
2111
2118
  end
2112
2119
  @bgcolor ||= $def_bg_color
2113
2120
  @color ||= $def_fg_color
2114
- $log.debug("repaint FIELD: #{id}, #{name}, #{row} #{col}, #{focusable} st: #{@state} ")
2121
+ $log.debug("repaint FIELD: #{id}, #{name}, #{row} #{col},pcol:#{@pcol}, #{focusable} st: #{@state} ")
2115
2122
  #return if display_length <= 0 # added 2009-02-17 00:17 sometimes editor comp has 0 and that
2116
2123
  # becomes negative below, no because editing still happens
2117
2124
  @display_length = 1 if display_length == 0
@@ -2143,6 +2150,7 @@ module RubyCurses
2143
2150
  @col_offset = c-@col # required so cursor lands in right place
2144
2151
  end
2145
2152
  @graphic.printstring r, c, sprintf("%-*s", display_length, printval), acolor, @attr
2153
+ @field_col = c
2146
2154
  @repaint_required = false
2147
2155
  end
2148
2156
  def set_focusable(tf)
@@ -2191,8 +2199,9 @@ module RubyCurses
2191
2199
  ##
2192
2200
  # position cursor at start of field
2193
2201
  def cursor_home
2194
- set_form_col 0
2202
+ @curpos = 0
2195
2203
  @pcol = 0
2204
+ set_form_col 0
2196
2205
  end
2197
2206
  ##
2198
2207
  # goto end of field, "end" is a keyword so could not use it.
@@ -2201,13 +2210,29 @@ module RubyCurses
2201
2210
  if blen < @display_length
2202
2211
  set_form_col blen
2203
2212
  else
2213
+ # there is a problem here FIXME.
2204
2214
  @pcol = blen-@display_length
2205
- set_form_col @display_length-1
2215
+ #set_form_col @display_length-1
2216
+ set_form_col blen
2206
2217
  end
2207
- @curpos = blen # HACK
2218
+ @curpos = blen # this is position in array where editing or motion is to happen regardless of what you see
2219
+ # regardless of pcol (panning)
2208
2220
  # $log.debug " crusor END cp:#{@curpos} pcol:#{@pcol} b.l:#{@buffer.length} d_l:#{@display_length} fc:#{@form.col}"
2209
2221
  #set_form_col @buffer.length
2210
2222
  end
2223
+ # sets the visual cursor on the window at correct place
2224
+ # added here since we need to account for pcol. 2011-12-7
2225
+ # NOTE be careful of curpos - pcol being less than 0
2226
+ def set_form_col col1=@curpos
2227
+ @curpos = col1 || 0 # NOTE we set the index of cursor here
2228
+ c = @col + @col_offset + @curpos - @pcol
2229
+ min = @col + @col_offset
2230
+ max = min + @display_length
2231
+ c = min if c < min
2232
+ c = max if c > max
2233
+ $log.debug " #{@name} FIELD set_form_col #{c}, curpos #{@curpos} , #{@col} + #{@col_offset} pcol:#{@pcol} "
2234
+ setrowcol nil, c
2235
+ end
2211
2236
  def delete_eol
2212
2237
  return -1 unless @editable
2213
2238
  pos = @curpos-1
@@ -2254,10 +2279,17 @@ module RubyCurses
2254
2279
  def delete_prev_char
2255
2280
  return -1 if !@editable
2256
2281
  return if @curpos <= 0
2282
+ # if we've panned, then unpan, and don't move cursor back
2283
+ # Otherwise, adjust cursor (move cursor back as we delete)
2284
+ adjust = true
2285
+ if @pcol > 0
2286
+ @pcol -= 1
2287
+ adjust = false
2288
+ end
2257
2289
  @curpos -= 1 if @curpos > 0
2258
2290
  delete_at
2291
+ addcol -1 if adjust # move visual cursor back
2259
2292
  set_modified
2260
- addcol -1
2261
2293
  end
2262
2294
  ## add a column to cursor position. Field
2263
2295
  def addcol num
@@ -60,7 +60,7 @@ module RubyCurses
60
60
  @x = '+'
61
61
  self.columns = cols if cols
62
62
  if !args.empty?
63
- puts "ARGS after shift #{args} "
63
+ #puts "ARGS after shift #{args} "
64
64
  if !args.empty?
65
65
  self.data = args
66
66
  end
@@ -84,8 +84,8 @@ module RubyCurses
84
84
  # set data as an array of arrays
85
85
  # @param [Array<Array>] data as array of arrays
86
86
  def data=(list)
87
- puts "got data: #{list.size} " if !$log
88
- puts list if !$log
87
+ #puts "got data: #{list.size} " if !$log
88
+ #puts list if !$log
89
89
  @list = list
90
90
  end
91
91
 
@@ -213,7 +213,7 @@ module RubyCurses
213
213
  end
214
214
  }
215
215
  @fmstr = fmt.join(@y)
216
- puts "format: #{@fmstr} "
216
+ #puts "format: #{@fmstr} " # 2011-12-09 23:09:57
217
217
  end
218
218
  end
219
219
  end
@@ -516,7 +516,7 @@ module RubyCurses
516
516
  rescue => err
517
517
  $error_message.value = err.to_s
518
518
  # @form.window.print_error_message # changed 2011 dts
519
- alert err.to_s
519
+ textdialog ["Error in TabularWidget: #{err} ", *err.backtrace], :title => "Exception"
520
520
  $log.error " Tabularwidget ERROR #{err} "
521
521
  $log.debug(err.backtrace.join("\n"))
522
522
  # XXX caller app has no idea error occurred so can't do anything !
@@ -392,7 +392,7 @@ module RubyCurses
392
392
  rescue => err
393
393
  $log.error " TEXTPAD ERROR #{err} "
394
394
  $log.debug(err.backtrace.join("\n"))
395
- alert err.to_s
395
+ textdialog ["Error in TextPad: #{err} ", *err.backtrace], :title => "Exception"
396
396
  # FIXME why does this result in a blank spot on screen till its refreshed again
397
397
  # should not happen if its deleting its panel and doing an update panel
398
398
  end
@@ -402,7 +402,7 @@ module RubyCurses
402
402
  rescue => err
403
403
  $log.debug( err) if err
404
404
  $log.debug(err.backtrace.join("\n")) if err
405
- alert "Got an exception in PadReader: #{err}. Check log"
405
+ textdialog ["Error in TextPad: #{err} ", *err.backtrace], :title => "Exception"
406
406
  $error_message.value = ""
407
407
  ensure
408
408
  end