cygnus 0.0.4 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/bin/cygnus CHANGED
@@ -8,7 +8,7 @@
8
8
  # Author: rkumar http://github.com/rkumar/mancurses/
9
9
  # Date: 2011-11-09 - 16:59
10
10
  # License: Same as Ruby's License (http://www.ruby-lang.org/LICENSE.txt)
11
- # Last update: 2013-03-19 20:04
11
+ # Last update: 2013-03-21 13:39
12
12
  #
13
13
  # == CHANGES
14
14
  # == BUGS
@@ -16,13 +16,16 @@
16
16
  # _ show form bindings too
17
17
  # x our bindings from the hash do not show up..
18
18
  # _ white line at bottom and right
19
+ # _ XXX standardize textpad based on textview
20
+ # _ do some things go from here to some comman like rdialog.rb?
21
+ # get_single etc, menus full_indexing ? or dir stuff ?
19
22
  #
20
23
  # == TODO
21
24
  #
22
- # _ use BS to cycle files visited as in mancurses
25
+ # x use BS to cycle files visited as in mancurses
23
26
  # _ show how many files 1-5 or 15 etc
24
27
  # _ toggle edit mode and view mode
25
- # _ long listing toggle + other toggles
28
+ # x long listing toggle + other toggles
26
29
  #
27
30
  # --- release first gem version after above are done ---
28
31
  #
@@ -496,7 +499,7 @@ def pad_display_file filename, renderer, config={}, &block
496
499
  listconfig.merge!(config)
497
500
  listconfig.delete(:row);
498
501
  listconfig.delete(:col);
499
- listconfig[:filename] = filename
502
+ #listconfig[:filename] = filename
500
503
  listconfig[:title] = filename
501
504
  listconfig[:row] = 0
502
505
  listconfig[:col] = 0
@@ -508,6 +511,7 @@ def pad_display_file filename, renderer, config={}, &block
508
511
  #lb = RubyCurses::List.new form, listconfig, &block
509
512
  lb = Cygnus::TextPad.new form, listconfig, &block
510
513
  lb.renderer renderer if renderer
514
+ lb.filename(filename, method(:get_file_contents))
511
515
  #lb.text(list)
512
516
  ## TODO FIXME hardocoding tmux format
513
517
  #lb.formatted_text(alist, :tmux)
@@ -756,19 +760,10 @@ end
756
760
  FileUtils.cd d
757
761
  display_dir
758
762
  end
759
- def filetype f
760
- text = `file #{f}`
761
- if text.include? "text"
762
- return :html if text.include?("HTML")
763
- return :text
764
- elsif text =~ /(archive|zip)/
765
- return :zip
766
- else
767
- return :directory if File.directory? f
768
- return :unknown
769
- end
770
- end
771
763
  def get_file_contents f
764
+ # escaping required for sending file name to shell command if there are spaces in file name
765
+ sf = Shellwords.escape(f)
766
+
772
767
  ft = filetype f
773
768
  case ft
774
769
  when :text
@@ -782,17 +777,17 @@ end
782
777
  end
783
778
  return lines
784
779
  when :html
785
- t = `html2text #{f} 2>&1`.split "\n"
780
+ t = `html2text #{sf} 2>&1`.split "\n"
786
781
  if t.first.include? "command not found"
787
782
  return File.open(f,"r").readlines
788
783
  end
789
784
  return t
790
785
  when :zip
791
- return `tar ztvf #{f}`.split("\n")
786
+ return `tar ztvf #{sf}`.split("\n")
792
787
  when :directory
793
788
  return Dir.glob("*")
794
789
  else
795
- return ["unknown type", `file #{f}`]
790
+ return ["unhandled type", `file #{sf}`]
796
791
  end
797
792
  end
798
793
  def show
@@ -1344,8 +1344,10 @@ def filetype f
1344
1344
  return :image
1345
1345
  elsif s.index "data"
1346
1346
  return :text
1347
+ else
1348
+ return :directory if File.directory? f
1347
1349
  end
1348
- nil
1350
+ return :unknown
1349
1351
  end
1350
1352
 
1351
1353
  def save_dir_pos
@@ -1518,7 +1520,7 @@ def insert_into_list dir, file
1518
1520
  #ix = $files.index(dir)
1519
1521
  #raise "something wrong can find #{dir}." unless ix
1520
1522
  #$files.insert ix, *file
1521
- $files.concat file
1523
+ $files.push *file
1522
1524
  end
1523
1525
  def get_important_files dir
1524
1526
  # checks various lists like visited_files and bookmarks
@@ -8,7 +8,7 @@
8
8
  # Author: rkumar http://github.com/rkumar/mancurses/
