rbcurse 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (113) hide show
  1. data/CHANGELOG +20 -0
  2. data/NOTES +45 -0
  3. data/README.markdown +47 -68
  4. data/TODO2.txt +29 -32
  5. data/VERSION +1 -1
  6. data/examples/abasiclist.rb +2 -0
  7. data/examples/app.rb +2 -2
  8. data/examples/app.sample +10 -0
  9. data/examples/appemail.rb +29 -2
  10. data/examples/appemaillb.rb +1 -1
  11. data/examples/appgcompose.rb +9 -2
  12. data/examples/appmethods.rb +69 -0
  13. data/examples/atree.rb +2 -2
  14. data/examples/menu1.rb +1 -1
  15. data/examples/newtesttabp.rb +8 -5
  16. data/examples/qdfilechooser.rb +6 -3
  17. data/examples/rfe.rb +71 -16
  18. data/examples/rmail.rb +76 -7
  19. data/examples/sqlc.rb +14 -3
  20. data/examples/sqlm.rb +3 -3
  21. data/examples/term.rb +48 -0
  22. data/examples/term2.rb +48 -21
  23. data/examples/test1.rb +2 -2
  24. data/examples/test2.rb +162 -71
  25. data/examples/testapp.rb +2 -2
  26. data/examples/testapp2.rb +1 -1
  27. data/examples/testchars.rb +47 -46
  28. data/examples/testcombo.rb +2 -2
  29. data/examples/testkeypress.rb +2 -2
  30. data/examples/testlistbox.rb +2 -2
  31. data/examples/testmenu.rb +1 -1
  32. data/examples/testmulticomp.rb +1 -1
  33. data/examples/testmultispl.rb +1 -1
  34. data/examples/testree.rb +1 -22
  35. data/examples/testscroller.rb +2 -2
  36. data/examples/testscrolllb.rb +2 -2
  37. data/examples/testscrollp.rb +2 -2
  38. data/examples/testscrollta.rb +2 -2
  39. data/examples/testscrolltable.rb +2 -2
  40. data/examples/testsplit.rb +2 -2
  41. data/examples/testsplit2.rb +2 -2
  42. data/examples/testsplit3.rb +2 -2
  43. data/examples/testsplit3_1.rb +2 -2
  44. data/examples/testsplit3a.rb +2 -2
  45. data/examples/testsplit3b.rb +2 -2
  46. data/examples/testsplitta.rb +2 -2
  47. data/examples/testsplittv.rb +2 -2
  48. data/examples/testsplittvv.rb +2 -2
  49. data/examples/testtable.rb +2 -2
  50. data/examples/testtabp.rb +2 -2
  51. data/examples/testtodo.rb +1 -1
  52. data/examples/testtpane.rb +2 -2
  53. data/examples/testtpane2.rb +2 -2
  54. data/examples/testtpanetable.rb +6 -5
  55. data/examples/testvimsplit.rb +2 -2
  56. data/examples/viewtodo.rb +1 -1
  57. data/lib/rbcurse.rb +2 -1
  58. data/lib/rbcurse/action.rb +1 -1
  59. data/lib/rbcurse/app.rb +51 -14
  60. data/lib/rbcurse/applicationheader.rb +1 -1
  61. data/lib/rbcurse/celleditor.rb +1 -1
  62. data/lib/rbcurse/colormap.rb +11 -11
  63. data/lib/rbcurse/comboboxcellrenderer.rb +1 -1
  64. data/lib/rbcurse/extras/bottomline.rb +22 -10
  65. data/lib/rbcurse/extras/directorylist.rb +14 -1
  66. data/lib/rbcurse/extras/divider.rb +1 -1
  67. data/lib/rbcurse/extras/listselectable.rb +2 -2
  68. data/lib/rbcurse/extras/masterdetail.rb +3 -1
  69. data/lib/rbcurse/extras/scrollbar.rb +1 -1
  70. data/lib/rbcurse/extras/stdscrwindow.rb +75 -24
  71. data/lib/rbcurse/extras/tabular.rb +8 -3
  72. data/lib/rbcurse/extras/viewer.rb +18 -2
  73. data/lib/rbcurse/io.rb +26 -13
  74. data/lib/rbcurse/keylabelprinter.rb +3 -2
  75. data/lib/rbcurse/listcellrenderer.rb +1 -0
  76. data/lib/rbcurse/listkeys.rb +3 -2
  77. data/lib/rbcurse/listselectable.rb +4 -3
  78. data/lib/rbcurse/rbasiclistbox.rb +12 -5
  79. data/lib/rbcurse/rcombo.rb +2 -2
  80. data/lib/rbcurse/rcommandwindow.rb +5 -3
  81. data/lib/rbcurse/rdialogs.rb +8 -2
  82. data/lib/rbcurse/rlistbox.rb +10 -2
  83. data/lib/rbcurse/rmenu.rb +4 -4
  84. data/lib/rbcurse/rmessagebox.rb +10 -5
  85. data/lib/rbcurse/rmulticontainer.rb +21 -8
  86. data/lib/rbcurse/rmultisplit.rb +2 -2
  87. data/lib/rbcurse/rmultitextview.rb +5 -3
  88. data/lib/rbcurse/rpopupmenu.rb +3 -3
  89. data/lib/rbcurse/rprogress.rb +3 -2
  90. data/lib/rbcurse/rscrollform.rb +2 -2
  91. data/lib/rbcurse/rscrollpane.rb +9 -9
  92. data/lib/rbcurse/rsplitpane.rb +2 -2
  93. data/lib/rbcurse/rsplitpane2.rb +2 -2
  94. data/lib/rbcurse/rtabbedpane.rb +48 -12
  95. data/lib/rbcurse/rtabbedwindow.rb +4 -4
  96. data/lib/rbcurse/rtable.rb +6 -3
  97. data/lib/rbcurse/rtextarea.rb +8 -8
  98. data/lib/rbcurse/rtextview.rb +3 -2
  99. data/lib/rbcurse/rtree.rb +2 -2
  100. data/lib/rbcurse/rviewport.rb +2 -2
  101. data/lib/rbcurse/rvimsplit.rb +3 -2
  102. data/lib/rbcurse/rwidget.rb +63 -26
  103. data/lib/rbcurse/scrollable.rb +1 -1
  104. data/lib/rbcurse/table/tablecellrenderer.rb +1 -1
  105. data/lib/rbcurse/table/tabledatecellrenderer.rb +1 -1
  106. data/lib/rbcurse/tree/treemodel.rb +1 -1
  107. data/lib/rbcurse/vieditable.rb +1 -0
  108. data/lib/ver/ncurses.rb +122 -4
  109. data/lib/ver/panel.rb +162 -0
  110. data/lib/ver/window.rb +262 -120
  111. metadata +19 -7
  112. data/examples/keytest.rb +0 -178
  113. data/examples/mpad2.rb +0 -156
