rbhex-core 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (108) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +20 -0
  3. data/CHANGELOG +2000 -0
  4. data/LICENSE +56 -0
  5. data/README.md +44 -0
  6. data/examples/abasiclist.rb +179 -0
  7. data/examples/alpmenu.rb +50 -0
  8. data/examples/app.sample +19 -0
  9. data/examples/atree.rb +100 -0
  10. data/examples/bline.rb +136 -0
  11. data/examples/common/file.rb +45 -0
  12. data/examples/data/README.markdown +9 -0
  13. data/examples/data/brew.txt +38 -0
  14. data/examples/data/color.2 +37 -0
  15. data/examples/data/gemlist.txt +60 -0
  16. data/examples/data/lotr.txt +12 -0
  17. data/examples/data/ports.txt +136 -0
  18. data/examples/data/table.txt +37 -0
  19. data/examples/data/tasks.csv +88 -0
  20. data/examples/data/tasks.txt +27 -0
  21. data/examples/data/todo.txt +10 -0
  22. data/examples/data/todo.txt.bak +10 -0
  23. data/examples/data/todocsv.csv +28 -0
  24. data/examples/data/unix1.txt +21 -0
  25. data/examples/data/unix2.txt +11 -0
  26. data/examples/dbdemo.rb +502 -0
  27. data/examples/dirtree.rb +94 -0
  28. data/examples/newtabbedwindow.rb +100 -0
  29. data/examples/newtesttabp.rb +92 -0
  30. data/examples/tabular.rb +146 -0
  31. data/examples/tasks.rb +178 -0
  32. data/examples/term2.rb +84 -0
  33. data/examples/testbuttons.rb +296 -0
  34. data/examples/testcombo.rb +102 -0
  35. data/examples/testfields.rb +195 -0
  36. data/examples/testkeypress.rb +72 -0
  37. data/examples/testlistbox.rb +170 -0
  38. data/examples/testmessagebox.rb +140 -0
  39. data/examples/testprogress.rb +116 -0
  40. data/examples/testree.rb +106 -0
  41. data/examples/testwsshortcuts.rb +66 -0
  42. data/examples/testwsshortcuts2.rb +128 -0
  43. data/lib/rbhex.rb +6 -0
  44. data/lib/rbhex/core/docs/index.txt +73 -0
  45. data/lib/rbhex/core/include/action.rb +80 -0
  46. data/lib/rbhex/core/include/actionmanager.rb +49 -0
  47. data/lib/rbhex/core/include/appmethods.rb +214 -0
  48. data/lib/rbhex/core/include/bordertitle.rb +48 -0
  49. data/lib/rbhex/core/include/chunk.rb +203 -0
  50. data/lib/rbhex/core/include/io.rb +553 -0
  51. data/lib/rbhex/core/include/listbindings.rb +74 -0
  52. data/lib/rbhex/core/include/listcellrenderer.rb +140 -0
  53. data/lib/rbhex/core/include/listeditable.rb +317 -0
  54. data/lib/rbhex/core/include/listscrollable.rb +663 -0
  55. data/lib/rbhex/core/include/listselectable.rb +271 -0
  56. data/lib/rbhex/core/include/multibuffer.rb +83 -0
  57. data/lib/rbhex/core/include/orderedhash.rb +77 -0
  58. data/lib/rbhex/core/include/ractionevent.rb +73 -0
  59. data/lib/rbhex/core/include/rchangeevent.rb +27 -0
  60. data/lib/rbhex/core/include/rhistory.rb +95 -0
  61. data/lib/rbhex/core/include/rinputdataevent.rb +47 -0
  62. data/lib/rbhex/core/include/vieditable.rb +172 -0
  63. data/lib/rbhex/core/include/widgetmenu.rb +66 -0
  64. data/lib/rbhex/core/system/colormap.rb +165 -0
  65. data/lib/rbhex/core/system/keyboard.rb +150 -0
  66. data/lib/rbhex/core/system/keydefs.rb +30 -0
  67. data/lib/rbhex/core/system/ncurses.rb +236 -0
  68. data/lib/rbhex/core/system/panel.rb +162 -0
  69. data/lib/rbhex/core/system/window.rb +913 -0
  70. data/lib/rbhex/core/util/ansiparser.rb +119 -0
  71. data/lib/rbhex/core/util/app.rb +1228 -0
  72. data/lib/rbhex/core/util/basestack.rb +410 -0
  73. data/lib/rbhex/core/util/bottomline.rb +1859 -0
  74. data/lib/rbhex/core/util/colorparser.rb +77 -0
  75. data/lib/rbhex/core/util/focusmanager.rb +31 -0
  76. data/lib/rbhex/core/util/padreader.rb +192 -0
  77. data/lib/rbhex/core/util/rcommandwindow.rb +604 -0
  78. data/lib/rbhex/core/util/rdialogs.rb +574 -0
  79. data/lib/rbhex/core/util/viewer.rb +149 -0
  80. data/lib/rbhex/core/util/widgetshortcuts.rb +506 -0
  81. data/lib/rbhex/core/version.rb +5 -0
  82. data/lib/rbhex/core/widgets/applicationheader.rb +103 -0
  83. data/lib/rbhex/core/widgets/box.rb +58 -0
  84. data/lib/rbhex/core/widgets/divider.rb +310 -0
  85. data/lib/rbhex/core/widgets/keylabelprinter.rb +194 -0
  86. data/lib/rbhex/core/widgets/rcombo.rb +253 -0
  87. data/lib/rbhex/core/widgets/rcontainer.rb +415 -0
  88. data/lib/rbhex/core/widgets/rlink.rb +30 -0
  89. data/lib/rbhex/core/widgets/rlist.rb +696 -0
  90. data/lib/rbhex/core/widgets/rmenu.rb +958 -0
  91. data/lib/rbhex/core/widgets/rmenulink.rb +22 -0
  92. data/lib/rbhex/core/widgets/rmessagebox.rb +387 -0
  93. data/lib/rbhex/core/widgets/rprogress.rb +118 -0
  94. data/lib/rbhex/core/widgets/rtabbedpane.rb +634 -0
  95. data/lib/rbhex/core/widgets/rtabbedwindow.rb +70 -0
  96. data/lib/rbhex/core/widgets/rtextarea.rb +960 -0
  97. data/lib/rbhex/core/widgets/rtextview.rb +739 -0
  98. data/lib/rbhex/core/widgets/rtree.rb +768 -0
  99. data/lib/rbhex/core/widgets/rwidget.rb +3277 -0
  100. data/lib/rbhex/core/widgets/scrollbar.rb +143 -0
  101. data/lib/rbhex/core/widgets/statusline.rb +113 -0
  102. data/lib/rbhex/core/widgets/tabular.rb +264 -0
  103. data/lib/rbhex/core/widgets/tabularwidget.rb +1142 -0
  104. data/lib/rbhex/core/widgets/textpad.rb +995 -0
  105. data/lib/rbhex/core/widgets/tree/treecellrenderer.rb +150 -0
  106. data/lib/rbhex/core/widgets/tree/treemodel.rb +428 -0
  107. data/rbhex-core.gemspec +32 -0
  108. metadata +172 -0