9
9
  # Date: 2011-11-09 - 16:59
10
10
  # License: Same as Ruby's License (http://www.ruby-lang.org/LICENSE.txt)
11
- # Last update: 2013-03-16 17:41
11
+ # Last update: 2013-03-21 14:10
12
12
  #
13
13
  # == CHANGES
14
14
  # == TODO
@@ -36,6 +36,7 @@ module Cygnus
36
36
  include BorderTitle
37
37
 
38
38
  dsl_accessor :suppress_border
39
+ dsl_accessor :print_footer
39
40
  attr_reader :current_index
40
41
  attr_reader :rows , :cols
41
42
  # You may pass height, width, row and col for creating a window otherwise a fullscreen window
@@ -178,15 +179,23 @@ module Cygnus
178
179
 
179
180
  # supply a filename as source for textpad
180
181
  # Reads up file into @content
181
-
182
- def filename(filename)
182
+ # One can optionally send in a method which takes a filename and returns an array of data
183
+ # This is required if you are processing files which are binary such as zip/archives and wish
184
+ # to print the contents. (e.g. cygnus gem sends in :get_file_contents).
185
+ # filename("a.c", method(:get_file_contents))
186
+ #
187
+ def filename(filename, reader=nil)
183
188
  @file = filename
184
189
  unless File.exists? filename
185
190
  alert "#{filename} does not exist"
186
191
  return
187
192
  end
188
193
  @filetype = File.extname filename
189
- @content = File.open(filename,"r").readlines
194
+ if reader
195
+ @content = reader.call(filename)
196
+ else
197
+ @content = File.open(filename,"r").readlines
198
+ end
190
199
  if @filetype == ""
191
200
  if @content.first.index("ruby")
192
201
  @filetype = ".rb"
@@ -201,18 +210,30 @@ module Cygnus
201
210
  # This will replace existing text
202
211
 
203
212
  ## XXX in list text returns the selected row, list returns the full thing, keep consistent
204
- def text lines
205
- raise "text() receiving null content" unless lines
213
+ def text(lines)
214
+ #raise "text() receiving null content" unless lines
215
+ return @content if lines.empty?
206
216
  @content = lines
207
217
  @_populate_needed = true
208
218
  @repaint_all = true
209
219
  init_vars
220
+ self
210
221
  end
211
222
  alias :list :text
212
223
  def content
213
224
  raise "content is nil " unless @content
214
225
  return @content
215
226
  end
227
+ alias :get_content :content
228
+
229
+ # print footer containing line and position
230
+ def print_foot #:nodoc:
231
+ @footer_attrib ||= Ncurses::A_REVERSE
232
+ footer = "R: #{@current_index+1}, C: #{@curpos+@pcol}, #{@list.length} lines "
233
+ $log.debug " print_foot calling printstring with #{@row} + #{@height} -1, #{@col}+2"
234
+ @graphic.printstring( @row + @height -1 , @col+2, footer, @color_pair || $datacolor, @footer_attrib)
235
+ @repaint_footer_required = false # 2010-01-23 22:55
236
+ end
216
237
 
217
238
  ## ---- the next 2 methods deal with printing chunks
218
239
  # we should put it int a common module and include it
@@ -255,6 +276,8 @@ module Cygnus
255
276
  end
256
277
  end
257
278
 
279
+ #
280
+ # pass in formatted text along with parser (:tmux or :ansi)
258
281
  def formatted_text text, fmt
259
282
  require 'rbcurse/core/include/chunk'
260
283
  @formatted_text = text
@@ -326,6 +349,10 @@ module Cygnus
326
349
  #@window.print_border @top, @left, @height-1, @width, clr
327
350
  @window.print_border_only @top, @left, @height-1, @width, clr
328
351
  print_title
352
+
353
+ @repaint_footer_required = true if @oldrow != @current_index
354
+ print_foot if @print_footer && !@suppress_borders && @repaint_footer_required
355
+
329
356
  @window.wrefresh
330
357
  end
331
358
  end
@@ -362,6 +389,9 @@ module Cygnus
362
389
  bind_key(?M, :middle_of_window)
363
390
  bind_key(?H, :top_of_window)
364
391
  bind_key(?w, :forward_word)
392
+ bind_key(?l, :cursor_forward)
393
+ bind_key(?h, :cursor_backward)
394
+ bind_key(?$, :cursor_eol)
365
395
  bind_key(KEY_ENTER, :fire_action_event)
366
396
  end
367
397
 
@@ -513,46 +543,20 @@ module Cygnus
513
543
  #
514
544
  #
515
545
  #
516
- # NOTE : if advancing pcol one has to clear the pad or something or else
517
- # there'll be older content on the right side.
518
546
  #