data/examples/appemail.rb CHANGED
@@ -5,6 +5,7 @@ require './rmail'
5
5
  # I've loaded it here ... http://gist.github.com/634166 with line encoding
6
6
  # You need to fix paths of local mbox files
7
7
 
8
+ # this will go into top namespace so will conflict with other apps!
8
9
  def testchoose
9
10
  # list filters as you type
10
11
  $log.debug "called test1 " if $log.debug?
@@ -15,7 +16,7 @@ def testnumberedmenu
15
16
  list1[0] = %w{ ruby ruby1.9 ruby1.8.x jruby rubinius ROR }
16
17
  str = numbered_menu list1, { :title => "Languages: ", :prompt => "Select :" }
17
18
  $log.debug "17 We got #{str.class} "
18
- say "We got #{str} "
19
+ say "We got #{str} " # will get overwritten by message() as soon as repaint happens
19
20
  end
20
21
  def testdisplay_list
21
22
  # scrollable list
@@ -70,7 +71,7 @@ App.new do
70
71
  @tv = nil
71
72
  borderattrib = :reverse
72
73
  @header = app_header "rbcurse 1.2.0", :text_center => "Yet Another Email Client that sucks", :text_right =>"", :color => :black, :bgcolor => :white#, :attr => Ncurses::A_BLINK
73
- message "Press F1 to exit ...................................................."
74
+ message "Press F10 to exit ...................................................."
74
75
 
75
76
 
76
77
  stack :margin_top => 1, :margin => 0, :width => :EXPAND do
@@ -106,6 +107,32 @@ App.new do
106
107
  $log.debug "called test1 " if $log.debug?
107
108
  str = choose "*.rb", :title => "Files", :prompt => "Choose a file: "
108
109
  end
