Module: CE
- Extended by:
- CE
- Included in:
- CE
- Defined in:
- lib/color_echo/const.rb,
lib/color_echo/internal.rb,
lib/color_echo/override.rb,
lib/color_echo/functions.rb,
lib/color_echo/variables.rb,
lib/color_echo/module/off.rb,
lib/color_echo/private_task.rb,
lib/color_echo/module/textattr.rb,
lib/color_echo/module/background.rb,
lib/color_echo/module/foreground.rb
Defined Under Namespace
Modules: BackGround, ForeGround, Off, TextAttr
Constant Summary collapse
- LIBS_NAME =
"color_echo"- VERSION =
"3.1.1"- SUMMARY =
"Decorates command line output with ANSI escape sequence."- HOMEPAGE =
"http://colorecho.github.io"- DESCRIPTION =
%(#{SUMMARY} #{HOMEPAGE})- @@allow_output =
false- @@enable =
true- @@isset =
false- @@rainbow =
false- @@refresh =
false- @@refresh_pre_match =
false- @@code_bg_color =
""- @@code_fg_color =
""- @@code_text_attr =
""- @@code_highlights =
[]
- @@code_rainbow =
""- @@cnt_limit =
count number for times method
0- @@pickup_list =
pickup patterns and decoration of it
{}
- @@cnt_pickups =
how much patterns having?
0- @@stateful_getter =
is stateful when ‘color_echo/get’?
false- @@assgined =
assgined methods to decorate
[:puts, :p, :print, :printf, :putc]
- @@puts =
save methods
method :puts
- @@p =
method :p
- @@print =
method :print
- @@printf =
method :printf
- @@putc =
method :putc
- @@get =
closure for CE.get
lambda do |text| print text end
- @@task =
assgined methods will be overwritten as this closure
lambda do |*arg| # get function name that was called if RUBY_VERSION >= "2.3.0" called = caller_locations(1).first.label else called = caller_locations(2).first.label end if available? && called == "get" || @@assgined.index(called.intern) task_available(called, arg) else # no available "color echo" task_unuse(called, arg) end end
Instance Method Summary collapse
-
#add_pickup_code(text) ⇒ Object
add pickup code.
-
#add_rainbow(text) ⇒ Object
add code to be a rainbow color.
-
#add_reset_line_feed(input) ⇒ Object
add reset & start code to line feed code.
-
#available? ⇒ Boolean
is allow to use? and is set code?.
-
#ch(fg, bg = nil, txs = nil) ⇒ Object
Self.
-
#ch_bg(name) ⇒ Object
(also: #bg)
Self.
-
#ch_fg(name) ⇒ Object
(also: #fg)
Self.
-
#ch_tx(*names) ⇒ Object
(also: #tx)
Self.
-
#cleanup_text(text) ⇒ Object
try to remove escape sequence code.
-
#convert_to_code(module_name, name) ⇒ Object
get sequence code by symbol.
-
#disable_refresh ⇒ Object
Disable refresh flag.
-
#enable? ⇒ Boolean
is allow to use?.
-
#enable_refresh(scope = :all) ⇒ Object
Enable refresh flag.
-
#get(text) ⇒ Object
get decorated text require “color_echo/get”.
-
#get_assigned ⇒ Object
Array.
-
#get_highlight_code(index = 0) ⇒ Object
return start escape sequence code.
-
#get_reset_code ⇒ Object
String.
-
#get_start_code ⇒ Object
return start escape sequence code.
-
#highlight(fg = nil, bg = nil, *txs) ⇒ Object
change hit lines decoration.
-
#isset? ⇒ Boolean
is set code?.
- #once ⇒ Object
-
#pickup(target, fg = :cyan, bg = nil, *txs) ⇒ Object
to decorate only the specified target.
-
#rainbow ⇒ Object
change mode to rainbow.
-
#reset(scope = :all) ⇒ Object
(also: #off, #disable)
reset code.
-
#reset_all ⇒ Object
reset all state of code.
-
#reset_bg ⇒ Object
reset background code.
-
#reset_fg ⇒ Object
reset foreground code.
-
#reset_highlight ⇒ Object
reset highlight code.
-
#reset_pickup ⇒ Object
reset pickup code.
-
#reset_rainbow ⇒ Object
reset and off raubow mode.
-
#reset_tx ⇒ Object
reset text attr code.
-
#stateful ⇒ Object
CE.get will be stateful.
-
#stateless ⇒ Object
CE.get will be stateless.
-
#task ⇒ Object
Proc.
-
#times(cnt) ⇒ Object
auto off until output set count.
-
#unuse ⇒ Object
do not allow to use.
-
#withdraw(*arg) ⇒ Object
Array.
Instance Method Details
#add_pickup_code(text) ⇒ Object
add pickup code
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/color_echo/internal.rb', line 158 def add_pickup_code(text) if @@refresh_pre_match # remove sequence code from given If it matches pickups orgtext = text.clone text = cleanup_text(text) end match_pattern_keys = [] # repeat as called pickup method @@pickup_list.each_pair do |key, hash| patterns = hash[:patterns] code_pickup = hash[:code] index = hash[:index] # repeat as patterns patterns.each do |pattern| if pattern.is_a?(Regexp) # pattern is Regexp if (pattern =~ text && !match_pattern_keys.index(index)) match_pattern_keys << index end if match_pattern_keys.size > 1 code_highlight = get_highlight_code(0) else code_highlight = get_highlight_code(index) end # global match after_text = "" (text.scan(pattern)).size.times do pattern =~ text after_text += $` + get_reset_code + code_pickup + $& + get_reset_code + get_start_code + code_highlight text = $' end text = after_text + text else # pattern is String if (text.index(pattern) != nil && !match_pattern_keys.index(index)) match_pattern_keys << index end if match_pattern_keys.size > 1 code_highlight = get_highlight_code(0) else code_highlight = get_highlight_code(index) end text.gsub!(pattern, get_reset_code + code_pickup + pattern + get_reset_code + get_start_code + code_highlight) end end end if match_pattern_keys.size == 0 # not match text = orgtext if @@refresh_pre_match return text end if match_pattern_keys.size == 1 text = get_highlight_code(match_pattern_keys[0]) + text + get_reset_code else # many pattern matched, use first -H option text = get_highlight_code(0) + text + get_reset_code end return text end |
#add_rainbow(text) ⇒ Object
add code to be a rainbow color
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/color_echo/internal.rb', line 119 def add_rainbow(text) cnt = 0 output = get_start_code text.each_char do |char| if char == $/ output += get_reset_code + $/ + get_start_code next end # TODO This way so lame... case cnt when 0 output += ForeGround::RED + char + Off::ALL + get_start_code when 1 output += ForeGround::GREEN + char + Off::ALL + get_start_code when 2 output += ForeGround::YELLOW + char + Off::ALL + get_start_code when 3 output += ForeGround::BLUE + char + Off::ALL + get_start_code when 4 output += ForeGround::MAGENTA + char + Off::ALL + get_start_code when 5 output += ForeGround::CYAN + char + Off::ALL + get_start_code when 6 output += ForeGround::WHITE + char + Off::ALL + get_start_code end cnt += 1 cnt = 0 if cnt >= 7 end output += get_reset_code return output end |
#add_reset_line_feed(input) ⇒ Object
add reset & start code to line feed code
109 110 111 112 113 114 |
# File 'lib/color_echo/internal.rb', line 109 def add_reset_line_feed(input) input = input.unpack("C*").pack("U*") if !input.valid_encoding? input.gsub!(/#{$/}/, get_reset_code + $/ + get_start_code) input += get_reset_code return input end |
#available? ⇒ Boolean
is allow to use? and is set code?
10 11 12 |
# File 'lib/color_echo/internal.rb', line 10 def available? return @@enable && isset? end |
#ch(fg, bg = nil, txs = nil) ⇒ Object
Returns self.
97 98 99 100 101 102 103 |
# File 'lib/color_echo/functions.rb', line 97 def ch(fg, bg=nil, txs=nil) ch_fg(fg) ch_bg(bg) ch_tx(*txs) # passing expand return self end |
#ch_bg(name) ⇒ Object Also known as: bg
Returns self.
70 71 72 73 74 75 76 77 |
# File 'lib/color_echo/functions.rb', line 70 def ch_bg(name) return nil if !name.is_a?(Symbol) @@rainbow = false if @@rainbow @@code_bg_color = convert_to_code("BackGround", name) return self end |
#ch_fg(name) ⇒ Object Also known as: fg
Returns self.
59 60 61 62 63 64 65 66 |
# File 'lib/color_echo/functions.rb', line 59 def ch_fg(name) return nil if !name.is_a?(Symbol) @@rainbow = false if @@rainbow @@code_fg_color = convert_to_code("ForeGround", name) return self end |
#ch_tx(*names) ⇒ Object Also known as: tx
Returns self.
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/color_echo/functions.rb', line 81 def ch_tx(*names) @@rainbow = false if @@rainbow names = names[0] if names[0].instance_of?(Array) names.each do |name| next if !name.is_a?(Symbol) @@code_text_attr += convert_to_code("TextAttr", name) end return self end |
#cleanup_text(text) ⇒ Object
try to remove escape sequence code
259 260 261 262 |
# File 'lib/color_echo/internal.rb', line 259 def cleanup_text(text) text.gsub!(/\e\[[0-9;]+[mK]/, "") return text end |
#convert_to_code(module_name, name) ⇒ Object
get sequence code by symbol
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/color_echo/internal.rb', line 232 def convert_to_code(module_name, name) return "" if (!name.is_a?(Symbol) || name == :nil) begin cname = name.to_s.swapcase # specified color index if cname.index("INDEX") num = cname.sub("INDEX", "").to_i - 1 return eval(%{#{module_name}::INDEX}) + num.to_s + "m" end code = eval(%{#{module_name}::#{cname}}) rescue NameError raise(NameError ,%{:#{name} is not defined! Please check reference.}) end return code end |
#disable_refresh ⇒ Object
Disable refresh flag
210 211 212 213 214 |
# File 'lib/color_echo/functions.rb', line 210 def disable_refresh @@refresh = false @@refresh_pre_match = false return self end |
#enable? ⇒ Boolean
is allow to use?
4 5 6 |
# File 'lib/color_echo/internal.rb', line 4 def enable? return @@enable end |
#enable_refresh(scope = :all) ⇒ Object
Enable refresh flag
198 199 200 201 202 203 204 205 206 |
# File 'lib/color_echo/functions.rb', line 198 def enable_refresh(scope=:all) if scope == :all @@refresh = true elsif scope == :prematch @@refresh_pre_match = true end return self end |
#get(text) ⇒ Object
get decorated text require “color_echo/get”
182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/color_echo/functions.rb', line 182 def get(text) if @@allow_output caller()[0] =~ /(.*?):(\d+)/ warn (%([WARNING] #{$1} #{$2}: You can't call CE.get. You must read like -> require 'color_echo/get' )) return text end if !text.is_a?(String) text = text.to_s end @@task.call(text) end |
#get_assigned ⇒ Object
Returns array.
241 242 243 |
# File 'lib/color_echo/functions.rb', line 241 def get_assigned return @@assgined end |
#get_highlight_code(index = 0) ⇒ Object
return start escape sequence code
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/color_echo/internal.rb', line 88 def get_highlight_code(index=0) if @@code_highlights[index] == nil code = @@code_highlights[0] if code == nil # e.g. no specify any -H option return "" else return code end end return @@code_highlights[index] end |
#get_reset_code ⇒ Object
Returns String.
103 104 105 |
# File 'lib/color_echo/internal.rb', line 103 def get_reset_code return self::Off::ALL end |
#get_start_code ⇒ Object
return start escape sequence code
77 78 79 80 81 82 83 |
# File 'lib/color_echo/internal.rb', line 77 def get_start_code if @@rainbow return @@code_rainbow else return @@code_fg_color + @@code_bg_color + @@code_text_attr end end |
#highlight(fg = nil, bg = nil, *txs) ⇒ Object
change hit lines decoration
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/color_echo/functions.rb', line 160 def highlight(fg=nil, bg=nil, *txs) fg = convert_to_code("ForeGround", fg) bg = convert_to_code("BackGround", bg) tx = "" if txs.size > 0 # if text attribute has input as array txs = txs[0] if txs[0].instance_of?(Array) txs.each do |name| next if !name.is_a?(Symbol) tx += convert_to_code("TextAttr", name) end end @@code_highlights << fg + bg + tx return self end |
#isset? ⇒ Boolean
is set code?
16 17 18 |
# File 'lib/color_echo/internal.rb', line 16 def isset? return get_start_code != "" || @@pickup_list.size > 0 || @@rainbow end |
#once ⇒ Object
53 54 55 |
# File 'lib/color_echo/functions.rb', line 53 def once times(1) end |
#pickup(target, fg = :cyan, bg = nil, *txs) ⇒ Object
to decorate only the specified target
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/color_echo/functions.rb', line 111 def pickup(target, fg=:cyan, bg=nil, *txs) key = target.object_id.to_s @@pickup_list[key] = {} if target.is_a?(Array) @@pickup_list[key][:patterns] = target else @@pickup_list[key][:patterns] = [target] end if fg.instance_of?(Symbol) code_fg = convert_to_code("ForeGround", fg) else code_fg = "" end if bg.instance_of?(Symbol) code_bg = convert_to_code("BackGround", bg) else code_bg = "" end code_tx = "" if txs.size > 0 txs = txs[0] if txs[0].instance_of?(Array) txs.each do |name| next if !name.is_a?(Symbol) code_tx += convert_to_code("TextAttr", name) end end @@pickup_list[key][:code] = code_fg + code_bg + code_tx @@pickup_list[key][:index] = @@cnt_pickups @@cnt_pickups += 1 return self end |
#rainbow ⇒ Object
change mode to rainbow
151 152 153 154 155 156 |
# File 'lib/color_echo/functions.rb', line 151 def rainbow @@rainbow = true @@code_rainbow = @@code_bg_color + @@code_text_attr return self end |
#reset(scope = :all) ⇒ Object Also known as: off, disable
reset code
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/color_echo/functions.rb', line 11 def reset(scope=:all) if scope.is_a?(Array) scopes = scope else scopes = [scope] end scopes.each do |scope| case scope when :all reset_fg reset_bg reset_tx reset_pickup reset_highlight reset_rainbow when :fg reset_fg when :bg reset_bg when :tx reset_tx when :pickup reset_pickup when :highlight reset_highlight when :rainbow reset_rainbow end end return self end |
#reset_all ⇒ Object
reset all state of code
66 67 68 69 70 71 72 73 |
# File 'lib/color_echo/internal.rb', line 66 def reset_all reset_fg reset_bg reset_tx reset_pickup reset_rainbow return self end |
#reset_bg ⇒ Object
reset background code
29 30 31 32 |
# File 'lib/color_echo/internal.rb', line 29 def reset_bg @@code_bg_color = "" return self end |
#reset_fg ⇒ Object
reset foreground code
22 23 24 25 |
# File 'lib/color_echo/internal.rb', line 22 def reset_fg @@code_fg_color = "" return self end |
#reset_highlight ⇒ Object
reset highlight code
51 52 53 54 |
# File 'lib/color_echo/internal.rb', line 51 def reset_highlight @@code_highlights = [] return self end |
#reset_pickup ⇒ Object
reset pickup code
43 44 45 46 47 |
# File 'lib/color_echo/internal.rb', line 43 def reset_pickup @@pickup_list = {} @@cnt_pickups = 0 return self end |
#reset_rainbow ⇒ Object
reset and off raubow mode
58 59 60 61 62 |
# File 'lib/color_echo/internal.rb', line 58 def reset_rainbow @@code_rainbow = "" @@rainbow = false return self end |
#reset_tx ⇒ Object
reset text attr code
36 37 38 39 |
# File 'lib/color_echo/internal.rb', line 36 def reset_tx @@code_text_attr = "" return self end |
#stateful ⇒ Object
CE.get will be stateful
218 219 220 221 |
# File 'lib/color_echo/functions.rb', line 218 def stateful @@stateful_getter = true return self end |
#stateless ⇒ Object
CE.get will be stateless
224 225 226 227 |
# File 'lib/color_echo/functions.rb', line 224 def stateless @@stateful_getter = false return self end |
#task ⇒ Object
Returns proc.
252 253 254 |
# File 'lib/color_echo/internal.rb', line 252 def task return @@task end |
#times(cnt) ⇒ Object
auto off until output set count
48 49 50 51 |
# File 'lib/color_echo/functions.rb', line 48 def times(cnt) @@cnt_limit = cnt return self end |
#unuse ⇒ Object
do not allow to use
4 5 6 |
# File 'lib/color_echo/functions.rb', line 4 def unuse @@enable = false end |
#withdraw(*arg) ⇒ Object
Returns array.
231 232 233 234 235 236 237 238 |
# File 'lib/color_echo/functions.rb', line 231 def withdraw(*arg) deleted = [] arg.each do |target| deleted << @@assgined.delete(target) end return deleted end |