rbcurse-core 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (94) hide show
  1. data/README.md +69 -0
  2. data/VERSION +1 -0
  3. data/examples/abasiclist.rb +151 -0
  4. data/examples/alpmenu.rb +46 -0
  5. data/examples/app.sample +17 -0
  6. data/examples/atree.rb +100 -0
  7. data/examples/common/file.rb +45 -0
  8. data/examples/data/README.markdown +9 -0
  9. data/examples/data/brew.txt +38 -0
  10. data/examples/data/color.2 +37 -0
  11. data/examples/data/gemlist.txt +60 -0
  12. data/examples/data/lotr.txt +12 -0
  13. data/examples/data/ports.txt +136 -0
  14. data/examples/data/table.txt +37 -0
  15. data/examples/data/tasks.csv +88 -0
  16. data/examples/data/tasks.txt +27 -0
  17. data/examples/data/todo.txt +10 -0
  18. data/examples/data/todocsv.csv +28 -0
  19. data/examples/data/unix1.txt +21 -0
  20. data/examples/data/unix2.txt +11 -0
  21. data/examples/dbdemo.rb +487 -0
  22. data/examples/dirtree.rb +90 -0
  23. data/examples/newtabbedwindow.rb +100 -0
  24. data/examples/newtesttabp.rb +92 -0
  25. data/examples/tabular.rb +132 -0
  26. data/examples/tasks.rb +167 -0
  27. data/examples/term2.rb +83 -0
  28. data/examples/testkeypress.rb +72 -0
  29. data/examples/testlistbox.rb +158 -0
  30. data/examples/testmessagebox.rb +140 -0
  31. data/examples/testree.rb +106 -0
  32. data/examples/testwsshortcuts.rb +66 -0
  33. data/examples/testwsshortcuts2.rb +127 -0
  34. data/lib/rbcurse.rb +8 -0
  35. data/lib/rbcurse/core/docs/index.txt +73 -0
  36. data/lib/rbcurse/core/include/action.rb +40 -0
  37. data/lib/rbcurse/core/include/appmethods.rb +112 -0
  38. data/lib/rbcurse/core/include/bordertitle.rb +41 -0
  39. data/lib/rbcurse/core/include/chunk.rb +182 -0
  40. data/lib/rbcurse/core/include/io.rb +953 -0
  41. data/lib/rbcurse/core/include/listcellrenderer.rb +140 -0
  42. data/lib/rbcurse/core/include/listeditable.rb +317 -0
  43. data/lib/rbcurse/core/include/listscrollable.rb +590 -0
  44. data/lib/rbcurse/core/include/listselectable.rb +264 -0
  45. data/lib/rbcurse/core/include/multibuffer.rb +83 -0
  46. data/lib/rbcurse/core/include/orderedhash.rb +77 -0
  47. data/lib/rbcurse/core/include/ractionevent.rb +67 -0
  48. data/lib/rbcurse/core/include/rchangeevent.rb +27 -0
  49. data/lib/rbcurse/core/include/rhistory.rb +62 -0
  50. data/lib/rbcurse/core/include/rinputdataevent.rb +47 -0
  51. data/lib/rbcurse/core/include/vieditable.rb +170 -0
  52. data/lib/rbcurse/core/system/colormap.rb +163 -0
  53. data/lib/rbcurse/core/system/keyboard.rb +150 -0
  54. data/lib/rbcurse/core/system/keydefs.rb +30 -0
  55. data/lib/rbcurse/core/system/ncurses.rb +218 -0
  56. data/lib/rbcurse/core/system/panel.rb +162 -0
  57. data/lib/rbcurse/core/system/window.rb +901 -0
  58. data/lib/rbcurse/core/util/ansiparser.rb +117 -0
  59. data/lib/rbcurse/core/util/app.rb +1235 -0
  60. data/lib/rbcurse/core/util/basestack.rb +407 -0
  61. data/lib/rbcurse/core/util/bottomline.rb +1850 -0
  62. data/lib/rbcurse/core/util/colorparser.rb +71 -0
  63. data/lib/rbcurse/core/util/focusmanager.rb +31 -0
  64. data/lib/rbcurse/core/util/padreader.rb +189 -0
  65. data/lib/rbcurse/core/util/rcommandwindow.rb +587 -0
  66. data/lib/rbcurse/core/util/rdialogs.rb +619 -0
  67. data/lib/rbcurse/core/util/viewer.rb +149 -0
  68. data/lib/rbcurse/core/util/widgetshortcuts.rb +505 -0
  69. data/lib/rbcurse/core/widgets/applicationheader.rb +102 -0
  70. data/lib/rbcurse/core/widgets/box.rb +58 -0
  71. data/lib/rbcurse/core/widgets/divider.rb +310 -0
  72. data/lib/rbcurse/core/widgets/keylabelprinter.rb +178 -0
  73. data/lib/rbcurse/core/widgets/rcombo.rb +238 -0
  74. data/lib/rbcurse/core/widgets/rcontainer.rb +415 -0
  75. data/lib/rbcurse/core/widgets/rlink.rb +30 -0
  76. data/lib/rbcurse/core/widgets/rlist.rb +723 -0
  77. data/lib/rbcurse/core/widgets/rmenu.rb +939 -0
  78. data/lib/rbcurse/core/widgets/rmenulink.rb +22 -0
  79. data/lib/rbcurse/core/widgets/rmessagebox.rb +373 -0
  80. data/lib/rbcurse/core/widgets/rprogress.rb +118 -0
  81. data/lib/rbcurse/core/widgets/rtabbedpane.rb +615 -0
  82. data/lib/rbcurse/core/widgets/rtabbedwindow.rb +68 -0
  83. data/lib/rbcurse/core/widgets/rtextarea.rb +920 -0
  84. data/lib/rbcurse/core/widgets/rtextview.rb +780 -0
  85. data/lib/rbcurse/core/widgets/rtree.rb +787 -0
  86. data/lib/rbcurse/core/widgets/rwidget.rb +3040 -0
  87. data/lib/rbcurse/core/widgets/scrollbar.rb +143 -0
  88. data/lib/rbcurse/core/widgets/statusline.rb +94 -0
  89. data/lib/rbcurse/core/widgets/tabular.rb +264 -0
  90. data/lib/rbcurse/core/widgets/tabularwidget.rb +1211 -0
  91. data/lib/rbcurse/core/widgets/textpad.rb +516 -0
  92. data/lib/rbcurse/core/widgets/tree/treecellrenderer.rb +150 -0
  93. data/lib/rbcurse/core/widgets/tree/treemodel.rb +428 -0
  94. metadata +156 -0