110
+ def help_text
111
+ <<-eos
112
+ APPEMAIL HELP
113
+
114
+ This is some help text for appemail.
115
+ We are testing out this feature.
116
+
117
+ Alt-x - Command mode (<tab> to see commands and select)
118
+ : - Command mode
119
+ <Enter> - Display mail headers for mailbox
120
+ Display body for selected header
121
+ F3 - Enable sidebars in order to change size of windows (toggle)
122
+ F10 - Quit application
123
+
124
+ Some commands for using bottom of screen as vim and emacs do.
125
+
126
+ testchoose - filter directory list as you type
127
+ testdir - vim style, tabbing completes matching files
128
+ testnumberedmenu - use menu indexes to select options
129
+ testdisplaylist - display a list at bottom of screen
130
+ testdisplaytext - display text at bottom
131
+
132
+ -----------------------------------------------------------------------
133
+ Hope you enjoyed this help.
134
+ eos
135
+ end
109
136
  @vim.set_left_component @dirs
110
137
 
111
138
 
@@ -245,7 +245,7 @@ App.new do
245
245
  @tv = nil
246
246
  borderattrib = :reverse
247
247
  @header = app_header "rbcurse 1.2.0", :text_center => "Yet Another Email Client that sucks", :text_right =>"", :color => :black, :bgcolor => :white#, :attr => Ncurses::A_BLINK
248
- message "Press F1 to exit ...................................................."
248
+ message "Press F10 to exit ...................................................."
249
249
 
250
250
 
251
251
 
@@ -261,7 +261,8 @@ module AppgCompose
261
261
  end
262
262
  unless @username
263
263
  ww = ENV['GMAIL_USER']
264
- ww << "@gmail.com" if ww
264
+ #ww << "@gmail.com" if ww # suddnely giving frozen string error
265
+ ww = ww + "@gmail.com" if ww
265
266
  @username = ask("Username: ") { |q| q.default = ww }
266
267
  end
267
268
  unless @password
@@ -274,7 +275,13 @@ module AppgCompose
274
275
  end
275
276
  unless @gmail
276
277
  say "Connecting to gmail..."
277
- @gmail = Gmail.connect!(@username, @password)
278
+ begin
279
+ @gmail = Gmail.connect!(@username, @password)
280
+ rescue => ex
281
+ @password = @username = nil
282
+ $log.debug( ex) if ex
283
+ $log.debug(ex.backtrace.join("\n")) if ex
284
+ end
278
285
  end
279
286
  unless @gmail
280
287
  say_with_pause "Cannot proceeed without connection" unless @gmail
@@ -0,0 +1,69 @@
1
+ module RubyCurses
2
+ module Utils
3
+ def _suspend clear=true
4
+ return unless block_given?
5
+ Ncurses.def_prog_mode
6
+ if clear
7
+ Ncurses.endwin
8
+ # NOTE: avoid false since screen remains half off
9
+ # too many issues
10
+ else
11
+ system "/bin/stty sane"
12
+ end
13
+ yield if block_given?
14
+ Ncurses.reset_prog_mode
15
+ if !clear
16
+ # Hope we don't screw your terminal up with this constantly.
17
+ VER::stop_ncurses
18
+ VER::start_ncurses
19
+ #@form.reset_all # not required
20
+ end
21
+ @form.repaint
22
+ @window.wrefresh
23
+ Ncurses::Panel.update_panels
24
+ end
25
+ def suspend
26
+ _suspend(false) do
27
+ system("tput cup 26 0")
28
+ system("tput ed")
29
+ system("echo Enter C-d to return to application")
30
+ system (ENV['PS1']='\s-\v\$ ') if ENV['SHELL']== '/bin/bash'
31
+ system(ENV['SHELL']);
32
+ end
33
+ end
34
+
35
+ def display_app_help help_array= nil
36
+ if help_array
37
+ arr = help_array
38
+ elsif respond_to? :help_text
39
+ arr = help_text
40
+ else
41
+ arr = []
42
+ arr << " NO HELP SPECIFIED FOR APP #{self} "
43
+ arr << " "
44
+ arr << " --- General help --- "
45
+ arr << " F10 - exit application "
46
+ arr << " Alt-x - select commands "
47
+ arr << " : - select commands "
48
+ arr << " "
49
+ end
50
+ case arr
51
+ when String
52
+ arr = arr.split("\n")
53
+ when Array
54
+ end
55
+ w = arr.max_by(&:length).length
56
+
57
+ require 'rbcurse/extras/viewer'
58
+ RubyCurses::Viewer.view(arr, :layout => [2, 10, [4+arr.size, 24].min, w+2],:close_key => KEY_RETURN, :title => "<Enter> to close", :print_footer => true) do |t|
59
+ # you may configure textview further here.
60
+ #t.suppress_borders true
61
+ #t.color = :black
62
+ #t.bgcolor = :white
63
+ # or
64
+ t.attr = :reverse
65
+ end
66
+ end
67
+ end # utils
68
+ end # module RubyC
69
+ include RubyCurses::Utils
data/examples/atree.rb CHANGED
@@ -5,7 +5,8 @@ App.new do
5
5
  message "Press Enter to expand/collapse"
