rbhex-core 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,27 @@
1
+ =begin
2
+ * Name: ChangeEvent
3
+ * Description: Event used to notify interested parties that state of component has changed
4
+ * Author: rkumar (arunachalesha)
5
+
6
+ --------
7
+ * Date: 2010-02-26 11:32
8
+ * License:
9
+ Same as Ruby's License (http://www.ruby-lang.org/LICENSE.txt)
10
+
11
+ =end
12
+
13
+ # Event created when state changed (as in ViewPort)
14
+ module RubyCurses
15
+ class ChangeEvent
16
+ attr_accessor :source
17
+ def initialize source
18
+ @source = source
19
+ end
20
+ def to_s
21
+ inspect
22
+ end
23
+ def inspect
24
+ "ChangeEvent #{@source}"
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,95 @@
1
+ # ----------------------------------------------------------------------------- #
2
+ # File: rhistory.rb
3
+ # Description: a module that pops up history, and then updates selected value
4
+ # This goes with Field.
5
+ # e.g.,
6
+ # field.extend(FieldHistory)
7
+ #
8
+ # The module name History was throwing up errors
9
+ # Author: rkumar http://github.com/rkumar/rbcurse/
10
+ # Date: 2011-11-27 - 18:10
11
+ # License: Same as Ruby's License (http://www.ruby-lang.org/LICENSE.txt)
12
+ # Last update: 2011-11-27 - 20:11
13
+ # ----------------------------------------------------------------------------- #
14
+ #
15
+ # supply history for this object, at least give an empty array
16
+ # widget would typically be Field,
17
+ # otherwise it should implement *text()* for getting and setting value
18
+ # and a *CHANGED* event for when user has modified a value and moved out
19
+ # You can externally set $history_key to any unused key, otherwise it is M-h
20
+ module RubyCurses
21
+ extend self
22
+ module FieldHistory
23
+ def self.extended(obj)
24
+
25
+ obj.instance_exec {
26
+ @history ||= []
27
+ $history_key ||= ?\M-h
28
+ # ensure that the field is not overriding this in handle_key
29
+ bind_key($history_key) { _show_history }
30
+ # widget should have CHANGED event, or this will either give error, or just not work
31
+ # else please update history whenever you want a value to be retrieved
32
+ bind(:CHANGED) { @history << @text if @text && (!@history.include? @text) }
33
+ }
34
+ end
35
+
36
+ # pass the array of history values
37
+ # Trying out a change where an item can also be sent in.
38
+ # I am lost, i want the initialization to happen once.
39
+ def history arr
40
+ return @history unless arr
41
+ if arr.is_a? Array
42
+ @history = arr
43
+ else
44
+ @history << arr unless @history.include? arr
45
+ end
46
+ end
47
+ def history=(x); history(x); end
48
+
49
+ # pass in some configuration for histroy such as row and column to show popup on
50
+ def history_config config={}
51
+ @_history_config = config
52
+ end
53
+
54
+ # popup the hist
55
+ #
56
+ private
57
+ def _show_history
58
+ return unless @history
59
+ return if @history.empty?
60
+ list = @history
61
+ @_history_config ||= {}
62
+ #list = ["No history"] if @history.empty?
63
+ raise ArgumentError, "show_history got nil list" unless list
64
+ # calculate r and c
65
+ # col if fine, except for when there's a label.
66
+ wcol = 0 # taking care of when dialog uses history 2012-01-4
67
+ wcol = self.form.window.left if self.form
68
+ c = wcol + ( @field_col || @col) # this is also dependent on window coords, as in a status_window or messagebox
69
+ sz = @history.size
70
+ wrow = 0
71
+ wrow = self.form.window.top if self.form
72
+ crow = wrow + @row
73
+ # if list can be displayed above, then fit it just above
74
+ if crow > sz + 2
75
+ r = crow - sz - 2
76
+ else
77
+ # else fit it in next row
78
+ r = crow + 1
79
+ end
80
+ #r = @row - 10
81
+ #if @row < 10
82
+ #r = @row + 1
83
+ #end
84
+ r = @_history_config[:row] || r
85
+ c = @_history_config[:col] || c
86
+ ret = popuplist(list, :row => r, :col => c, :title => " History ")
87
+ if ret
88
+ self.text = list[ret]
89
+ self.set_form_col
90
+ end
91
+ @form.repaint if @form
92
+ @window.wrefresh if @window
93
+ end
94
+ end # mod History
95
+ end # mod RubyC
@@ -0,0 +1,47 @@
1
+ =begin
2
+ * Name: InputDataEvent
3
+ * Description: Event created when data modified in Field or TextEdit
4
+ * Author: rkumar (arunachalesha)
5
+
6
+ --------
7
+ * Date: 2008-12-24 17:27
8
+ * License:
9
+ Same as Ruby's License (http://www.ruby-lang.org/LICENSE.txt)
10
+
11
+ NOTE: this is how we used to write code in the Java days. Anyone reading this source,
12
+ this is NOT how to code in rubyland. Please see this link, for how to code such as class:
13
+ http://blog.grayproductions.net/articles/all_about_struct
14
+
15
+ =end
16
+
17
+ # Event created when data modified in Field or TextEdit
18
+ # 2008-12-24 17:54
19
+ module RubyCurses
20
+ class InputDataEvent
21
+ attr_accessor :index0, :index1, :source, :type, :row, :text
22
+ def initialize index0, index1, source, type, row, text
23
+ @index0 = index0
24
+ @index1 = index1
25
+ @source = source
26
+ @type = type
27
+ @row = row
28
+ @text = text
29
+ end
30
+ # until now to_s was returning inspect, but to make it easy for users let us return the value
31
+ # they most expect which is the text that was changed
32
+ def to_s
33
+ inspect
34
+ end
35
+ def inspect
36
+ ## now that textarea.to_s prints content we shouldn pass it here.
37
+ #"#{@type.to_s}, #{@source}, ind0:#{@index0}, ind1:#{@index1}, row:#{@row}, text:#{@text}"
38
+ "#{@type.to_s}, ind0:#{@index0}, ind1:#{@index1}, row:#{@row}, text:#{@text}"
39
+ end
40
+ # this is so that earlier applications were getting source in the block, not an event. they
41
+ # were doing a fld.getvalue, so we must keep those apps running
42
+ # @since 1.2.0 added 2010-09-11 12:25
43
+ def getvalue
44
+ @source.getvalue
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,172 @@
1
+ #**************************************************************
2
+ # Author: rkumar (arunachalesha)
3
+ # Date: 2010-03-11 22:18
4
+ # Provides the caller ability to do some edit operations
5
+ # on list widgets using either keys (vim largely)
6
+ # or a menu. made originally for textview and multitextview
7
+ #
8
+ #**************************************************************
9
+
10
+
11
+ require 'rbhex/core/include/listeditable'
12
+ module RubyCurses
13
+ module ViEditable
14
+ include ListEditable
15
+
16
+ def vieditable_init
17
+ $log.debug " inside vieditable_init "
18
+ @editable = true
19
+ bind_key( ?C, :edit_line)
20
+ #bind_key( ?o, :insert_line)
21
+ #bind_key( ?O) { insert_line(@current_index-1) }
22
+ bind_key( ?o) { insert_line(@current_index+1) }
23
+ bind_key( ?O) { insert_line(@current_index) }
24
+ bind_key( ?D, :delete_eol)
25
+ bind_key( [?d, ?$], :delete_eol)
26
+ bind_key( [?d, ?d] , :delete_line )
27
+ bind_key( [?d, ?w], :delete_word )
28
+ bind_key( [?d, ?t], :delete_till )
29
+ bind_key( [?d, ?f], :delete_forward )
30
+ bind_key( ?\C-_ ) { @undo_handler.undo if @undo_handler }
31
+ bind_key( ?u ) { @undo_handler.undo if @undo_handler }
32
+ bind_key( ?\C-r ) { @undo_handler.redo if @undo_handler }
33
+ bind_key( ?x, :delete_curr_char )
34
+ bind_key( ?X, :delete_prev_char )
35
+ bind_key( [?y, ?y] , :kill_ring_save )
36
+ bind_key( ?p, :yank ) # paste after this line
37
+ bind_key( ?P ) { yank(@current_index - 1) } # should be before this line
38
+ bind_key(?w, :forward_word)
39
+ bind_key(?f, :forward_char)
40
+ bind_key(?\M-y, :yank_pop)
41
+ bind_key(?\M-w, :kill_ring_save)
42
+ @_events.push :CHANGE # thru vieditable
43
+
44
+ end
45
+ ##
46
+ # Separate mappings for listboxes.
47
+ # Some methods don;'t make sense for listboxes and are crashing
48
+ # since not present for them. f was being overwritten, too.
49
+ # Sorry for duplication, need to clean this somehow.
50
+ def vieditable_init_listbox
51
+ $log.debug " inside vieditable_init_listbox "
52
+ @editable = true
53
+ bind_key( ?C, :edit_line)
54
+ bind_key( ?o) { insert_line(@current_index+1) }
55
+ bind_key( ?O) { insert_line(@current_index) }
56
+ bind_key( [?d, ?d] , :delete_line )
57
+ bind_key( ?\C-_ ) { @undo_handler.undo if @undo_handler }
58
+ bind_key( ?u ) { @undo_handler.undo if @undo_handler }
59
+ bind_key( ?\C-r ) { @undo_handler.redo if @undo_handler }
60
+ bind_key( [?y, ?y] , :kill_ring_save )
61
+ bind_key( ?p, :yank ) # paste after this line
62
+ #bind_key( ?P ) { yank(@current_index - 1) } # should be before this line
63
+ # seems -1 was pasting 2 lines before
64
+ bind_key( ?P ) { yank(@current_index - 0) } # should be before this line
65
+ bind_key(?w, :forward_word)
66
+ bind_key(?\M-y, :yank_pop)
67
+ bind_key(?\C-y, :yank)
68
+ bind_key(?\M-w, :kill_ring_save)
69
+ @_events.push :CHANGE # thru vieditable
70
+ #bind_key( ?D, :delete_eol)
71
+ #bind_key( [?d, ?$], :delete_eol)
72
+ #bind_key(?f, :forward_char)
73
+ #bind_key( ?x, :delete_curr_char )
74
+ #bind_key( ?X, :delete_prev_char )
75
+ #bind_key( [?d, ?w], :delete_word )
76
+ #bind_key( [?d, ?t], :delete_till )
77
+ #bind_key( [?d, ?f], :delete_forward )
78
+
79
+ end
80
+
81
+ # currently only adding delete_line and some yank pop functions
82
+ # These will all give wrong results in table due to _header_offset
83
+ def vieditable_init_tabular
84
+ $log.debug " inside vieditable_init tabular"
85
+ @editable = true
86
+ #bind_key( ?C, :edit_line)
87
+ #bind_key( ?o, :insert_line)
88
+ #bind_key( ?O) { insert_line(@current_index-1) }
89
+ #bind_key( ?o) { insert_line(@current_index+1) }
90
+ #bind_key( ?O) { insert_line(@current_index) }
91
+ bind_key( [?d, ?d] , :delete_line )
92
+ #bind_key( ?\C-_ ) { @undo_handler.undo if @undo_handler }
93
+ #bind_key( ?u ) { @undo_handler.undo if @undo_handler }
94
+ #bind_key( ?\C-r ) { @undo_handler.redo if @undo_handler }
95
+ bind_key( [?y, ?y] , :kill_ring_save )
96
+ bind_key( ?p, :yank ) # paste after this line
97
+ bind_key( ?P ) { yank(@current_index - 1) } # should be before this line
98
+ bind_key(?\M-y, :yank_pop)
99
+ bind_key(?\M-w, :kill_ring_save)
100
+ @_events.push :CHANGE # thru vieditable
101
+ end
102
+
103
+ ##
104
+ # edit current or given line
105
+ def edit_line lineno=@current_index
106
+ line = self[lineno]
107
+ prompt = "Edit: "
108
+ maxlen = 80
109
+ config={};
110
+ oldline = line.dup
111
+ config[:default] = line
112
+ ret, str = rbgetstr(@form.window, $error_message_row, $error_message_col, prompt, maxlen, config)
113
+ $log.debug " rbgetstr returned #{ret} , #{str} "
114
+ return if ret != 0
115
+ self[lineno].replace(str)
116
+ fire_handler :CHANGE, InputDataEvent.new(0,oldline.length, self, :DELETE_LINE, lineno, oldline) # 2008-12-24 18:34
117
+ fire_handler :CHANGE, InputDataEvent.new(0,str.length, self, :INSERT_LINE, lineno, str)
118
+ @repaint_required = true
119
+ @widget_scrolled = true
120
+ end
121
+ ##
122
+ # insert a line
123
+ # FIXME needs to fire handler 2010-05-23 11:40
124
+ def insert_line lineno=@current_index
125
+ prompt = "Insert: "
126
+ maxlen = 80
127
+ #config={};
128
+ #config[:default] = line
129
+ #ret, str = rbgetstr(@form.window, $error_message_row, $error_message_col, prompt, maxlen, config)
130
+ ret, str = input_string prompt
131
+ #ret, str = rbgetstr(@form.window, @row+@height-1, @col+1, prompt, maxlen, config)
132
+ $log.debug " rbgetstr returned #{ret} , #{str} "
133
+ return if ret != 0
134
+ @list.insert lineno, str
135
+ ## added handler on 2010-05-23 11:46 - undo works - tested in testlistbox.rb
136
+ fire_handler :CHANGE, InputDataEvent.new(0,str.length, self, :INSERT_LINE, lineno, str)
137
+ @widget_scrolled = true
138
+ @repaint_required = true
139
+ end
140
+ ##
141
+ # common method to edit given string
142
+ # @param [String] string to edit/modify
143
+ # @param [String] prompt to display before string
144
+ # @param [int] max length of input
145
+ # @return [0, -1] return value 0 if okay, -1 if error
146
+ #
147
+ def edit_string string, prompt="Edit: ", maxlen=80
148
+ config={};
149
+ config[:default] = string
150
+ ret, str = rbgetstr(@form.window, $error_message_row, $error_message_col, prompt, maxlen, config)
151
+ #return str if ret == 0
152
+ #return ""
153
+ end
154
+ ##
155
+ # common method to input a blank string
156
+ # @param [String] prompt to display before string
157
+ # @param [int] max length of input
158
+ # @return [0, -1] return value 0 if okay, -1 if error
159
+ def input_string prompt="Insert: ", maxlen=80
160
+ #ret, str = rbgetstr(@form.window, $error_message_row, $error_message_col, prompt, maxlen, config)
161
+ ret, str = rbgetstr(@form.window, $error_message_row, $error_message_col, prompt, maxlen, config)
162
+ #return str if ret == 0
163
+ #return ""
164
+ end
165
+ def edit_chars
166
+
167
+ end
168
+ def edit_word
169
+
170
+ end
171
+ end # module
172
+ end # module
@@ -0,0 +1,66 @@
1
+ # ----------------------------------------------------------------------------- #
2
+ # File: widgetmenu.rb
3
+ # Description: a module that displays a menu for customization of a field
4
+ # e.g.,
5
+ # field.extend(WidgetMenu)
6
+ #
7
+ # Author: rkumar http://github.com/rkumar/rbcurse/
8
+ # Date: 2011-12-2x
9
+ # License: Same as Ruby's License (http://www.ruby-lang.org/LICENSE.txt)
10
+ # Last update: 2011-12-26 - 20:25
11
+ # ----------------------------------------------------------------------------- #
12
+ #
13
+ # Provide a system for us to define a menu for customizing a widget, such that
14
+ # applicatin can also add more menuitems
15
+ module RubyCurses
16
+ extend self
17
+ module WidgetMenu
18
+ include Io # added 2011-12-26
19
+ # add a menu item which can any one of
20
+ # @param key, label, desc, action | symbol
21
+ # key, symbol
22
+ # Action
23
+ # Action[] (maybe)
24
+ def self.extended(obj)
25
+ # don't want this executed each time
26
+ @objects ||= []
27
+ return if @objects.include? obj
28
+ @objects << obj
29
+
30
+ obj.instance_exec {
31
+ @_menuitems ||= []
32
+ # callign this method means that no other programs can use those actions else
33
+ # that method will be called more than once, so it must either be called in the constructor
34
+ # or else have a check that it is only called once.
35
+ obj.init_menu if obj.respond_to? :init_menu
36
+ }
37
+
38
+ end
39
+ def add_menu_item *val
40
+ #@_menuitems ||= []
41
+ @_menuitems << val
42
+ end
43
+ #
44
+ # insert an item at given position (index)
45
+ def insert_menu_item pos, *val
46
+ #@_menuitems ||= []
47
+ @_menuitems[pos] = val
48
+ end
49
+ def create_menuitem *args
50
+ PromptMenu.create_menuitem *args
51
+ end
52
+
53
+ # popup the hist
54
+ #
55
+ def _show_menu
56
+ return if @_menuitems.nil? || @_menuitems.empty?
57
+ list = @_menuitems
58
+ menu = PromptMenu.new self do |m|
59
+ list.each { |e|
60
+ m.add *e
61
+ }
62
+ end
63
+ menu.display_new :title => 'Widget Menu (Press letter)'
64
+ end
65
+ end # mod History
66
+ end # mod RubyC
@@ -0,0 +1,165 @@
1
+ require 'rbhex/core/system/ncurses'
2
+
3
+ module RubyCurses
4
+ module ColorMap
5
+ # 2010-09-20 12:22 changed colors from string to symbol
6
+ ## private
7
+ # returns a color constant for a human color string
8
+ def ColorMap.get_color_const colorstring
9
+ # added check for fixnum if we go beyond these constants 2011-11-28
10
+ # e.g. to use full 256 colors
11
+ return colorstring if colorstring.is_a? Fixnum
12
+ ret = FFI::NCurses.const_get "COLOR_#{colorstring.to_s.upcase}"
13
+ end
14
+ ## private
15
+ # creates a new color pair, puts in color map and returns color_pair
16
+ # number
17
+ def ColorMap.install_color fgc, bgc
18
+ #$log.debug " install_color found #{fgc} #{@bgc} "
19
+ @color_id += 1
20
+ fg = ColorMap.get_color_const fgc
21
+ bg = ColorMap.get_color_const bgc
22
+ FFI::NCurses.init_pair(@color_id, fg, bg);
23
+ $color_map[[fgc, bgc]] = @color_id
24
+ return @color_id
25
+ end
26
+ #
27
+ # returns the colors that make up the given pair
28
+ # you may want to find what makes up $bottomcolor and set color and bgcolor with it.
29
+ # @param [Fixnum] color_pair
30
+ # @return [Symbol, Symbol] foreground and backgrounf color
31
+ # @example
32
+ # color, bgcolor = get_colors_for_pair $datacolor
33
+ #
34
+ def ColorMap.get_colors_for_pair pair
35
+ $color_map.invert[pair]
36
+ end
37
+ ## public
38
+ # returns a color_pair for a given foreground and background color
39
+ def ColorMap.get_color fgc, bgc=$def_bg_color
40
+ fgc = fgc.to_sym if fgc.is_a? String
41
+ bgc = bgc.to_sym if bgc.is_a? String
42
+ if $color_map.include? [fgc, bgc]
43
+ #$log.debug " get_color found #{fgc} #{@bgc} "
44
+ return $color_map[[fgc, bgc]]
45
+ else
46
+ #$log.debug " get_color NOT found #{fgc} #{@bgc} "
47
+ return ColorMap.install_color fgc, bgc
48
+ end
49
+ end
50
+ def ColorMap.colors
51
+ @@colors
52
+ end
53
+ # returns true if color is a valid one, else false
54
+ # @param [Symbol] color such as :black :cyan :yellow
55
+ # @return [Boolean] true if valid, else false
56
+ def ColorMap.is_color? color
57
+ return true if color.is_a? Fixnum # so we can use 256 colors
58
+ @@colors.include? color.to_sym
59
+ end
60
+
61
+ ## public
62
+ # setup color map at start of application
63
+ def ColorMap.setup
64
+ @color_id = 0
65
+ $color_map = {}
66
+ FFI::NCurses.start_color();
67
+ # Initialize few color pairs
68
+ $def_fg_color = :white # pls set these 2 for your application
69
+ $def_bg_color = :black
70
+ #COLORS = [COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_YELLOW, COLOR_BLUE,
71
+ # COLOR_MAGENTA, COLOR_CYAN, COLOR_WHITE]
72
+ @@colors = [:black, :red, :green, :yellow, :blue, :magenta, :cyan, :white]
73
+
74
+ # make foreground colors
75
+ bg = ColorMap.get_color_const $def_bg_color
76
+ @@colors[0...@@colors.size].each_with_index do |color, i|
77
+ next if color == $def_bg_color # NOTE hope this doesn't do something if you change def_bg
78
+ ColorMap.install_color color, $def_bg_color
79
+ end
80
+ $reversecolor = ColorMap.get_color $def_bg_color, $def_fg_color
81
+ $popupcolor = ColorMap.get_color :cyan, $def_fg_color
82
+
83
+ $errorcolor = ColorMap.get_color :white, :red
84
+ #$promptcolor = $selectedcolor = ColorMap.get_color(:yellow, :red)
85
+ $promptcolor = ColorMap.get_color(:yellow, :red)
86
+ $normalcolor = $datacolor = ColorMap.get_color(:white, :black)
87
+ $bottomcolor = $topcolor = ColorMap.get_color(:white, :blue)
88
+ $selectedcolor = $datacolor # since we now use reverse attr in list
89
+
90
+ $row_selected_attr = Ncurses::A_REVERSE
91
+ $row_focussed_attr = Ncurses::A_BOLD
92
+ $row_attr = Ncurses::A_NORMAL
93
+
94
+ # $log.debug " colormap SETUP: #{$datacolor} #{$reversecolor} "
95
+ end
96
+
97
+ end # modul
98
+ if $0 == __FILE__
99
+ require 'logger'
100
+ require 'ver/window'
101
+ #include Ncurses # FFI 2011-09-8
102
+ include ColorMap
103
+ # Initialize curses
104
+ begin
105
+ $log = Logger.new("rbc13.log")
106
+ VER::start_ncurses
107
+ @window = VER::Window.root_window
108
+ $log.level = Logger::DEBUG
109
+ ColorMap.setup
110
+
111
+ # Create the window to be associated with the form
112
+ # Un post form and free the memory
113
+
114
+ catch(:close) do
115
+ # $log.debug "START ---------"
116
+ # need to pass a form, not window.
117
+ r = 1; c = 2; i=0
118
+ attr = Ncurses::A_NORMAL
119
+ @window.printstring 20, c, "press 0-9 to change BG color, F1/q to quit. r-everse, n-ormal,b-old ", ColorMap.get_color('white')
120
+
121
+
122
+
123
+ while((ch = @window.getchar()) != FFI::NCurses::KEY_F1 )
124
+ next if ch == -1
125
+ break if ch == ?q.getbyte(0)
126
+ case ch
127
+ when ?r.getbyte(0)
128
+ attr |= Ncurses::A_REVERSE
129
+ when ?b.getbyte(0)
130
+ attr |= Ncurses::A_BOLD
131
+ when ?n.getbyte(0)
132
+ attr = Ncurses::A_NORMAL
133
+ when ?u.getbyte(0)
134
+ attr |= Ncurses::A_UNDERLINE
135
+ else
136
+ i = ch.chr.to_i
137
+ i = 1 if i > ColorMap::colors.length-1
138
+ end
139
+ bg = ColorMap::colors[i]
140
+ @@colors = %w[black red green yellow blue magenta cyan white]
141
+ @window.printstring r, c, "%-40s" % "red #{bg} ", ColorMap.get_color('red',bg) , attr
142
+ @window.printstring 2, c, "%-40s" % "blue #{bg} ", ColorMap.get_color('blue',bg) , attr
143
+ @window.printstring 3, c, "%-40s" % "white #{bg} ", ColorMap.get_color('white',bg) , attr
144
+ @window.printstring 4, c, "%-40s" % "green #{bg} ", ColorMap.get_color('green',bg) , attr
145
+ @window.printstring 5, c, "%-40s" % "cyan #{bg} ", ColorMap.get_color('cyan',bg) , attr
146
+ @window.printstring 6, c, "%-40s" % "magenta #{bg} ", ColorMap.get_color('magenta',bg) , attr
147
+ @window.printstring 7, c, "black #{bg} ", ColorMap.get_color('black',bg) , attr
148
+ @window.wrefresh
149
+ end
150
+ # VER::Keyboard.focus = tp
151
+ end
152
+ rescue => ex
153
+ ensure
154
+ # @panel = @window.panel if @window
155
+ # Ncurses::Panel.del_panel(@panel) if !@panel.nil?
156
+ # @window.delwin if !@window.nil?
157
+ @window.destroy unless @window.nil?
158
+ VER::stop_ncurses
159
+ p ex if ex
160
+ p(ex.backtrace.join("\n")) if ex
161
+ # $log.debug( ex) if ex
162
+ # $log.debug(ex.backtrace.join("\n")) if ex
163
+ end
164
+ end
165
+ end