519
547
  def handle_key ch
520
548
  return :UNHANDLED unless @content
521
549
  map_keys unless @mapped_keys
522
550
 
523
- @maxrow = @content_rows - @rows
524
- @maxcol = @content_cols - @cols
525
551
 
526
- # need to understand the above line, can go below zero.
527
- # all this seems to work fine in padreader.rb in util.
528
- # somehow maxcol going to -33
529
552
  @oldrow = @prow
530
553
  @oldcol = @pcol
531
554
  $log.debug "XXX: PAD got #{ch} prow = #{@prow}"
532
555
  begin
533
556
  case ch
534
- when key(?l)
535
- # TODO take multipler
536
- #@pcol += 1
537
- if @curpos < @cols
538
- @curpos += 1
539
- end
540
- when key(?$)
541
- #@pcol = @maxcol - 1
542
- @curpos = [@content[@current_index].size, @cols].min
543
- when key(?h)
544
- # TODO take multipler
545
- if @curpos > 0
546
- @curpos -= 1
547
- end
548
- #when key(?0)
549
- #@curpos = 0
550
557
  when ?0.getbyte(0)..?9.getbyte(0)
551
558
  if ch == ?0.getbyte(0) && $multiplier == 0
552
- # copy of C-a - start of line
553
- @repaint_required = true if @pcol > 0 # tried other things but did not work
554
- @pcol = 0
555
- @curpos = 0
559
+ cursor_bol
556
560
  return 0
557
561
  end
558
562
  # storing digits entered so we can multiply motion actions
@@ -584,7 +588,7 @@ module Cygnus
584
588
  return :UNHANDLED if ret == :UNHANDLED
585
589
  end
586
590
  rescue => err
587
- $log.error " TEXTPAD ERROR 111 #{err} "
591
+ $log.error " TEXTPAD ERROR 591 #{err} "
588
592
  $log.debug( err) if err
589
593
  $log.debug(err.backtrace.join("\n")) if err
590
594
  textdialog ["Error in TextPad: #{err} ", *err.backtrace], :title => "Exception"
@@ -595,16 +599,25 @@ module Cygnus
595
599
  end
596
600
  return 0
597
601
  end # while loop
602
+
603
+ #
604
+ # event when user hits enter on a row, user would bind :PRESS
605
+ #
598
606
  def fire_action_event
599
607
  return if @content.nil? || @content.size == 0
600
608
  require 'rbcurse/core/include/ractionevent'
601
609
  aev = TextActionEvent.new self, :PRESS, current_value().to_s, @current_index, @curpos
602
610
  fire_handler :PRESS, aev
603
611
  end
612
+ #
613
+ # returns current value (what cursor is on)
604
614
  def current_value
605
615
  @content[@current_index]
606
616
  end
607
617
  #
618
+ # execute binding when a row is entered, used more in lists to display some text
619
+ # in a header or footer as one traverses
620
+ #
608
621
  def on_enter_row arow
609
622
  return if @content.nil? || @content.size == 0
610
623
  require 'rbcurse/core/include/ractionevent'
@@ -623,16 +636,22 @@ module Cygnus
623
636
  FFI::NCurses.delwin(@pad) if @pad # when do i do this ? FIXME
624
637
  @pad = nil
625
638
  end
639
+ #
640
+ # return true if the given row is visible
626
641
  def is_visible? index
627
642
  j = index - @prow #@toprow
628
643
  j >= 0 && j <= @scrollatrows
629
644
  end
645
+ #
646
+ # called when this widget is entered, by form
630
647
  def on_enter
631
648
  set_form_row
632
649
  end
650
+ # called by form
633
651
  def set_form_row
634
652
  setrowcol @lastrow, @lastcol
635
653
  end
654
+ # called by form
636
655
  def set_form_col
637
656
  end
638
657
 
@@ -648,15 +667,6 @@ module Cygnus
648
667
  @current_index = @content.count()-1 if @current_index > @content.count()-1
649
668
  ensure_visible
650
669
 
651
- #$status_message.value = "visible #{@prow} , #{@current_index} "
652
- #unless is_visible? @current_index
653
- #if @prow > @current_index
654
- ##$status_message.value = "1 #{@prow} > #{@current_index} "
655
- #@prow -= 1
656
- #else
657
- #end
658
- #end
659
- #end
660
670
  check_prow
661
671
  #$log.debug "XXX: PAD BOUNDS ci:#{@current_index} , old #{@oldrow},pr #{@prow}, max #{@maxrow} pcol #{@pcol} maxcol #{@maxcol}"
662
672
  @crow = @current_index + r - @prow