6
6
 
7
7
  stack :margin_top => 2, :margin => 5, :width => 30 do
8
- tree :height => 10, :title => "ruby way" do
8
+ tm = nil
9
+ atree = tree :height => 10, :title => "ruby way" do
9
10
  root "root" do
10
11
  branch "hello" do
11
12
  leaf "ruby"
@@ -17,7 +18,6 @@ App.new do
17
18
  end
18
19
  end
19
20
 
20
-
21
21
  # using a Hash
22
22
  model = { :ruby => [ "jruby", {:mri => %W[ 1.8.6 1.8.7]}, {:yarv => %W[1.9.1 1.9.2]}, "rubinius", "macruby" ], :python => %W[ cpython jython laden-swallow ] }
23
23
  tree :data => model, :title => "Hash"
data/examples/menu1.rb CHANGED
@@ -34,7 +34,7 @@ require 'rbcurse/app'
34
34
  end
35
35
  end
36
36
  end # menubar
37
- mb.toggle_key = KEY_F2
37
+ mb.toggle_key = FFI::NCurses::KEY_F2
38
38
  @form.set_menu_bar mb
39
39
  stack :margin_top => 10, :margin => 5 do
40
40
  field "a field", :attr => 'reverse', :block_event => :CHANGE do |e|
@@ -2,7 +2,7 @@
2
2
  #
3
3
  #$LOAD_PATH << "/Users/rahul/work/projects/rbcurse/"
4
4
  require 'rubygems'
5
- require 'ncurses'
5
+ #require 'ncurses' # FFI
6
6
  require 'logger'
7
7
  require 'rbcurse'
8
8
  #require 'rbcurse/newtabbedpane'
@@ -27,7 +27,8 @@ class TestTabbedPane
27
27
  button_type :ok
28
28
  end
29
29
  @tab1 = @tp.add_tab "&Language"
30
- f1 = @tab1.form
30
+ #f1 = @tab1.form
31
+ f1 = @tp.form @tab1
31
32
  #$radio = Variable.new
32
33
  radio1 = RadioButton.new f1 do
33
34
  #variable $radio
@@ -60,7 +61,8 @@ class TestTabbedPane
60
61
  col 5
61
62
  end
62
63
  @tab2 = @tp.add_tab "&Settings"
63
- f2 = @tab2.form
64
+ #f2 = @tab2.form
65
+ f2 = @tp.form @tab2
64
66
  r = 4
65
67
  butts = [ "Use &HTTP/1.0", "Use &frames", "&Use SSL" ]
66
68
  bcodes = %w[ HTTP, FRAMES, SSL ]
@@ -74,7 +76,8 @@ class TestTabbedPane
74
76
  end
75
77
  end
76
78
  @tab3 = @tp.add_tab "&Editors"
77
- f3 = @tab3.form
79
+ #f3 = @tab3.form
80
+ f3 = @tp.form @tab3
78
81
  butts = %w[ &Vim E&macs &Jed &Other ]
79
82
  bcodes = %w[ VIM EMACS JED OTHER]
80
83
  row = 4
@@ -106,7 +109,7 @@ if $0 == __FILE__
106
109
  begin
107
110
  # XXX update with new color and kb
108
111
  VER::start_ncurses # this is initializing colors via ColorMap.setup
109
- $log = Logger.new("view.log")
112
+ $log = Logger.new("rbc13.log")
110
113
  $log.level = Logger::DEBUG
111
114
  n = TestTabbedPane.new
112
115
  n.run