@@ -0,0 +1,94 @@
1
+ require 'rbhex/core/util/app'
2
+ require 'fileutils'
3
+ require 'rbhex/core/widgets/tree/treemodel'
4
+ begin
5
+ require './common/file' # Allow for Ruby < 1.9.2
6
+ rescue LoadError
7
+ require_relative 'common/file'
8
+ end
9
+
10
+ def _directories wd
11
+ $log.debug " directories got XXX: #{wd} "
12
+ d = Dir.new(wd)
13
+ ent = d.entries.reject{|e| !File.directory? File.join(wd,e)}
14
+ $log.debug " directories got XXX: #{ent} "
15
+ ent.delete(".");ent.delete("..")
16
+ return ent
17
+ end
18
+ App.new do
19
+ def help_text
20
+ ["Press <Enter> to expand/collapse directories. Press <space> to list a directory",
21
+ "Press <space> on filename to page it, or <ENTER> to view it in vi",
22
+ "At present the PWD may not be updated, so you'll just have to be in the correct",
23
+ "dir to actually view the file"]
24
+ end
25
+ header = app_header "rbhex #{Rbhex::Core::VERSION}", :text_center => "Yet Another Dir Lister", :text_right =>"Directory Lister" , :color => :white, :bgcolor => :black #, :attr => Ncurses::A_BLINK
26
+ message "Press Enter to expand/collapse, <space> to view in lister. <F1> Help"
27
+ @form.help_manager.help_text = help_text()
28
+
29
+ pwd = Dir.getwd
30
+ entries = _directories pwd
31
+ patharray = pwd.split("/")
32
+ # we have an array of path, to add recursively, one below the other
33
+ nodes = []
34
+ nodes << TreeNode.new(patharray.shift)
35
+ patharray.each do |e|
36
+ nodes << nodes.last.add(e)
37
+ end
38
+ last = nodes.last
39
+ nodes.last.add entries
40
+ model = DefaultTreeModel.new nodes.first
41
+
42
+
43
+
44
+ ht = FFI::NCurses.LINES - 2
45
+ borderattrib = :normal
46
+ flow :margin_top => 1, :margin_left => 0, :width => :expand, :height => ht do
47
+ @t = tree :data => model, :width_pc => 30, :border_attrib => borderattrib
48
+ @t.bind :TREE_WILL_EXPAND_EVENT do |node|
49
+ path = File.join(*node.user_object_path)
50
+ dirs = _directories path
51
+ ch = node.children
52
+ ch.each do |e|
53
+ o = e.user_object
54
+ if dirs.include? o
55
+ dirs.delete o
56
+ else
57
+ # delete this child since its no longer present TODO
58
+ end
59
+ end
60
+ #message " #{node} will expand: #{path}, #{dirs} "
61
+ node.add dirs
62
+ end
63
+ @t.bind :TREE_SELECTION_EVENT do |ev|
64
+ if ev.state == :SELECTED
65
+ node = ev.node
66
+ path = File.join(*node.user_object_path)
67
+ if File.exists? path
68
+ files = Dir.new(path).entries
69
+ files.delete(".")
70
+ @l.list files
71
+ #TODO show all details in filelist
72
+ @current_path = path
73
+ end
74
+ end
75
+ end # select
76
+ @t.expand_node last #
77
+ @t.mark_parents_expanded last # make parents visible
78
+ @l = listbox :width_pc => 70, :border_attrib => borderattrib
79
+
80
+ @l.bind :LIST_SELECTION_EVENT do |ev|
81
+ message ev.source.text #selected_value
82
+ _f = File.join(@current_path, ev.source.text)
83
+ file_page _f if ev.type == :INSERT
84
+ #TODO when selects drill down
85
+ #TODO when selecting, sync tree with this
86
+ end
87
+ # on pressing enter, we edit the file using vi or EDITOR
88
+ @l.bind :PRESS do |ev|
89
+ _f = File.join(@current_path, ev.source.text)
90
+ file_edit _f if File.exists? _f
91
+ end
92
+ end
93
+ status_line :row => FFI::NCurses.LINES - 1
94
+ 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 'rbhex'
5
+ require 'rbhex/core/widgets/rtabbedpane'
6
+ require 'rbhex/core/widgets/rcontainer'
7
+ require 'rbhex/core/widgets/rcombo'
8
+ require 'rbhex/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 => 20,
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 'rbhex'
5
+ #require 'rbhex/core/widgets/newtabbedpane'
6
+ require 'rbhex/core/widgets/rtabbedpane'
7
+ require 'rbhex/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,146 @@
1
+ require 'rbhex/core/util/app'
2
+
3
+ App.new do
4
+ # TODO: combine this with widget menu
5
+ def app_menu
6
+ menu = PromptMenu.new self do
7
+ item :e, :edit
8
+ item :o, :open_new
9
+ item :d, :delete
10
+ item :u, :undo_delete
11
+ #item :y, :yank
12
+ #item :p, :paste
13
+ item :/, :search
14
+ end
15
+ menu.display_new :title => "Menu"
16
+ end
17
+ # to execute when app_menu is invoked
18
+ def execute_this *cmd
19
+ cmd = cmd[0][0] # extract first letter of command
20
+ cmdi = cmd.getbyte(0)
21
+ case cmd
22
+ when 'e','o','p'
23
+ @window.ungetch cmdi
24
+ when 'y','d'
25
+ @window.ungetch cmdi
26
+ @window.ungetch cmdi
27
+ when 'u'
28
+ @window.ungetch cmd.upcase.getbyte(0)
29
+ when 's'
30
+ @window.ungetch ?\/.getbyte(0)
31
+ end
32
+ end
33
+ def help_text
34
+ <<-eos
35
+ Help for tabular widgets
36
+ ------------------------
37
+
38
+ Keys that can be used on header
39
+
40
+ <ENTER> - sort given field (press on header)
41
+ <-> - press <minus> to reduce column width
42
+ <+> - press <plus> to increase column width
43
+
44
+ Keys that can be used on data rows
45
+
46
+ <space> - select a row
47
+ <Ctr-space> - range select
48
+ <u> - unselect all (conflicts with vim keys!!)
49
+ <a> - select all
50
+ <*> - invert selection
51
+
52
+ </> - <slash> for searching,
53
+ <n> to continue searching
54
+
55
+ Keys specific to this example
56
+
57
+ <e> - edit current row
58
+ <dd> - delete current row or <num> rows
59
+ <o> - insert a row after current one
60
+ <U> - undo delete
61
+
62
+ Motion keys
63
+
64
+ Usual for lists and textview such as :
65
+ j, k, h, l
66
+ w and b for (next/prev) column
67
+ C-d and C-b
68
+ gg and G
69
+
70
+ eos
71
+ end
72
+ def edit_row tw
73
+ row = tw.current_value
74
+ h = tw.columns
75
+ _edit h, row, " Edit "
76
+ tw.repaint_required true
77
+ end
78
+ def insert_row tw
79
+ h = tw.columns
80
+ row = []
81
+ h.each { |e| row << "" }
82
+ ret = _edit h, row, "Insert"
83
+ if ret
84
+ tw.insert tw.real_index(), row
85
+ tw.repaint_required true
86
+ end
87
+ end
88
+
89
+ # making a generic edit messagebox - quick dirty
90
+ def _edit h, row, title
91
+ _l = longest_in_list h
92
+ _w = _l.size
93
+ config = { :width => 70, :title => title }
94
+ bw = get_color $datacolor, :black, :white
95
+ mb = MessageBox.new config do
96
+ h.each_with_index { |f, i|
97
+ add Field.new :label => "%*s:" % [_w, f], :text => row[i].chomp, :name => i.to_s,
98
+ :bgcolor => :cyan,
99
+ :display_length => 50,
100
+ :label_color_pair => bw
101
+ }
102
+ button_type :ok_cancel
103
+ end
104
+ index = mb.run
105
+ return nil if index != 0
106
+ h.each_with_index { |e, i|
107
+ f = mb.widget(i.to_s)
108
+ row[i] = f.text
109
+ }
110
+ row
111
+ end
112
+ def resize
113
+ tab = @form.by_name["tab"]
114
+ cols = Ncurses.COLS
115
+ rows = Ncurses.LINES
116
+ tab.width_pc ||= (1.0*tab.width / $orig_cols)
117
+ tab.height_pc ||= (1.0*tab.height / $orig_rows)
118
+ tab.height = (tab.height_pc * rows).floor
119
+ tab.width = (tab.width_pc * cols).floor
120
+ end
121
+ header = app_header "rbhex #{Rbhex::Core::VERSION}", :text_center => "Tabular Demo", :text_right =>"Fat-free !",
122
+ :color => :black, :bgcolor => :green #, :attr => :bold
123
+ message "Press F10 to exit, F1 for help, : for menu"
124
+ @form.help_manager.help_text = help_text()
125
+ $orig_cols = Ncurses.COLS
126
+ $orig_rows = Ncurses.LINES
127
+
128
+ h = %w[ Id Title Priority Status]
129
+ file = File.expand_path("../data/table.txt", __FILE__)
130
+ lines = File.open(file,'r').readlines
131
+ arr = []
132
+ lines.each { |l| arr << l.split("|") }
133
+ flow :margin_top => 1, :height => FFI::NCurses.LINES-2 do
134
+ tw = tabular_widget :print_footer => true, :name => "tab"
135
+ tw.columns = h
136
+ tw.column_align 0, :right
137
+ tw.set_content arr
138
+ tw.bind_key([?d,?d], 'delete row') { tw.delete_line }
139
+ tw.bind_key(?U, 'undo delete') { tw.undo_delete }
140
+ tw.bind_key(?e, 'edit row') { edit_row tw }
141
+ tw.bind_key(?o, 'insert row') { insert_row tw }
142
+ end # stack
143
+ status_line :row => FFI::NCurses.LINES-1
144
+ @form.bind_key(?:, 'menu') { app_menu }
145
+ @form.bind(:RESIZE) { resize }
146
+ end # app
data/examples/tasks.rb ADDED
@@ -0,0 +1,178 @@
1
+ require 'rbhex/core/util/app'
2
+ require 'rbhex/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
+ def resize
27
+ tab = @form.by_name["tasklist"]
28
+ cols = Ncurses.COLS
29
+ rows = Ncurses.LINES
30
+ tab.width_pc ||= (1.0*tab.width / $orig_cols)
31
+ tab.height_pc ||= (1.0*tab.height / $orig_rows)
32
+ tab.height = (tab.height_pc * rows).floor
33
+ tab.width = (tab.width_pc * cols).floor
34
+ #$log.debug "XXX: RESIZE h w #{tab.height} , #{tab.width} "
35
+ end
36
+ @default_prefix = " "
37
+ header = app_header "rbhex #{Rbhex::Core::VERSION}", :text_center => "Task List", :text_right =>"New Improved!"
38
+
39
+ message "Press F10 or qq to quit "
40
+
41
+ file = File.expand_path("../data/todo.txt", __FILE__)
42
+ alist = File.open(file,'r').readlines
43
+ #flow :margin_top => 1, :item_width => 50 , :height => FFI::NCurses.LINES-2 do
44
+ #stack :margin_top => 1, :width => :expand, :height => FFI::NCurses.LINES-4 do
45
+
46
+ #task = field :label => " Task:", :display_length => 50, :maxlen => 80, :bgcolor => :cyan, :color => :black
47
+ #pri = field :label => "Priority:", :display_length => 1, :maxlen => 1, :type => :integer,
48
+ #:valid_range => 1..9, :bgcolor => :cyan, :color => :black , :default => "5"
49
+ #pri.overwrite_mode = true
50
+ # u,se voerwrite mode for this TODO and catch exception
51
+
52
+ lb = listbox :list => alist.sort, :title => "[ todos ]", :name => "tasklist", :row => 1, :height => Ncurses.LINES-4, :width => Ncurses.COLS-1
53
+ lb.should_show_focus = false
54
+ lb.cell_renderer CellRenderer.new
55
+ lb.bind_key(?d, "Delete Row"){
56
+ if confirm("Delete #{lb.current_value} ?")
57
+ lb.delete_at lb.current_index
58
+ # TODO reposition cursor at 0. use list_data_changed ?
59
+ end
60
+ }
61
+ lb.bind_key(?e, "Edit Row"){
62
+ if ((value = get_string("Edit Task:", :width => 80, :default => lb.current_value, :maxlen => 80, :display_length => 70)) != nil)
63
+
64
+ lb[lb.current_index]=value
65
+ end
66
+ }
67
+ lb.bind_key(?a, "Add Record"){
68
+
69
+ # ADD
70
+ task = Field.new :label => " Task:", :display_length => 60, :maxlen => 80, :bgcolor => :cyan, :color => :black,
71
+ :name => 'task'
72
+ pri = Field.new :label => "Priority:", :display_length => 1, :maxlen => 1, :type => :integer,
73
+ :valid_range => 1..9, :bgcolor => :cyan, :color => :black , :default => "5", :name => 'pri'
74
+ pri.overwrite_mode = true
75
+ config = {}
76
+ config[:width] = 80
77
+ config[:title] = "New Task"
78
+ tp = MessageBox.new config do
79
+ item task
80
+ item pri
81
+ button_type :ok_cancel
82
+ default_button 0
83
+ end
84
+ index = tp.run
85
+ if index == 0 # OK
86
+ # when does this memory get released ??? XXX
87
+ _t = tp.form.by_name['pri'].text
88
+ if _t != ""
89
+ val = @default_prefix + tp.form.by_name['pri'].text + ". " + tp.form.by_name['task'].text
90
+ w = @form.by_name["tasklist"]
91
+ _l = w.list
92
+ _l << val
93
+ w.list(_l.sort)
94
+ end
95
+ else # CANCEL
96
+ #return nil
97
+ end
98
+ }
99
+ # decrease priority
100
+ lb.bind_key(?-, 'decrease priority'){
101
+ line = lb.current_value
102
+ p = line[1,1].to_i
103
+ if p < 9
104
+ p += 1
105
+ line[1,1] = p.to_s
106
+ lb[lb.current_index]=line
107
+ lb.list(lb.list.sort)
108
+ end
109
+ }
110
+ # increase priority
111
+ lb.bind_key(?+, 'increase priority'){
112
+ line = lb.current_value
113
+ p = line[1,1].to_i
114
+ if p > 1
115
+ p -= 1
116
+ line[1,1] = p.to_s
117
+ lb[lb.current_index]=line
118
+ lb.list(lb.list.sort)
119
+ # how to get the new row of that item and position it there. so one
120
+ # can do consecutive increases or decreases
121
+ # cursor on old row, but current has become zero. FIXME
122
+ # Maybe setform_row needs to be called
123
+ end
124
+ }
125
+ # mark as done
126
+ lb.bind_key(?x, 'mark done'){
127
+ line = lb.current_value
128
+ line[0,1] = "x"
129
+ lb[lb.current_index]=line
130
+ lb.list(lb.list.sort)
131
+ }
132
+ # flag task with a single character
133
+ lb.bind_key(?!, 'flag'){
134
+ line = lb.current_value.chomp
135
+ value = get_string("Flag for #{line}. Enter one character.", :maxlen => 1, :display_length => 1)
136
+ #if ((value = get_string("Edit Task:", :width => 80, :default => lb.current_value)) != nil)
137
+ #lb[lb.current_index]=value
138
+ #end
139
+ if value ##&& value[0,1] != " "
140
+ line[0,1] = value[0,1]
141
+ lb[lb.current_index]=line
142
+ lb.list(lb.list.sort)
143
+ end
144
+ }
145
+ #end # stack
146
+ s = status_line
147
+ @form.bind(:RESIZE) { resize }
148
+
149
+ keyarray = [
150
+ ["F1" , "Help"], ["F10" , "Exit"],
151
+ ["F2", "Menu"], ["F4", "View"],
152
+ ["d", "delete item"], ["e", "edit item"],
153
+ ["a", "add item"], ["x", "close item"],
154
+ ["+", "inc priority"], ["-", "dec priority"],
155
+
156
+ ["M-x", "Command"], nil
157
+ ]
158
+
159
+ gw = get_color($reversecolor, 'green', 'black')
160
+ @adock = dock keyarray, { :row => Ncurses.LINES-2, :footer_color_pair => $datacolor,
161
+ :footer_mnemonic_color_pair => gw }
162
+
163
+ @window.confirm_close_command do
164
+ confirm "Sure you wanna quit?", :default_button => 1
165
+ end
166
+ @window.close_command do
167
+ w = @form.by_name["tasklist"]
168
+ if confirm("Save tasks?", :default_button => 0)
169
+ system("cp #{file} #{file}.bak")
170
+ File.open(file, 'w') {|f|
171
+ w.list.each { |e|
172
+ f.puts(e)
173
+ }
174
+ }
175
+ end
176
+ end
177
+
178
+ end # app