@@ -674,14 +684,16 @@ module Cygnus
674
684
  @repaint_required = true
675
685
  end
676
686
  end
687
+ #
688
+ # save last cursor position so when reentering, cursor can be repositioned
677
689
  def lastcurpos r,c
678
690
  @lastrow = r
679
691
  @lastcol = c
680
692
  end
681
693
 
682
694
 
683
- # check that prow and pcol are within bounds
684
-
695
+ # check that prow and pcol are within bounds
696
+ #
685
697
  def check_prow
686
698
  @prow = 0 if @prow < 0
687
699
  @pcol = 0 if @pcol < 0
@@ -700,17 +712,13 @@ module Cygnus
700
712
  # the pad shows earlier stuff.
701
713
  #
702
714
  return
703
- # the following was causing bugs if content was smaller than pad
704
- if @prow > @maxrow-1
705
- @prow = @maxrow-1
706
- end
707
- if @pcol > @maxcol-1
708
- @pcol = @maxcol-1
709
- end
710
715
  end
711
716
  public
712
717
  ##
713
718
  # Ask user for string to search for
719
+ # This uses the dialog, but what if user wants the old style.
720
+ # Isn't there a cleaner way to let user override style, or allow user
721
+ # to use own UI for getting pattern and then passing here.
714
722
  def ask_search
715
723
  str = get_string("Enter pattern: ")
716
724
  return if str.nil?
@@ -768,6 +776,9 @@ module Cygnus
768
776
  @prow = @current_index
769
777
  end
770
778
  end
779
+ #
780
+ # jumps cursor to next work, like vim's w key
781
+ #
771
782
  def forward_word
772
783
  $multiplier = 1 if !$multiplier || $multiplier == 0
773
784
  line = @current_index
@@ -793,11 +804,46 @@ module Cygnus
793
804
  @current_index = line
794
805
  @curpos = pos
795
806
  ensure_visible
796
- #@buffer = @list[@current_index].to_s
797
- #set_form_row
798
- #set_form_col pos
799
807
  @repaint_required = true
800
808
  end
809
+ #
810
+ # move cursor forward by one char (currently will not pan)
811
+ def cursor_forward
812
+ $multiplier = 1 if $multiplier == 0
813
+ if @curpos < @cols
814
+ @curpos += $multiplier
815
+ if @curpos > @cols
816
+ @curpos = @cols
817
+ end
818
+ @repaint_required = true
819
+ end
820
+ $multiplier = 0
821
+ end
822
+ #
823
+ # move cursor backward by one char (currently will not pan)
824
+ def cursor_backward
825
+ $multiplier = 1 if $multiplier == 0
826
+ if @curpos > 0
827
+ @curpos -= $multiplier
828
+ @curpos = 0 if @curpos < 0
829
+ @repaint_required = true
830
+ end
831
+ $multiplier = 0
832
+ end
833
+ # moves cursor to end of line also panning window if necessary
834
+ def cursor_eol
835
+ @pcol = @content_cols - @cols - 1
836
+ @curpos = @content[@current_index].size
837
+ @repaint_required = true
838
+ end
839
+ #
840
+ # moves cursor to start of line, panning if required
841
+ def cursor_bol
842
+ # copy of C-a - start of line
843
+ @repaint_required = true if @pcol > 0
844
+ @pcol = 0
845
+ @curpos = 0
846
+ end
801
847
 
802
848
  end # class textpad
803
849
 
@@ -810,7 +856,7 @@ module Cygnus
810
856
  #cp = $datacolor
811
857
  cp = get_color($datacolor, fg, bg)
812
858
  ## XXX believe it or not, the next line can give you "invalid byte sequence in UTF-8
813
- # even when processing filename at times.
859
+ # even when processing filename at times. Or if its an mp3 or non-text file.
814
860
  if text =~ /^\s*# / || text =~ /^\s*## /
815
861
  fg = :red
816
862
  #att = BOLD
@@ -1,3 +1,3 @@
1
1
  module Cygnus
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.6"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cygnus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-19 00:00:00.000000000 Z
12
+ date: 2013-03-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -76,7 +76,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
76
76
  version: '0'
77
77
  segments:
78
78
  - 0
79
- hash: 849870953013136634
79
+ hash: 2432678409323259259
80
80
  required_rubygems_version: !ruby/object:Gem::Requirement
81
81
  none: false
82
82
  requirements:
@@ -85,7 +85,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
85
  version: '0'
86
86
  segments:
87
87
  - 0
88
- hash: 849870953013136634
88
+ hash: 2432678409323259259
89
89
  requirements: []
90
90
  rubyforge_project:
91
91
  rubygems_version: 1.8.25