@@ -2,7 +2,7 @@
2
2
  # a quick dirty file chooser in 2 lines of code.
3
3
  #$LOAD_PATH << "/Users/rahul/work/projects/rbcurse/"
4
4
  require 'rubygems'
5
- require 'ncurses'
5
+ #require 'ncurses' # FFI
6
6
  require 'logger'
7
7
  require 'ver/ncurses'
8
8
  require 'ver/window'
@@ -59,10 +59,13 @@ class QDFileChooser
59
59
  default_button 0
60
60
  button_type :ok_cancel
61
61
  end
62
+ #$log.debug "MBOX :selected #{@listb.selected_item}, #{@listb[@listb.getvalue[0]]} "
62
63
  return @mb.selected_index == 0 ? :OK : :CANCEL
63
- $log.debug "MBOX :selected #{@listb.selected_item}"
64
64
  end
65
65
  def get_selected_file
66
- return @mb.selected_index == 0 ? @listb.selected_item : nil
66
+ #return @mb.selected_index == 0 ? @listb.selected_item : nil
67
+ #return @mb.selected_index == 0 ? @listb[@listb.getvalue[0]] : nil
68
+ # return either the selected_value or if user pressed okay, then focussed item
69
+ return @mb.selected_index == 0 ? @listb.selected_value || @listb.selected_item : nil
67
70
  end
68
71
  end
data/examples/rfe.rb CHANGED
@@ -109,6 +109,7 @@ class FileExplorer
109
109
  list.list_data_model.remove_all
110
110
  #list.list_data_model.insert 0, *fl
111
111
  list.list_data_model.insert 0, *flist
112
+ list.set_form_row
112
113
  end
113
114
  def sort key, reverse=false
114
115
  # remove parent before sorting, keep at top
@@ -494,13 +495,13 @@ class RFe
494
495
  footer_attrib 'bold'
495
496
  end
496
497
  #content = File.open(fp,"r").readlines
498
+ begin
497
499
  @textview.set_content content #, :WRAP_WORD
498
500
  @v_form.repaint
499
501
  @v_window.wrefresh
500
502
  Ncurses::Panel.update_panels
501
- begin
502
503
  while((ch = @v_window.getchar()) != ?\C-q.getbyte(0) )
503
- break if ch == KEY_F3
504
+ break if ch == FFI::NCurses::KEY_F3
504
505
  @v_form.handle_key ch
505
506
  @v_form.repaint
506
507
  ##@v_window.wrefresh
@@ -677,9 +678,10 @@ class RFe
677
678
  @form.bind_key(?\C-f){
678
679
  @klp.mode :file
679
680
  @klp.repaint
680
- ## FIXME chr could fail !!
681
681
  while((ch = @window.getchar()) != ?\C-c.getbyte(0) )
682
- if "cmdsuvrex".index(ch.chr) == nil
682
+ if ch < 33 || ch > 126
683
+ Ncurses.beep
684
+ elsif "cmdsuvrex".index(ch.chr) == nil
683
685
  Ncurses.beep
684
686
  else
685
687
  opt_file ch.chr
@@ -692,9 +694,10 @@ class RFe
692
694
  @klp.mode :dir
693
695
  @klp.repaint
694
696
  keys = @klp.get_current_keys
695
- ## FIXME chr could fail !!
696
697
  while((ch = @window.getchar()) != ?\C-c.getbyte(0) )
697
- if !keys.include?(ch.chr)
698
+ if ch < 33 || ch > 126
699
+ Ncurses.beep
700
+ elsif !keys.include?(ch.chr)
698
701
  Ncurses.beep
699
702
  else
700
703
  opt_dir ch.chr
@@ -714,28 +717,80 @@ class RFe
714
717
  rescue Interrupt
715
718
  end
716
719
  }
