rbhex-core 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +20 -0
- data/CHANGELOG +2000 -0
- data/LICENSE +56 -0
- data/README.md +44 -0
- data/examples/abasiclist.rb +179 -0
- data/examples/alpmenu.rb +50 -0
- data/examples/app.sample +19 -0
- data/examples/atree.rb +100 -0
- data/examples/bline.rb +136 -0
- data/examples/common/file.rb +45 -0
- data/examples/data/README.markdown +9 -0
- data/examples/data/brew.txt +38 -0
- data/examples/data/color.2 +37 -0
- data/examples/data/gemlist.txt +60 -0
- data/examples/data/lotr.txt +12 -0
- data/examples/data/ports.txt +136 -0
- data/examples/data/table.txt +37 -0
- data/examples/data/tasks.csv +88 -0
- data/examples/data/tasks.txt +27 -0
- data/examples/data/todo.txt +10 -0
- data/examples/data/todo.txt.bak +10 -0
- data/examples/data/todocsv.csv +28 -0
- data/examples/data/unix1.txt +21 -0
- data/examples/data/unix2.txt +11 -0
- data/examples/dbdemo.rb +502 -0
- data/examples/dirtree.rb +94 -0
- data/examples/newtabbedwindow.rb +100 -0
- data/examples/newtesttabp.rb +92 -0
- data/examples/tabular.rb +146 -0
- data/examples/tasks.rb +178 -0
- data/examples/term2.rb +84 -0
- data/examples/testbuttons.rb +296 -0
- data/examples/testcombo.rb +102 -0
- data/examples/testfields.rb +195 -0
- data/examples/testkeypress.rb +72 -0
- data/examples/testlistbox.rb +170 -0
- data/examples/testmessagebox.rb +140 -0
- data/examples/testprogress.rb +116 -0
- data/examples/testree.rb +106 -0
- data/examples/testwsshortcuts.rb +66 -0
- data/examples/testwsshortcuts2.rb +128 -0
- data/lib/rbhex.rb +6 -0
- data/lib/rbhex/core/docs/index.txt +73 -0
- data/lib/rbhex/core/include/action.rb +80 -0
- data/lib/rbhex/core/include/actionmanager.rb +49 -0
- data/lib/rbhex/core/include/appmethods.rb +214 -0
- data/lib/rbhex/core/include/bordertitle.rb +48 -0
- data/lib/rbhex/core/include/chunk.rb +203 -0
- data/lib/rbhex/core/include/io.rb +553 -0
- data/lib/rbhex/core/include/listbindings.rb +74 -0
- data/lib/rbhex/core/include/listcellrenderer.rb +140 -0
- data/lib/rbhex/core/include/listeditable.rb +317 -0
- data/lib/rbhex/core/include/listscrollable.rb +663 -0
- data/lib/rbhex/core/include/listselectable.rb +271 -0
- data/lib/rbhex/core/include/multibuffer.rb +83 -0
- data/lib/rbhex/core/include/orderedhash.rb +77 -0
- data/lib/rbhex/core/include/ractionevent.rb +73 -0
- data/lib/rbhex/core/include/rchangeevent.rb +27 -0
- data/lib/rbhex/core/include/rhistory.rb +95 -0
- data/lib/rbhex/core/include/rinputdataevent.rb +47 -0
- data/lib/rbhex/core/include/vieditable.rb +172 -0
- data/lib/rbhex/core/include/widgetmenu.rb +66 -0
- data/lib/rbhex/core/system/colormap.rb +165 -0
- data/lib/rbhex/core/system/keyboard.rb +150 -0
- data/lib/rbhex/core/system/keydefs.rb +30 -0
- data/lib/rbhex/core/system/ncurses.rb +236 -0
- data/lib/rbhex/core/system/panel.rb +162 -0
- data/lib/rbhex/core/system/window.rb +913 -0
- data/lib/rbhex/core/util/ansiparser.rb +119 -0
- data/lib/rbhex/core/util/app.rb +1228 -0
- data/lib/rbhex/core/util/basestack.rb +410 -0
- data/lib/rbhex/core/util/bottomline.rb +1859 -0
- data/lib/rbhex/core/util/colorparser.rb +77 -0
- data/lib/rbhex/core/util/focusmanager.rb +31 -0
- data/lib/rbhex/core/util/padreader.rb +192 -0
- data/lib/rbhex/core/util/rcommandwindow.rb +604 -0
- data/lib/rbhex/core/util/rdialogs.rb +574 -0
- data/lib/rbhex/core/util/viewer.rb +149 -0
- data/lib/rbhex/core/util/widgetshortcuts.rb +506 -0
- data/lib/rbhex/core/version.rb +5 -0
- data/lib/rbhex/core/widgets/applicationheader.rb +103 -0
- data/lib/rbhex/core/widgets/box.rb +58 -0
- data/lib/rbhex/core/widgets/divider.rb +310 -0
- data/lib/rbhex/core/widgets/keylabelprinter.rb +194 -0
- data/lib/rbhex/core/widgets/rcombo.rb +253 -0
- data/lib/rbhex/core/widgets/rcontainer.rb +415 -0
- data/lib/rbhex/core/widgets/rlink.rb +30 -0
- data/lib/rbhex/core/widgets/rlist.rb +696 -0
- data/lib/rbhex/core/widgets/rmenu.rb +958 -0
- data/lib/rbhex/core/widgets/rmenulink.rb +22 -0
- data/lib/rbhex/core/widgets/rmessagebox.rb +387 -0
- data/lib/rbhex/core/widgets/rprogress.rb +118 -0
- data/lib/rbhex/core/widgets/rtabbedpane.rb +634 -0
- data/lib/rbhex/core/widgets/rtabbedwindow.rb +70 -0
- data/lib/rbhex/core/widgets/rtextarea.rb +960 -0
- data/lib/rbhex/core/widgets/rtextview.rb +739 -0
- data/lib/rbhex/core/widgets/rtree.rb +768 -0
- data/lib/rbhex/core/widgets/rwidget.rb +3277 -0
- data/lib/rbhex/core/widgets/scrollbar.rb +143 -0
- data/lib/rbhex/core/widgets/statusline.rb +113 -0
- data/lib/rbhex/core/widgets/tabular.rb +264 -0
- data/lib/rbhex/core/widgets/tabularwidget.rb +1142 -0
- data/lib/rbhex/core/widgets/textpad.rb +995 -0
- data/lib/rbhex/core/widgets/tree/treecellrenderer.rb +150 -0
- data/lib/rbhex/core/widgets/tree/treemodel.rb +428 -0
- data/rbhex-core.gemspec +32 -0
- metadata +172 -0
@@ -0,0 +1,80 @@
|
|
1
|
+
# ----------------------------------------------------------------------------- #
|
2
|
+
# File: action.rb
|
3
|
+
# Description: A common action class which can be used with buttons, popupmenu
|
4
|
+
# and anythign else that takes an action or command
|
5
|
+
# Author: rkumar http://github.com/rkumar/rbcurse/
|
6
|
+
# Date: been around since the beginning
|
7
|
+
# License: Same as Ruby's License (http://www.ruby-lang.org/LICENSE.txt)
|
8
|
+
# Last update: use ,,L
|
9
|
+
# NOTE: I don't like the dependence on rwidget and EventHandler. Seems it needs
|
10
|
+
# that only for fire_handler and not sure if that's used. I've not bound :FIRE
|
11
|
+
# ever.
|
12
|
+
#
|
13
|
+
# Darn, do i really need to have dsl_accessors and property This is not a
|
14
|
+
# widget and there's no repaint. Do button's and popups really repaint
|
15
|
+
# themselves when a dsl_property is modified ?
|
16
|
+
# ----------------------------------------------------------------------------- #
|
17
|
+
#
|
18
|
+
require 'rbhex/core/widgets/rwidget'
|
19
|
+
include RubyCurses
|
20
|
+
module RubyCurses
|
21
|
+
## encapsulates behaviour allowing centralization
|
22
|
+
# == Example
|
23
|
+
# a = Action.new("&New Row") { commands }
|
24
|
+
# a.accelerator "Alt N"
|
25
|
+
# menu.add(a)
|
26
|
+
# b = Button.new form do
|
27
|
+
# action a
|
28
|
+
# ...
|
29
|
+
# end
|
30
|
+
class Action < Proc
|
31
|
+
include EventHandler # removed 2012-01-3 maybe you can bind FIRE
|
32
|
+
#include ConfigSetup # removed 2012-01-3
|
33
|
+
# name used on button or menu
|
34
|
+
dsl_property :name
|
35
|
+
dsl_property :enabled
|
36
|
+
dsl_accessor :tooltip_text
|
37
|
+
dsl_accessor :help_text
|
38
|
+
dsl_accessor :mnemonic
|
39
|
+
dsl_accessor :accelerator
|
40
|
+
|
41
|
+
def initialize name, config={}, &block
|
42
|
+
super &block
|
43
|
+
@name = name
|
44
|
+
@name.freeze
|
45
|
+
@enabled = true
|
46
|
+
# removing dependency from config
|
47
|
+
#config_setup config # @config.each_pair { |k,v| variable_set(k,v) }
|
48
|
+
@config = config
|
49
|
+
keys = @config.keys
|
50
|
+
keys.each do |e|
|
51
|
+
variable_set(e, @config[e])
|
52
|
+
end
|
53
|
+
@_events = [:FIRE]
|
54
|
+
end
|
55
|
+
def call
|
56
|
+
return unless @enabled
|
57
|
+
# seems to be here, if you've bound :FIRE no this, not on any widget
|
58
|
+
fire_handler :FIRE, self
|
59
|
+
super
|
60
|
+
end
|
61
|
+
# the next 3 are to adapt this to CMenuitems
|
62
|
+
def hotkey
|
63
|
+
return @mnemonic if @mnemonic
|
64
|
+
ix = @name.index('&')
|
65
|
+
if ix
|
66
|
+
return @name[ix+1, 1].downcase
|
67
|
+
end
|
68
|
+
end
|
69
|
+
# to adapt this to CMenuitems
|
70
|
+
def label
|
71
|
+
@name.sub('&','')
|
72
|
+
end
|
73
|
+
# to adapt this to CMenuitems
|
74
|
+
def action
|
75
|
+
self
|
76
|
+
end
|
77
|
+
|
78
|
+
end # class
|
79
|
+
end # module
|
80
|
+
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# ----------------------------------------------------------------------------- #
|
2
|
+
# File: actionmanager.rb
|
3
|
+
# Description: a class that manages actions for a widget
|
4
|
+
#
|
5
|
+
# Author: rkumar http://github.com/rkumar/rbcurse/
|
6
|
+
# Date: 2012-01-4
|
7
|
+
# License: Same as Ruby's License (http://www.ruby-lang.org/LICENSE.txt)
|
8
|
+
# Last update: ,,L
|
9
|
+
# ----------------------------------------------------------------------------- #
|
10
|
+
#
|
11
|
+
# Maintains actions for a widget
|
12
|
+
module RubyCurses
|
13
|
+
class ActionManager
|
14
|
+
include Io
|
15
|
+
attr_reader :actions
|
16
|
+
|
17
|
+
def initialize #form, config={}, &block
|
18
|
+
@actions = []
|
19
|
+
#instance_eval &block if block_given?
|
20
|
+
end
|
21
|
+
def add_action act
|
22
|
+
@actions << act
|
23
|
+
end
|
24
|
+
def remove_action act
|
25
|
+
@actions.remove act
|
26
|
+
end
|
27
|
+
#
|
28
|
+
# insert an item at given position (index)
|
29
|
+
def insert_action pos, *val
|
30
|
+
@actions[pos] = val
|
31
|
+
end
|
32
|
+
#def create_menuitem *args
|
33
|
+
#PromptMenu.create_menuitem *args
|
34
|
+
#end
|
35
|
+
|
36
|
+
# popup the hist
|
37
|
+
#
|
38
|
+
def show_actions
|
39
|
+
return if @actions.empty?
|
40
|
+
list = @actions
|
41
|
+
menu = PromptMenu.new self do |m|
|
42
|
+
list.each { |e|
|
43
|
+
m.add *e
|
44
|
+
}
|
45
|
+
end
|
46
|
+
menu.display_new :title => 'Widget Menu (Press letter)'
|
47
|
+
end
|
48
|
+
end # class
|
49
|
+
end # mod RubyC
|
@@ -0,0 +1,214 @@
|
|
1
|
+
module RubyCurses
|
2
|
+
module Utils
|
3
|
+
private
|
4
|
+
def _suspend clear=true
|
5
|
+
return unless block_given?
|
6
|
+
Ncurses.def_prog_mode
|
7
|
+
if clear
|
8
|
+
Ncurses.endwin
|
9
|
+
# NOTE: avoid false since screen remains half off
|
10
|
+
# too many issues
|
11
|
+
else
|
12
|
+
system "/bin/stty sane"
|
13
|
+
end
|
14
|
+
yield if block_given?
|
15
|
+
Ncurses.reset_prog_mode
|
16
|
+
if !clear
|
17
|
+
# Hope we don't screw your terminal up with this constantly.
|
18
|
+
VER::stop_ncurses
|
19
|
+
VER::start_ncurses
|
20
|
+
#@form.reset_all # not required
|
21
|
+
end
|
22
|
+
@form.repaint if @form
|
23
|
+
@window.wrefresh if @window
|
24
|
+
Ncurses::Panel.update_panels
|
25
|
+
end
|
26
|
+
#
|
27
|
+
# Suspends to shell so user can execute commands.
|
28
|
+
# Maybe not be able to get your prompt correctly.
|
29
|
+
#
|
30
|
+
public
|
31
|
+
def suspend
|
32
|
+
_suspend(false) do
|
33
|
+
system("tput cup 26 0")
|
34
|
+
system("tput ed")
|
35
|
+
system("echo Enter C-d to return to application")
|
36
|
+
system (ENV['PS1']='\s-\v\$ ') if ENV['SHELL']== '/bin/bash'
|
37
|
+
system(ENV['SHELL']);
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
#
|
42
|
+
# Displays application help using either array provided
|
43
|
+
# or checking for :help_text method
|
44
|
+
# @param [Array] help text
|
45
|
+
def display_app_help help_array= nil
|
46
|
+
if help_array
|
47
|
+
arr = help_array
|
48
|
+
elsif respond_to? :help_text
|
49
|
+
arr = help_text
|
50
|
+
else
|
51
|
+
arr = []
|
52
|
+
arr << " NO HELP SPECIFIED FOR APP #{self} "
|
53
|
+
arr << " "
|
54
|
+
arr << " --- General help --- "
|
55
|
+
arr << " F10 - exit application "
|
56
|
+
arr << " Alt-x - select commands "
|
57
|
+
arr << " : - select commands "
|
58
|
+
arr << " "
|
59
|
+
end
|
60
|
+
case arr
|
61
|
+
when String
|
62
|
+
arr = arr.split("\n")
|
63
|
+
when Array
|
64
|
+
end
|
65
|
+
w = arr.max_by(&:length).length
|
66
|
+
|
67
|
+
require 'rbhex/core/util/viewer'
|
68
|
+
RubyCurses::Viewer.view(arr, :layout => [2, 10, [4+arr.size, 24].min, w+2],:close_key => KEY_ENTER, :title => "<Enter> to close", :print_footer => true) do |t|
|
69
|
+
# you may configure textview further here.
|
70
|
+
#t.suppress_borders true
|
71
|
+
#t.color = :black
|
72
|
+
#t.bgcolor = :white
|
73
|
+
# or
|
74
|
+
t.attr = :reverse
|
75
|
+
end
|
76
|
+
end
|
77
|
+
#
|
78
|
+
# prompts user for unix command and displays output in viewer
|
79
|
+
#
|
80
|
+
def shell_output
|
81
|
+
$shell_history ||= []
|
82
|
+
cmd = get_string("Enter shell command:", :maxlen => 50) do |f|
|
83
|
+
require 'rbhex/core/include/rhistory'
|
84
|
+
f.extend(FieldHistory)
|
85
|
+
f.history($shell_history)
|
86
|
+
end
|
87
|
+
if cmd && !cmd.empty?
|
88
|
+
run_command cmd
|
89
|
+
$shell_history.push(cmd) unless $shell_history.include? cmd
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
#
|
94
|
+
# executes given command and displays in viewer
|
95
|
+
# @param [String] unix command, e.g., git -st
|
96
|
+
def run_command cmd
|
97
|
+
# http://whynotwiki.com/Ruby_/_Process_management#What_happens_to_standard_error_.28stderr.29.3F
|
98
|
+
require 'rbhex/core/util/viewer'
|
99
|
+
begin
|
100
|
+
res = `#{cmd} 2>&1`
|
101
|
+
rescue => ex
|
102
|
+
res = ex.to_s
|
103
|
+
res << ex.backtrace.join("\n")
|
104
|
+
end
|
105
|
+
res.gsub!("\t"," ")
|
106
|
+
RubyCurses::Viewer.view(res.split("\n"), :close_key => KEY_ENTER, :title => "<Enter> to close, M-l M-h to scroll")
|
107
|
+
end
|
108
|
+
def shell_out command
|
109
|
+
w = @window || @form.window
|
110
|
+
w.hide
|
111
|
+
Ncurses.endwin
|
112
|
+
ret = system command
|
113
|
+
Ncurses.refresh
|
114
|
+
#Ncurses.curs_set 0 # why ?
|
115
|
+
w.show
|
116
|
+
return ret
|
117
|
+
end
|
118
|
+
end # utils
|
119
|
+
class PrefixCommand
|
120
|
+
attr_accessor :object
|
121
|
+
def initialize _symbol, calling, config={}, &block
|
122
|
+
@object = calling
|
123
|
+
@symbol = _symbol
|
124
|
+
@descriptions = {}
|
125
|
+
define_prefix_command _symbol
|
126
|
+
yield self if block_given?
|
127
|
+
end
|
128
|
+
def define_prefix_command _name, config={}
|
129
|
+
$rb_prefix_map ||= {}
|
130
|
+
#h = {}
|
131
|
+
#@map = h
|
132
|
+
_name = _name.to_sym unless _name.is_a? Symbol
|
133
|
+
# TODO it may already exist, so retrieve it
|
134
|
+
$rb_prefix_map[_name] ||= {}
|
135
|
+
@map = $rb_prefix_map[_name]
|
136
|
+
# create a variable by name _name
|
137
|
+
# create a method by same name to use
|
138
|
+
@object.instance_eval %{
|
139
|
+
def #{_name.to_s} *args
|
140
|
+
h = $rb_prefix_map["#{_name}".to_sym]
|
141
|
+
raise "No prefix_map named #{_name}, #{$rb_prefix_map.keys} " unless h
|
142
|
+
ch = @window.getchar
|
143
|
+
if ch
|
144
|
+
res = h[ch]
|
145
|
+
if res.is_a? Proc
|
146
|
+
res.call
|
147
|
+
else
|
148
|
+
send(res) if res
|
149
|
+
end
|
150
|
+
else
|
151
|
+
0
|
152
|
+
end
|
153
|
+
end
|
154
|
+
}
|
155
|
+
return _name
|
156
|
+
end
|
157
|
+
def call
|
158
|
+
h = @map
|
159
|
+
ch = @object.window.getch # dicey.
|
160
|
+
$log.debug "XXX: CALLED #{ch} "
|
161
|
+
if ch
|
162
|
+
if ch == KEY_F1
|
163
|
+
text = ["Options are: "]
|
164
|
+
h.keys.each { |e| c = keycode_tos(e); text << " #{c} #{@descriptions[e]} " }
|
165
|
+
textdialog text, :title => " #{@symbol} key bindings "
|
166
|
+
return
|
167
|
+
end
|
168
|
+
res = h[ch]
|
169
|
+
if res.is_a? Proc
|
170
|
+
res.call
|
171
|
+
elsif res.is_a? Symbol
|
172
|
+
@object.send(res) if res
|
173
|
+
else
|
174
|
+
Ncurses.beep
|
175
|
+
@object.window.ungetch(ch)
|
176
|
+
|
177
|
+
:UNHANDLED
|
178
|
+
end
|
179
|
+
else
|
180
|
+
raise "got nothing"
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
# define a key within a prefix key map such as C-x
|
185
|
+
# Now that i am moving this from global, how will describe bindings get hold of the bindings
|
186
|
+
# and descriptions
|
187
|
+
def define_key _keycode, *args, &blk
|
188
|
+
_symbol = @symbol
|
189
|
+
h = $rb_prefix_map[_symbol]
|
190
|
+
raise ArgumentError, "No such keymap #{_symbol} defined. Use define_prefix_command." unless h
|
191
|
+
_keycode = _keycode[0].getbyte(0) if _keycode[0].class == String
|
192
|
+
arg = args.shift
|
193
|
+
if arg.is_a? String
|
194
|
+
desc = arg
|
195
|
+
arg = args.shift
|
196
|
+
elsif arg.is_a? Symbol
|
197
|
+
# its a symbol
|
198
|
+
desc = arg.to_s
|
199
|
+
elsif arg.nil?
|
200
|
+
desc = "unknown"
|
201
|
+
else
|
202
|
+
raise ArgumentError, "Don't know how to handle #{arg.class} in PrefixManager"
|
203
|
+
end
|
204
|
+
@descriptions[_keycode] = desc
|
205
|
+
|
206
|
+
if !block_given?
|
207
|
+
blk = arg
|
208
|
+
end
|
209
|
+
h[_keycode] = blk
|
210
|
+
end
|
211
|
+
alias :key :define_key
|
212
|
+
end
|
213
|
+
end # module RubyC
|
214
|
+
include RubyCurses::Utils
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# I am moving the common title and border printing stuff into
|
2
|
+
# a separate module.
|
3
|
+
module RubyCurses
|
4
|
+
module BorderTitle
|
5
|
+
dsl_accessor :suppress_borders #to_print_borders
|
6
|
+
dsl_accessor :border_attrib, :border_color
|
7
|
+
dsl_accessor :title #set this on top
|
8
|
+
dsl_accessor :title_attrib #bold, reverse, normal
|
9
|
+
|
10
|
+
def bordertitle_init
|
11
|
+
@_bordertitle_init_called = true
|
12
|
+
@row_offset = @col_offset = 0 if @suppress_borders
|
13
|
+
@internal_width = 1 if @suppress_borders # the other programs have zero not 1 NOTE
|
14
|
+
end
|
15
|
+
# why the dash does it reduce height by one.
|
16
|
+
def print_borders
|
17
|
+
bordertitle_init unless @_bordertitle_init_called
|
18
|
+
raise ArgumentError, "Graphic not set" unless @graphic
|
19
|
+
raise "#{self} needs width" unless @width
|
20
|
+
raise "#{self} needs height" unless @height
|
21
|
+
width = @width
|
22
|
+
height = @height-1
|
23
|
+
window = @graphic
|
24
|
+
startcol = @col
|
25
|
+
startrow = @row
|
26
|
+
@color_pair = get_color($datacolor)
|
27
|
+
bordercolor = @border_color || @color_pair
|
28
|
+
borderatt = @border_attrib || Ncurses::A_NORMAL
|
29
|
+
window.print_border startrow, startcol, height, width, bordercolor, borderatt
|
30
|
+
print_title
|
31
|
+
end
|
32
|
+
def print_title
|
33
|
+
bordertitle_init unless @_bordertitle_init_called
|
34
|
+
return unless @title
|
35
|
+
raise "#{self} needs width" unless @width
|
36
|
+
@color_pair ||= get_color($datacolor) # should we not use this ??? XXX
|
37
|
+
#$log.debug " print_title #{@row}, #{@col}, #{@width} "
|
38
|
+
# check title.length and truncate if exceeds width
|
39
|
+
_title = @title
|
40
|
+
if @title.length > @width - 2
|
41
|
+
_title = @title[0..@width-2]
|
42
|
+
end
|
43
|
+
@graphic.printstring( @row, @col+(@width-_title.length)/2, _title, @color_pair, @title_attrib) unless @title.nil?
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
48
|
+
include BorderTitle
|
@@ -0,0 +1,203 @@
|
|
1
|
+
# ------------------------------------------------------------ #
|
2
|
+
# File: chunk.rb
|
3
|
+
# Description:
|
4
|
+
# Author: rkumar http://github.com/rkumar/rbcurse/
|
5
|
+
# Date: 07.11.11 - 12:31
|
6
|
+
# Same as Ruby's License (http://www.ruby-lang.org/LICENSE.txt)
|
7
|
+
# Last update: 2013-04-01 13:38
|
8
|
+
# ------------------------------------------------------------ #
|
9
|
+
#
|
10
|
+
|
11
|
+
module RubyCurses
|
12
|
+
module Chunks
|
13
|
+
extend self
|
14
|
+
class Chunk
|
15
|
+
|
16
|
+
# color_pair of associated text
|
17
|
+
# text to print
|
18
|
+
# attribute of associated text
|
19
|
+
#attr_accessor :color, :text, :attrib
|
20
|
+
attr_reader :chunk
|
21
|
+
|
22
|
+
def initialize color, text, attrib
|
23
|
+
@chunk = [ color, text, attrib ]
|
24
|
+
#@color = color
|
25
|
+
#@text = text
|
26
|
+
#@attrib = attrib
|
27
|
+
end
|
28
|
+
def color
|
29
|
+
@chunk[0]
|
30
|
+
end
|
31
|
+
def text
|
32
|
+
@chunk[1]
|
33
|
+
end
|
34
|
+
def attrib
|
35
|
+
@chunk[2]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# consists of an array of chunks and corresponds to a line
|
40
|
+
# to be printed.
|
41
|
+
class ChunkLine
|
42
|
+
|
43
|
+
# an array of chunks
|
44
|
+
attr_reader :chunks
|
45
|
+
|
46
|
+
def initialize arr=nil
|
47
|
+
@chunks = arr.nil? ? Array.new : arr
|
48
|
+
end
|
49
|
+
def <<(chunk)
|
50
|
+
raise ArgumentError, "Chunk object expected. Received #{chunk.class} " unless chunk.is_a? Chunk
|
51
|
+
@chunks << chunk
|
52
|
+
end
|
53
|
+
alias :add :<<
|
54
|
+
def each &block
|
55
|
+
@chunks.each &block
|
56
|
+
end
|
57
|
+
|
58
|
+
# returns length of text in chunks
|
59
|
+
def row_length
|
60
|
+
result = 0
|
61
|
+
@chunks.each { |e| result += e.text.length }
|
62
|
+
return result
|
63
|
+
end
|
64
|
+
# returns match for str in this chunk
|
65
|
+
# added 2013-03-07 - 23:59
|
66
|
+
def index str
|
67
|
+
result = 0
|
68
|
+
@chunks.each { |e| txt = e.text;
|
69
|
+
ix = txt.index(str)
|
70
|
+
return result + ix if ix
|
71
|
+
result += e.text.length
|
72
|
+
}
|
73
|
+
return nil
|
74
|
+
end
|
75
|
+
alias :length :row_length
|
76
|
+
alias :size :row_length
|
77
|
+
|
78
|
+
# return a Chunkline containing only the text for the range requested
|
79
|
+
def substring start, size
|
80
|
+
raise "substring not implemented yet"
|
81
|
+
end
|
82
|
+
def to_s
|
83
|
+
result = ""
|
84
|
+
@chunks.each { |e| result << e.text }
|
85
|
+
result
|
86
|
+
end
|
87
|
+
|
88
|
+
# added to take care of many string methods that are called.
|
89
|
+
# Callers really don't know this is a chunkline, they assume its a string
|
90
|
+
# 2013-03-21 - 19:01
|
91
|
+
def method_missing(sym, *args, &block)
|
92
|
+
self.to_s.send sym, *args, &block
|
93
|
+
end
|
94
|
+
end
|
95
|
+
class ColorParser
|
96
|
+
def initialize cp
|
97
|
+
color_parser cp
|
98
|
+
@color_pair = $datacolor
|
99
|
+
@attrib = FFI::NCurses::A_NORMAL
|
100
|
+
@color_array = [:white]
|
101
|
+
@bgcolor_array = [:black]
|
102
|
+
@attrib_array = [@attrib]
|
103
|
+
@color_pair_array = [@color_pair]
|
104
|
+
@color = :white
|
105
|
+
@bgcolor = :black
|
106
|
+
end
|
107
|
+
#
|
108
|
+
# Takes a formatted string and converts the parsed parts to chunks.
|
109
|
+
#
|
110
|
+
# @param [String] takes the entire line or string and breaks into an array of chunks
|
111
|
+
# @yield chunk if block
|
112
|
+
# @return [ChunkLine] # [Array] array of chunks
|
113
|
+
# @since 1.4.1 2011-11-3 experimental, can change
|
114
|
+
public
|
115
|
+
def convert_to_chunk s, colorp=$datacolor, att=FFI::NCurses::A_NORMAL
|
116
|
+
#require 'rbhex/core/include/chunk'
|
117
|
+
|
118
|
+
@color_parser ||= get_default_color_parser()
|
119
|
+
## defaults
|
120
|
+
color_pair = @color_pair
|
121
|
+
attrib = @attrib
|
122
|
+
#res = []
|
123
|
+
res = ChunkLine.new
|
124
|
+
color = @color
|
125
|
+
bgcolor = @bgcolor
|
126
|
+
# stack the values, so when user issues "/end" we can pop earlier ones
|
127
|
+
|
128
|
+
@color_parser.parse_format(s) do |p|
|
129
|
+
case p
|
130
|
+
when Array
|
131
|
+
## got color / attrib info, this starts a new span
|
132
|
+
|
133
|
+
#color, bgcolor, attrib = *p
|
134
|
+
lc, lb, la = *p
|
135
|
+
if la
|
136
|
+
@attrib = get_attrib la
|
137
|
+
end
|
138
|
+
if lc || lb
|
139
|
+
@color = lc ? lc : @color_array.last
|
140
|
+
@bgcolor = lb ? lb : @bgcolor_array.last
|
141
|
+
@color_array << @color
|
142
|
+
@bgcolor_array << @bgcolor
|
143
|
+
@color_pair = get_color($datacolor, @color, @bgcolor)
|
144
|
+
end
|
145
|
+
@color_pair_array << @color_pair
|
146
|
+
@attrib_array << @attrib
|
147
|
+
#$log.debug "XXX: CHUNK start #{color_pair} , #{attrib} :: c:#{lc} b:#{lb} "
|
148
|
+
#$log.debug "XXX: CHUNK start arr #{@color_pair_array} :: #{@attrib_array} "
|
149
|
+
|
150
|
+
when :endcolor
|
151
|
+
|
152
|
+
# end the current (last) span
|
153
|
+
@color_pair_array.pop
|
154
|
+
@color_pair = @color_pair_array.last
|
155
|
+
@attrib_array.pop
|
156
|
+
@attrib = @attrib_array.last
|
157
|
+
#$log.debug "XXX: CHUNK end #{color_pair} , #{attrib} "
|
158
|
+
#$log.debug "XXX: CHUNK end arr #{@color_pair_array} :: #{@attrib_array} "
|
159
|
+
when :reset # ansi has this
|
160
|
+
# end all previous colors
|
161
|
+
@color_pair = $datacolor # @color_pair_array.first
|
162
|
+
@color_pair_array = [@color_pair]
|
163
|
+
@attrib = FFI::NCurses::A_NORMAL #@attrib_array.first
|
164
|
+
@attrib_array = [@attrib]
|
165
|
+
@bgcolor_array = [@bgcolor_array.first]
|
166
|
+
@color_array = [@color_array.first]
|
167
|
+
|
168
|
+
when String
|
169
|
+
|
170
|
+
## create the chunk
|
171
|
+
#$log.debug "XXX: CHUNK using on #{p} : #{@color_pair} , #{@attrib} " # 2011-12-10 12:38:51
|
172
|
+
|
173
|
+
#chunk = [color_pair, p, attrib]
|
174
|
+
chunk = Chunk.new @color_pair, p, @attrib
|
175
|
+
if block_given?
|
176
|
+
yield chunk
|
177
|
+
else
|
178
|
+
res << chunk
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end # parse
|
182
|
+
return res unless block_given?
|
183
|
+
end
|
184
|
+
def get_default_color_parser
|
185
|
+
require 'rbhex/core/util/colorparser'
|
186
|
+
@color_parser || DefaultColorParser.new
|
187
|
+
end
|
188
|
+
# supply with a color parser, if you supplied formatted text
|
189
|
+
public
|
190
|
+
def color_parser f
|
191
|
+
$log.debug "XXX: color_parser setting in CP to #{f} "
|
192
|
+
if f == :tmux
|
193
|
+
@color_parser = get_default_color_parser()
|
194
|
+
elsif f == :ansi
|
195
|
+
require 'rbhex/core/util/ansiparser'
|
196
|
+
@color_parser = AnsiParser.new
|
197
|
+
else
|
198
|
+
@color_parser = f
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end # class
|
202
|
+
end
|
203
|
+
end
|