@@ -0,0 +1,90 @@
1
+ require 'rbcurse/core/util/app'
2
+ require 'fileutils'
3
+ require 'rbcurse/core/widgets/tree/treemodel'
4
+ #require 'rbcurse/common/file'
5
+ require './common/file'
6
+
7
+ def _directories wd
8
+ $log.debug " directories got XXX: #{wd} "
9
+ d = Dir.new(wd)
10
+ ent = d.entries.reject{|e| !File.directory? File.join(wd,e)}
11
+ $log.debug " directories got XXX: #{ent} "
12
+ ent.delete(".");ent.delete("..")
13
+ return ent
14
+ end
15
+ App.new do
16
+ def help_text
17
+ ["Press <Enter> to expand/collapse directories. Press <space> to list a directory",
18
+ "Press <space> on filename to page it, or <ENTER> to view it in vi",
19
+ "At present the PWD may not be updated, so you'll just have to be in the correct",
20
+ "dir to actually view the file"]
21
+ end
22
+ header = app_header "rbcurse #{Rbcurse::VERSION}", :text_center => "Yet Another Dir Lister", :text_right =>"Directory Lister" , :color => :white, :bgcolor => :black #, :attr => Ncurses::A_BLINK
23
+ message "Press Enter to expand/collapse, <space> to view in lister. <F1> Help"
24
+
25
+ pwd = Dir.getwd
26
+ entries = _directories pwd
27
+ patharray = pwd.split("/")
28
+ # we have an array of path, to add recursively, one below the other
29
+ nodes = []
30
+ nodes << TreeNode.new(patharray.shift)
31
+ patharray.each do |e|
32
+ nodes << nodes.last.add(e)
33
+ end
34
+ last = nodes.last
35
+ nodes.last.add entries
36
+ model = DefaultTreeModel.new nodes.first
37
+
38
+
39
+
40
+ ht = FFI::NCurses.LINES - 2
41
+ borderattrib = :normal
42
+ flow :margin_top => 1, :margin_left => 0, :width => :expand, :height => ht do
43
+ @t = tree :data => model, :width_pc => 30, :border_attrib => borderattrib
44
+ @t.bind :TREE_WILL_EXPAND_EVENT do |node|
45
+ path = File.join(*node.user_object_path)
46
+ dirs = _directories path
47
+ ch = node.children
48
+ ch.each do |e|
49
+ o = e.user_object
50
+ if dirs.include? o
51
+ dirs.delete o
52
+ else
53
+ # delete this child since its no longer present TODO
54
+ end
55
+ end
56
+ #message " #{node} will expand: #{path}, #{dirs} "
57
+ node.add dirs
58
+ end
59
+ @t.bind :TREE_SELECTION_EVENT do |ev|
60
+ if ev.state == :SELECTED
61
+ node = ev.node
62
+ path = File.join(*node.user_object_path)
63
+ if File.exists? path
64
+ files = Dir.new(path).entries
65
+ files.delete(".")
66
+ @l.list files
67
+ #TODO show all details in filelist
68
+ @current_path = path
69
+ end
70
+ end
71
+ end # select
72
+ @t.expand_node last #
73
+ @t.mark_parents_expanded last # make parents visible
74
+ @l = listbox :width_pc => 70, :border_attrib => borderattrib
75
+
76
+ @l.bind :LIST_SELECTION_EVENT do |ev|
77
+ message ev.source.text #selected_value
78
+ _f = File.join(@current_path, ev.source.text)
79
+ file_page _f if ev.type == :INSERT
80
+ #TODO when selects drill down
81
+ #TODO when selecting, sync tree with this
82
+ end
83
+ # on pressing enter, we edit the file using vi or EDITOR
84
+ @l.bind :PRESS do |ev|
85
+ _f = File.join(@current_path, ev.source.text)
86
+ file_edit _f if File.exists? _f
87
+ end
88
+ end
89
+ status_line :row => FFI::NCurses.LINES - 1
90
+ end # app
@@ -0,0 +1,100 @@
1
+ # this is a test program, tests out tabbed panes. type F1 to exit
2
+ #
3
+ require 'logger'
4
+ require 'rbcurse'
5
+ require 'rbcurse/core/widgets/rtabbedpane'
6
+ require 'rbcurse/core/widgets/rcontainer'
7
+ require 'rbcurse/core/widgets/rcombo'
8
+ require 'rbcurse/core/widgets/rtabbedwindow'
9
+
10
+ include RubyCurses
11
+ class SetupTabbedPane
12
+ def run
13
+ $config_hash ||= Variable.new Hash.new
14
+ #configvar.update_command(){ |v| $config_hash[v.source()] = v.value }
15
+
16
+ r = Container.new nil, :suppress_borders => true
17
+ l1 = Label.new nil, :name => "profile", :attr => 'bold', :text => "Profile"
18
+ f1 = Field.new nil, :name => "name", :maxlen => 20, :display_length => 20, :bgcolor => :white,
19
+ :color => :black, :text => "abc", :label => ' Name: '
20
+ f2 = Field.new nil, :name => "email", :display_length => 20, :bgcolor => :white,
21
+ :color => :blue, :text => "me@google.com", :label => 'Email: '
22
+ f3 = RadioButton.new nil, :variable => $config_hash, :text => "red", :value => "RED", :color => :red
23
+ f4 = RadioButton.new nil, :variable => $config_hash, :text => "blue", :value => "BLUE", :color => :blue
24
+ f5 = RadioButton.new nil, :variable => $config_hash, :text => "green", :value => "GREEN", :color => :green
25
+ r.add(l1,f1)
26
+ r.add(f2)
27
+ r.add(f3,f4,f5)
28
+
29
+ tp = TabbedWindow.new :row => 3, :col => 7, :width => 60, :height => 20 do
30
+ title "User Setup"
31
+ button_type :ok_apply_cancel
32
+ tab "&Profile" do
33
+ item Field.new nil, :row => 2, :col => 2, :text => "enter your name", :label => ' Name: '
34
+ item Field.new nil, :row => 3, :col => 2, :text => "enter your email", :label => 'Email: '
35
+ end
36
+ tab "&Settings" do
37
+ item Label.new nil, :text => "Text", :row => 1, :col => 2, :attr => 'bold'
38
+ item CheckBox.new nil, :row => 2, :col => 2, :text => "Antialias text"
39
+ item CheckBox.new nil, :row => 3, :col => 2, :text => "Use bold fonts"
40
+ item CheckBox.new nil, :row => 4, :col => 2, :text => "Allow blinking text"
41
+ item CheckBox.new nil, :row => 5, :col => 2, :text => "Display ANSI Colors"
42
+ item Label.new nil, :text => "Cursor", :row => 7, :col => 2, :attr => 'bold'
43
+ $config_hash.set_value Variable.new, :cursor
44
+ item RadioButton.new nil, :row => 8, :col => 2, :text => "Block", :value => "block", :variable => $config_hash[:cursor]
45
+ item RadioButton.new nil, :row => 9, :col => 2, :text => "Blink", :value => "blink", :variable => $config_hash[:cursor]
46
+ item RadioButton.new nil, :row => 10, :col => 2, :text => "Underline", :value => "underline", :variable => $config_hash[:cursor]
47
+ end
48
+ tab "&Term" do
49
+
50
+ item Label.new nil, :text => "Arrow Key in Combos", :row => 2, :col => 2, :attr => 'bold'
51
+ x = Variable.new
52
+ $config_hash.set_value x, :term
53
+ item RadioButton.new nil, :row => 3, :col => 2, :text => "ignore", :value => "ignore", :variable => $config_hash[:term]
54
+ item RadioButton.new nil, :row => 4, :col => 2, :text => "popup", :value => "popup", :variable => $config_hash[:term]
55
+ item RadioButton.new nil, :row => 5, :col => 2, :text => "next", :value => "next", :variable => $config_hash[:term]
56
+ cb = ComboBox.new nil, :row => 7, :col => 2, :display_length => 15,
57
+ :list => %w[xterm xterm-color xterm-256color screen vt100 vt102],
58
+ :label => "Declare terminal as: "
59
+ #radio.update_command() {|rb| ENV['TERM']=rb.value }
60
+ item cb
61
+ x.update_command do |rb|
62
+ cb.arrow_key_policy=rb.value.to_sym
63
+ end
64
+
65
+ end
66
+ tab "Conta&iner" do
67
+ item r
68
+ end
69
+ # tell tabbedpane what to do if a button is pressed (ok/apply/cancel)
70
+ command do |eve|
71
+ alert "user pressed button index:#{eve.event} , Name: #{eve.action_command}, Tab: #{eve.source.current_tab} "
72
+ case eve.event
73
+ when 0,2 # ok cancel
74
+ throw :close, eve.event
75
+ when 1 # apply
76
+ end
77
+ end
78
+ end
79
+ tp.run
80
+ end
81
+
82
+ end
83
+ if $0 == __FILE__
84
+ # Initialize curses
85
+ begin
86
+ # XXX update with new color and kb
87
+ VER::start_ncurses # this is initializing colors via ColorMap.setup
88
+ $log = Logger.new((File.join(ENV["LOGDIR"] || "./" ,"rbc13.log")))
89
+ $log.level = Logger::DEBUG
90
+ tp = SetupTabbedPane.new()
91
+ buttonindex = tp.run
92
+ rescue => ex
93
+ ensure
94
+ VER::stop_ncurses
95
+ p ex if ex
96
+ p(ex.backtrace.join("\n")) if ex
97
+ $log.debug( ex) if ex
98
+ $log.debug(ex.backtrace.join("\n")) if ex
99
+ end
100
+ end
@@ -0,0 +1,92 @@
1
+ # this is a test program, tests out tabbed panes. type F1 to exit
2
+ #
3
+ require 'logger'
4
+ require 'rbcurse'
5
+ #require 'rbcurse/core/widgets/newtabbedpane'
6
+ require 'rbcurse/core/widgets/rtabbedpane'
7
+ require 'rbcurse/core/widgets/rcontainer' # tempo FIXME remove this since we arent using afterfixing rtabbedp
8
+
9
+ class TestTabbedPane
10
+ def initialize
11
+ acolor = $reversecolor
12
+ #$config_hash ||= {}
13
+ end
14
+ def run
15
+ $config_hash ||= Variable.new Hash.new
16
+ #configvar.update_command(){ |v| $config_hash[v.source()] = v.value }
17
+ @window = VER::Window.root_window
18
+ @form = Form.new @window
19
+ r = 1; c = 30;
20
+ tp = RubyCurses::TabbedPane.new @form, :height => 12, :width => 50,
21
+ :row => 13, :col => 10 do
22
+ button_type :ok
23
+ end
24
+ tp.add_tab "&Language" do
25
+ _r = 2
26
+ colors = [:red, :green, :cyan]
27
+ %w[ ruby jruby macruby].each_with_index { |e, i|
28
+ item RadioButton.new nil,
29
+ :variable => $config_hash,
30
+ :name => "radio1",
31
+ :text => e,
32
+ :value => e,
33
+ :color => colors[i],
34
+ :row => _r+i,
35
+ :col => 5
36
+ }
37
+ end
38
+ tp.add_tab "&Settings" do
39
+ r = 2
40
+ butts = [ "Use &HTTP/1.0", "Use &frames", "&Use SSL" ]
41
+ bcodes = %w[ HTTP, FRAMES, SSL ]
42
+ butts.each_with_index do |t, i|
43
+ item RubyCurses::CheckBox.new nil,
44
+ :text => butts[i],
45
+ :variable => $config_hash,
46
+ :name => bcodes[i],
47
+ :row => r+i,
48
+ :col => 5
49
+ end
50
+ end
51
+ tp.add_tab "&Editors" do
52
+ butts = %w[ &Vim E&macs &Jed &Other ]
53
+ bcodes = %w[ VIM EMACS JED OTHER]
54
+ row = 2
55
+ butts.each_with_index do |name, i|
56
+ item RubyCurses::CheckBox.new nil ,
57
+ :text => name,
58
+ :variable => $config_hash,
59
+ :name => bcodes[i],
60
+ :row => row+i,
61
+ :col => 5
62
+ end
63
+ end
64
+ help = "q to quit. <TAB> through tabs, Space or Enter to select Tab."
65
+ RubyCurses::Label.new @form, {:text => help, :row => 1, :col => 2, :color => :yellow}
66
+ @form.repaint
67
+ @window.wrefresh
68
+ Ncurses::Panel.update_panels
69
+ while((ch = @window.getchar()) != ?q.getbyte(0) )
70
+ @form.handle_key(ch)
71
+ @window.wrefresh
72
+ end
73
+ end
74
+ end
75
+ if $0 == __FILE__
76
+ # Initialize curses
77
+ begin
78
+ # XXX update with new color and kb
79
+ VER::start_ncurses # this is initializing colors via ColorMap.setup
80
+ $log = Logger.new("rbc13.log")
81
+ $log.level = Logger::DEBUG
82
+ n = TestTabbedPane.new
83
+ n.run
84
+ rescue => ex
85
+ ensure
86
+ VER::stop_ncurses
87
+ p ex if ex
88
+ p(ex.backtrace.join("\n")) if ex
89
+ $log.debug( ex) if ex
90
+ $log.debug(ex.backtrace.join("\n")) if ex
91
+ end
92
+ end
@@ -0,0 +1,132 @@
1
+ require 'rbcurse/core/util/app'
2
+
3
+ App.new do
4
+ def app_menu
5
+ menu = PromptMenu.new self do
6
+ item :e, :edit
7
+ item :o, :open_new
8
+ item :d, :delete
9
+ item :u, :undo_delete
10
+ #item :y, :yank
11
+ #item :p, :paste
12
+ item :/, :search
13
+ end
14
+ menu.display_new :title => "Menu"
15
+ end
16
+ # to execute when app_menu is invoked
17
+ def execute_this *cmd
18
+ cmd = cmd[0][0] # extract first letter of command
19
+ cmdi = cmd.getbyte(0)
20
+ case cmd
21
+ when 'e','o','p'
22
+ @window.ungetch cmdi
23
+ when 'y','d'
24
+ @window.ungetch cmdi
25
+ @window.ungetch cmdi
26
+ when 'u'
27
+ @window.ungetch cmd.upcase.getbyte(0)
28
+ when 's'
29
+ @window.ungetch ?\/.getbyte(0)
30
+ end
31
+ end
32
+ def help_text
33
+ <<-eos
34
+ Help for tabular widgets
35
+ ------------------------
36
+
37
+ Keys that can be used on header
38
+
39
+ <ENTER> - sort given field (press on header)
40
+ <-> - press <minus> to reduce column width
41
+ <+> - press <plus> to increase column width
42
+
43
+ Keys that can be used on data rows
44
+
45
+ <space> - select a row
46
+ <Ctr-space> - range select
47
+ <u> - unselect all (conflicts with vim keys!!)
48
+ <a> - select all
49
+ <*> - invert selection
50
+
51
+ </> - <slash> for searching,
52
+ <n> to continue searching
53
+
54
+ Keys specific to this example
55
+
56
+ <e> - edit current row
57
+ <dd> - delete current row or <num> rows
58
+ <o> - insert a row after current one
59
+ <U> - undo delete
60
+
61
+ Motion keys
62
+
63
+ Usual for lists and textview such as :
64
+ j, k, h, l
65
+ w and b for (next/prev) column
66
+ C-d and C-b
67
+ gg and G
68
+
69
+ eos
70
+ end
71
+ def edit_row tw
72
+ row = tw.current_value
73
+ h = tw.columns
74
+ _edit h, row, " Edit "
75
+ tw.repaint_required true
76
+ end
77
+ def insert_row tw
78
+ h = tw.columns
79
+ row = []
80
+ h.each { |e| row << "" }
81
+ ret = _edit h, row, "Insert"
82
+ if ret
83
+ tw.insert tw.real_index(), row
84
+ tw.repaint_required true
85
+ end
86
+ end
87
+
88
+ # making a generic edit messagebox - quick dirty
89
+ def _edit h, row, title
90
+ _l = longest_in_list h
91
+ _w = _l.size
92
+ config = { :width => 70, :title => title }
93
+ bw = get_color $datacolor, :black, :white
94
+ mb = MessageBox.new config do
95
+ h.each_with_index { |f, i|
96
+ add Field.new :label => "%*s:" % [_w, f], :text => row[i].chomp, :name => i.to_s,
97
+ :bgcolor => :cyan,
98
+ :display_length => 50,
99
+ :label_color_pair => bw
100
+ }
101
+ button_type :ok_cancel
102
+ end
103
+ index = mb.run
104
+ return nil if index != 0
105
+ h.each_with_index { |e, i|
106
+ f = mb.widget(i.to_s)
107
+ row[i] = f.text
108
+ }
109
+ row
110
+ end
111
+ header = app_header "rbcurse #{Rbcurse::VERSION}", :text_center => "Tabular Demo", :text_right =>"Fat-free !",
112
+ :color => :black, :bgcolor => :green #, :attr => :bold
113
+ message "Press F10 to exit, F1 for help, : for menu"
114
+
115
+ h = %w[ Id Title Priority Status]
116
+ file = "data/table.txt"
117
+ lines = File.open(file,'r').readlines
118
+ arr = []
119
+ lines.each { |l| arr << l.split("|") }
120
+ flow :margin_top => 1, :height => FFI::NCurses.LINES-3 do
121
+ tw = tabular_widget :print_footer => true
122
+ tw.columns = h
123
+ tw.column_align 0, :right
124
+ tw.set_content arr
125
+ tw.bind_key([?d,?d], 'delete row') { tw.delete_line }
126
+ tw.bind_key(?U, 'undo delete') { tw.undo_delete }
127
+ tw.bind_key(?e, 'edit row') { edit_row tw }
128
+ tw.bind_key(?o, 'insert row') { insert_row tw }
129
+ end # stack
130
+ status_line :row => FFI::NCurses.LINES-1
131
+ @form.bind_key(?:, 'menu') { app_menu }
132
+ end # app
data/examples/tasks.rb ADDED
@@ -0,0 +1,167 @@
1
+ require 'rbcurse/core/util/app'
2
+ require 'rbcurse/core/widgets/rlist'
3
+
4
+ # a simple example of how to get colored row rendering.
5
+ # also see rfe.rb for a more complete example
6
+ class CellRenderer
7
+ attr_accessor :display_length
8
+ def repaint g, r, c, crow, content, focus_type, selected
9
+ color = $datacolor
10
+ att = NORMAL
11
+ att = REVERSE if selected
12
+ color = get_color($datacolor, :yellow, :red) if content =~ /^.1/
13
+ color = get_color($datacolor, :red, :blue) if content =~ /^.2/
14
+ color = get_color($datacolor, :white, :black) if content =~ /^.3/
15
+ color = get_color($datacolor, :green, :black) if content =~ /^.4/
16
+ color = get_color($datacolor, :red, :black) if content =~ /^.5/
17
+ color = get_color($datacolor, :cyan, :black) if content =~ /^.6/
18
+ color = get_color($datacolor, :magenta, :black) if content =~ /^.[7-9]/
19
+ color = get_color($datacolor, :blue, :black) if content =~ /^x/
20
+ g.wattron(Ncurses.COLOR_PAIR(color) | att)
21
+ g.mvwprintw(r, c, "%s", :string, content);
22
+ g.wattroff(Ncurses.COLOR_PAIR(color) | att)
23
+ end
24
+ end
25
+ App.new do
26
+ @default_prefix = " "
27
+ header = app_header "rbcurse #{Rbcurse::VERSION}", :text_center => "Task List", :text_right =>"New Improved!"
28
+
29
+ message "Press F10 or qq to escape from here"
30
+
31
+ file = "data/todo.txt"
32
+ alist = File.open(file,'r').readlines if File.exists? file
33
+ #flow :margin_top => 1, :item_width => 50 , :height => FFI::NCurses.LINES-2 do
34
+ stack :margin_top => 1, :width => :expand, :height => FFI::NCurses.LINES-4 do
35
+
36
+ #task = field :label => " Task:", :display_length => 50, :maxlen => 80, :bgcolor => :cyan, :color => :black
37
+ #pri = field :label => "Priority:", :display_length => 1, :maxlen => 1, :type => :integer,
38
+ #:valid_range => 1..9, :bgcolor => :cyan, :color => :black , :default => "5"
39
+ #pri.overwrite_mode = true
40
+ # u,se voerwrite mode for this TODO and catch exception
41
+
42
+ lb = listbox :list => alist.sort, :title => "[ todos ]", :height_pc => 100, :name => "tasklist"
43
+ lb.should_show_focus = false
44
+ lb.cell_renderer CellRenderer.new
45
+ lb.bind_key(?d, "Delete Row"){
46
+ if confirm("Delete #{lb.current_value} ?")
47
+ lb.delete_at lb.current_index
48
+ # TODO reposition cursor at 0. use list_data_changed ?
49
+ end
50
+ }
51
+ lb.bind_key(?e, "Edit Row"){
52
+ if ((value = get_string("Edit Task:", :width => 80, :default => lb.current_value, :maxlen => 80, :display_length => 70)) != nil)
53
+
54
+ lb[lb.current_index]=value
55
+ end
56
+ }
57
+ lb.bind_key(?a, "Add Record"){
58
+
59
+ # ADD
60
+ task = Field.new :label => " Task:", :display_length => 60, :maxlen => 80, :bgcolor => :cyan, :color => :black,
61
+ :name => 'task'
62
+ pri = Field.new :label => "Priority:", :display_length => 1, :maxlen => 1, :type => :integer,
63
+ :valid_range => 1..9, :bgcolor => :cyan, :color => :black , :default => "5", :name => 'pri'
64
+ pri.overwrite_mode = true
65
+ config = {}
66
+ config[:width] = 80
67
+ config[:title] = "New Task"
68
+ tp = MessageBox.new config do
69
+ item task
70
+ item pri
71
+ button_type :ok_cancel
72
+ default_button 0
73
+ end
74
+ index = tp.run
75
+ if index == 0 # OK
76
+ # when does this memory get released ??? XXX
77
+ _t = tp.form.by_name['pri'].text
78
+ if _t != ""
79
+ val = @default_prefix + tp.form.by_name['pri'].text + ". " + tp.form.by_name['task'].text
80
+ w = @form.by_name["tasklist"]
81
+ _l = w.list
82
+ _l << val
83
+ w.list(_l.sort)
84
+ end
85
+ else # CANCEL
86
+ #return nil
87
+ end
88
+ }
89
+ # decrease priority
90
+ lb.bind_key(?-, 'decrease priority'){
91
+ line = lb.current_value
92
+ p = line[1,1].to_i
93
+ if p < 9
94
+ p += 1
95
+ line[1,1] = p.to_s
96
+ lb[lb.current_index]=line
97
+ lb.list(lb.list.sort)
98
+ end
99
+ }
100
+ # increase priority
101
+ lb.bind_key(?+, 'increase priority'){
102
+ line = lb.current_value
103
+ p = line[1,1].to_i
104
+ if p > 1
105
+ p -= 1
106
+ line[1,1] = p.to_s
107
+ lb[lb.current_index]=line
108
+ lb.list(lb.list.sort)
109
+ # how to get the new row of that item and position it there. so one
110
+ # can do consecutive increases or decreases
111
+ # cursor on old row, but current has become zero. FIXME
112
+ # Maybe setform_row needs to be called
113
+ end
114
+ }
115
+ # mark as done
116
+ lb.bind_key(?x, 'mark done'){
117
+ line = lb.current_value
118
+ line[0,1] = "x"
119
+ lb[lb.current_index]=line
120
+ lb.list(lb.list.sort)
121
+ }
122
+ # flag task with a single character
123
+ lb.bind_key(?!, 'flag'){
124
+ line = lb.current_value.chomp
125
+ value = get_string("Flag for #{line}. Enter one character.", :maxlen => 1, :display_length => 1)
126
+ #if ((value = get_string("Edit Task:", :width => 80, :default => lb.current_value)) != nil)
127
+ #lb[lb.current_index]=value
128
+ #end
129
+ if value ##&& value[0,1] != " "
130
+ line[0,1] = value[0,1]
131
+ lb[lb.current_index]=line
132
+ lb.list(lb.list.sort)
133
+ end
134
+ }
135
+ end # stack
136
+ s = status_line
137
+
138
+ keyarray = [
139
+ ["F1" , "Help"], ["F10" , "Exit"],
140
+ ["F2", "Menu"], ["F4", "View"],
141
+ ["d", "delete item"], ["e", "edit item"],
142
+ ["a", "add item"], ["x", "close item"],
143
+ ["+", "inc priority"], ["-", "dec priority"],
144
+
145
+ ["M-x", "Command"], nil
146
+ ]
147
+
148
+ gw = get_color($reversecolor, 'green', 'black')
149
+ @adock = dock keyarray, { :row => Ncurses.LINES-2, :footer_color_pair => $datacolor,
150
+ :footer_mnemonic_color_pair => gw }
151
+
152
+ @window.confirm_close_command do
153
+ confirm "Sure you wanna quit?", :default_button => 1
154
+ end
155
+ @window.close_command do
156
+ w = @form.by_name["tasklist"]
157
+ if confirm("Save tasks?", :default_button => 0)
158
+ system("cp #{file} #{file}.bak")
159
+ File.open(file, 'w') {|f|
160
+ w.list.each { |e|
161
+ f.puts(e)
162
+ }
163
+ }
164
+ end
165
+ end
166
+
167
+ end # app