717
- @form.bind_key(KEY_F3){
718
- view()
720
+ @form.bind_key(FFI::NCurses::KEY_F3){
721
+ begin
722
+ view()
723
+ rescue => err
724
+ alert err.to_s
725
+ end
726
+ }
727
+ @form.bind_key(FFI::NCurses::KEY_F1){
728
+ #Io.view(["this is some help text","hello there"])
729
+ arr = []
730
+ arr << " FILE BROWSER HELP "
731
+ arr << " "
732
+ arr << " <tab> - switch between windows"
733
+ arr << " <enter> - open dir"
734
+ arr << " F3 - view file/dir/zip contents (toggle) "
735
+ arr << " F4 - edit file content"
736
+ arr << " <char> - first file starting with <char>"
737
+ arr << " "
738
+ arr << " < > ^ V - move help window "
739
+ arr << " "
740
+ w = arr.max_by(&:length).length
741
+ #arr = ["this is some help text","hello there"]
742
+
743
+ require 'rbcurse/extras/viewer'
744
+ RubyCurses::Viewer.view(arr, :layout => [10,10, 4+arr.size, w+2],:close_key => KEY_RETURN, :title => "<Enter> to close", :print_footer => false) do |t|
745
+ # you may configure textview further here.
746
+ #t.suppress_borders true
747
+ #t.color = :black
748
+ #t.bgcolor = :white
749
+ # or
750
+ t.attr = :reverse
751
+ #t.bind_key(KEY_F1){ alert "closing up!"; throw :close }
752
+ # just for kicks move window around
753
+ t.bind_key('<'){ f = t.form.window; c = f.left - 1; f.hide; f.mvwin(f.top, c); f.show;
754
+ f.reset_layout([f.height, f.width, f.top, c]);
755
+ }
756
+ t.bind_key('>'){ f = t.form.window; c = f.left + 1; f.hide; f.mvwin(f.top, c);
757
+ f.reset_layout([f.height, f.width, f.top, c]); f.show;
758
+ }
759
+ t.bind_key('^'){ f = t.form.window; c = f.top - 1 ; f.hide; f.mvwin(c, f.left);
760
+ f.reset_layout([f.height, f.width, c, f.left]) ; f.show;
761
+ }
762
+ t.bind_key('V'){ f = t.form.window; c = f.top + 1 ; f.hide; f.mvwin(c, f.left);
763
+ f.reset_layout([f.height, f.width, c, f.left]); f.show;
764
+ }
765
+ end
719
766
  }
720
- @form.bind_key(KEY_F4){
767
+ @form.bind_key(FFI::NCurses::KEY_F4){
721
768
  edit()
722
769
  }
723
- @form.bind_key(KEY_F6){
770
+ @form.bind_key(FFI::NCurses::KEY_F6){
724
771
  selected_index, sort_key, reverse, case_sensitive = sort_popup
725
772
  if selected_index == 0
726
773
  @current_list.sort(sort_key, reverse)
727
774
  end
728
775
  }
729
- @form.bind_key(KEY_F5){
776
+ @form.bind_key(FFI::NCurses::KEY_F5){
730
777
  filter()
731
778
  }
732
- @form.bind_key(KEY_F7){
779
+ @form.bind_key(FFI::NCurses::KEY_F7){
733
780
  grep_popup()
734
781
  }
735
- @form.bind_key(KEY_F8){
782
+ @form.bind_key(FFI::NCurses::KEY_F8){
736
783
  system_popup()
737
784
  }
738
- @form.bind_key(?\C-m){
785
+ # will no longer come here. list event has to be used
786
+ #@form.bind_key(?\C-m){ # listbox has eaten it up
787
+ @lista.list.bind(:PRESS){
788
+ dir = @current_list.filepath
789
+ if File.directory? @current_list.filepath
790
+ @current_list.change_dir dir
791
+ end
792
+ }
793
+ @listb.list.bind(:PRESS){
739
794
  dir = @current_list.filepath
740
795
  if File.directory? @current_list.filepath
741
796
  @current_list.change_dir dir
@@ -997,8 +1052,8 @@ if $0 == __FILE__
997
1052
  begin
998
1053
  # Initialize curses
999
1054
  VER::start_ncurses # this is initializing colors via ColorMap.setup
1000
- #$log = Logger.new("view.log")
1001
- $log = Logger.new(ENV['LOGDIR'] || "" + "view.log")
1055
+ #$log = Logger.new("rbc13.log")
1056
+ $log = Logger.new(ENV['LOGDIR'] || "" + "rbc13.log")
1002
1057
 
1003
1058
  $log.level = Logger::DEBUG
1004
1059
 
data/examples/rmail.rb CHANGED
@@ -1,14 +1,12 @@
1
1
  #!/usr/bin/env ruby -w
2
+ # WARNING. This file is required by appemail.rb.
2
3
  #
3
- # this will only work in 1.8.7, since mailread.rb is not present after that
4
- # supply the path of any mbox file such as ~/mbox or ~/mail/read-mail
4
+ # Supply the path of any mbox file such as ~/mbox or ~/mail/read-mail
5
5
  # and see a listing of mails, then give a msg number and see body
6
6
  # You may run this with -EASCII-8BIT if it crashes with a UTF8 error.
7
- require 'mailread'
7
+ #require 'mailread' # i've copied it in here 2011-09-18
8
8
  require 'date'
9
9
 
10
- #BOLD = "\e[1m"
11
- #CLEAR = "\e[0m"
12
10
  # a sample mail formatter class
13
11
  # Does formatting just like alpine
14
12
  # Each message is passed in the format method, thus this is reused
@@ -82,13 +80,14 @@ class Mbox
82
80
  # takes a mailbox name, e,g., mbox
83
81
  # Does not update, since this is just a demo
84
82
  def initialize folder
85
- raise ArgumentError, "#{folder} not a valid file" if !File.exists? folder
83
+ raise ArgumentError, "#{folder} not a valid file. Pls supply correct path." if !File.exists? folder
86
84
  @folder = folder
87
85
  @unread_count = 0
88
86
  @read_count = 0
89
87
  @mboxh = {}
90
88
  @mbox_counts = {}
91
89
  @mails = nil
90
+ @formatter = nil
92
91
  parse
93
92
  end
94
93
  def parse &block # :yields: Mail
@@ -163,14 +162,84 @@ class MboxManager
163
162
  @mails
164
163
  end
165
164
  end
165
+
166
+ # The Mail class represents an internet mail message (as per RFC822, RFC2822)
167
+ # with headers and a body.
168
+ class Mail
169
+
170
+ # Create a new Mail where +f+ is either a stream which responds to gets(),
171
+ # or a path to a file. If +f+ is a path it will be opened.
172
+ #
173
+ # The whole message is read so it can be made available through the #header,
174
+ # #[] and #body methods.
175
+ #
176
+ # The "From " line is ignored if the mail is in mbox format.
177
+ def initialize(f)
178
+ unless defined? f.gets
179
+ f = open(f, "r")
180
+ opened = true
181
+ end
182
+
183
+ @header = {}
184
+ @body = []
185
+ begin
186
+ while line = f.gets()
187
+ line.chop!
188
+ # Added encode by RK since crashing with UTF-8 error
189
+ line = line.encode("ASCII-8BIT", :invalid => :replace, :undef => :replace, :replace => "?")
190
+ next if /^From /=~line # skip From-line
191
+ break if /^$/=~line # end of header
192
+
193
+ if /^(\S+?):\s*(.*)/=~line
194
+ (attr = $1).capitalize!
195
+ @header[attr] = $2
196
+ elsif attr
197
+ line.sub!(/^\s*/, '')
198
+ @header[attr] += "\n" + line
199
+ end
200
+ end
201
+
202
+ return unless line
203
+
204
+ while line = f.gets()
205
+ # Added encode by RK since crashing with UTF-8 error
206
+ line = line.encode("ASCII-8BIT", :invalid => :replace, :undef => :replace, :replace => "?")
207
+ #line = line.encode('ASCII-8BIT') # added RK
208
+ break if /^From /=~line
209
+ @body.push(line)
210
+ end
211
+ ensure
212
+ f.close if opened
213
+ end
214
+ end
215
+
216
+ # Return the headers as a Hash.
217
+ def header
218
+ return @header
219
+ end
220
+
221
+ # Return the message body as an Array of lines
222
+ def body
223
+ return @body
224
+ end
225
+
226
+ # Return the header corresponding to +field+.
227
+ #
228
+ # Matching is case-insensitive.
229
+ def [](field)
230
+ @header[field.capitalize]
231
+ end
232
+ end
166
233
  #puts mails.size
167
234
  if __FILE__ == $PROGRAM_NAME
168
235
  MAILBOX = ARGV[0] || "mbox"
169
- mx = Mbox.new "mbox"
236
+ mx = Mbox.new MAILBOX
170
237
  mx.formatted_each do |str|
171
238
  puts str
172
239
  end
173
240
  mails = mx.mails
241
+ BOLD = "\e[1m"
242
+ CLEAR = "\e[0m"
174
243
 
175
244
  # ask user for a number and print body for that
176
245
  while true