Module: ANSI::Code
Overview
ANSI Codes
Ansi::Code module makes it very easy to use ANSI codes. These are esspecially nice for beautifying shell output.
Ansi::Code.red + "Hello" + Ansi::Code.blue + "World"
=> "\e[31mHello\e[34mWorld"
Ansi::Code.red{ "Hello" } + Ansi::Code.blue{ "World" }
=> "\e[31mHello\e[0m\e[34mWorld\e[0m"
IMPORTANT! Do not mixin Ansi::Code, instead use Mixin.
See CHART for list of all supported codes.
Constant Summary collapse
- PATTERN =
Regexp for matching most ANSI codes.
/\e\[(\d+)m/
- ENDCODE =
ANSI clear code.
"\e[0m"
Class Method Summary collapse
-
.colors ⇒ Object
List of primary colors.
-
.styles ⇒ Object
List of primary styles.
Instance Method Summary collapse
-
#[](*codes) ⇒ Object
Return ANSI code given a list of symbolic names.
-
#ansi(*codes) ⇒ String
(also: #style, #color)
Apply ANSI codes to a first argument or block value.
-
#code(*codes) ⇒ String
Look-up code from chart, or if Integer simply pass through.
-
#display(line, column = 0) ⇒ Object
Like
move
but returns to original positon after yielding the block. -
#down(spaces = 1) ⇒ Object
Move cursor down a specificed number of spaces.
-
#hex(string, background = false) ⇒ Object
Creates an xterm-256 color from a CSS-style color string.
-
#left(spaces = 1) ⇒ Object
Move cursor left a specificed number of spaces.
-
#method_missing(code, *args, &blk) ⇒ Object
Use method missing to dispatch ANSI code methods.
-
#move(line, column = 0) ⇒ Object
Move cursor to line and column.
-
#random(background = false) ⇒ Integer
Provides a random primary ANSI color.
-
#rgb(red, green, blue, background = false) ⇒ Object
Creates an xterm-256 color from rgb value.
-
#rgb_256(r, g, b) ⇒ Object
private
Gets closest xterm-256 color.
-
#right(spaces = 1) ⇒ Object
Move cursor right a specificed number of spaces.
-
#unansi(string = nil) ⇒ String
(also: #unstyle, #uncolor)
Remove ANSI codes from string or block value.
-
#up(spaces = 1) ⇒ Object
Move cursor up a specificed number of spaces.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(code, *args, &blk) ⇒ Object
Use method missing to dispatch ANSI code methods.
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/ansi/code.rb', line 142 def method_missing(code, *args, &blk) esc = nil if CHART.key?(code) esc = "\e[#{CHART[code]}m" elsif SPECIAL_CHART.key?(code) esc = SPECIAL_CHART[code] end if esc if string = args.first return string unless $ansi #warn "use ANSI block notation for future versions" return "#{esc}#{string}#{ENDCODE}" end if block_given? return yield unless $ansi return "#{esc}#{yield}#{ENDCODE}" end esc else super(code, *args, &blk) end end |
Class Method Details
.colors ⇒ Object
List of primary colors.
55 56 57 |
# File 'lib/ansi/code.rb', line 55 def self.colors %w{black red green yellow blue magenta cyan white} end |
.styles ⇒ Object
List of primary styles.
50 51 52 |
# File 'lib/ansi/code.rb', line 50 def self.styles %w{bold dark italic underline underscore blink rapid reverse negative concealed strike} end |
Instance Method Details
#[](*codes) ⇒ Object
Return ANSI code given a list of symbolic names.
113 114 115 |
# File 'lib/ansi/code.rb', line 113 def [](*codes) code(*codes) end |
#ansi(*codes) ⇒ String Also known as: style, color
Apply ANSI codes to a first argument or block value.
227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/ansi/code.rb', line 227 def ansi(*codes) #:yield: if block_given? string = yield.to_s else string = codes.shift.to_s end return string unless $ansi c = code(*codes) c + string.gsub(ENDCODE, ENDCODE + c) + ENDCODE end |
#code(*codes) ⇒ String
Look-up code from chart, or if Integer simply pass through. Also resolves :random and :on_random.
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/ansi/code.rb', line 291 def code(*codes) list = [] codes.each do |code| list << \ case code when Integer code when Array rgb(*code) when :random random when :on_random random(true) else CHART[code.to_sym] end end "\e[" + (list * ";") + "m" end |
#display(line, column = 0) ⇒ Object
Like move
but returns to original positon after yielding the block.
173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/ansi/code.rb', line 173 def display(line, column=0) #:yield: result = "\e[s" result << "\e[#{line.to_i};#{column.to_i}H" if block_given? result << yield result << "\e[u" #elsif string # result << string # result << "\e[u" end result end |
#down(spaces = 1) ⇒ Object
Move cursor down a specificed number of spaces.
197 198 199 |
# File 'lib/ansi/code.rb', line 197 def down(spaces=1) "\e[#{spaces.to_i}B" end |
#hex(string, background = false) ⇒ Object
Creates an xterm-256 color from a CSS-style color string.
331 332 333 334 335 336 |
# File 'lib/ansi/code.rb', line 331 def hex(string, background=false) string.tr!('#','') x = (string.size == 6 ? 2 : 1) r, g, b = [0,1,2].map{ |i| string[i*x,2].to_i(16) } rgb(r, g, b, background) end |
#left(spaces = 1) ⇒ Object
Move cursor left a specificed number of spaces.
202 203 204 |
# File 'lib/ansi/code.rb', line 202 def left(spaces=1) "\e[#{spaces.to_i}D" end |
#move(line, column = 0) ⇒ Object
Move cursor to line and column.
187 188 189 |
# File 'lib/ansi/code.rb', line 187 def move(line, column=0) "\e[#{line.to_i};#{column.to_i}H" end |
#random(background = false) ⇒ Integer
Provides a random primary ANSI color.
317 318 319 |
# File 'lib/ansi/code.rb', line 317 def random(background=false) (background ? 40 : 30) + rand(8) end |
#rgb(red, green, blue, background = false) ⇒ Object
Creates an xterm-256 color from rgb value.
326 327 328 |
# File 'lib/ansi/code.rb', line 326 def rgb(red, green, blue, background=false) "#{background ? 48 : 38};5;#{rgb_value(red, green, blue)}" end |
#rgb_256(r, g, b) ⇒ Object (private)
Gets closest xterm-256 color.
341 342 343 344 345 346 |
# File 'lib/ansi/code.rb', line 341 def rgb_256(r, g, b) r, g, b = [r, g, b].map{ |c| rgb_valid(c); (6 * (c.to_f / 256.0)).to_i } v = (r * 36 + g * 6 + b + 16).abs raise ArgumentError, "RGB value outside 0-255 range" if v > 255 v end |
#right(spaces = 1) ⇒ Object
Move cursor right a specificed number of spaces.
207 208 209 |
# File 'lib/ansi/code.rb', line 207 def right(spaces=1) "\e[#{spaces.to_i}C" end |
#unansi(string = nil) ⇒ String Also known as: unstyle, uncolor
Remove ANSI codes from string or block value.
251 252 253 254 255 256 257 258 |
# File 'lib/ansi/code.rb', line 251 def unansi(string=nil) #:yield: if block_given? string = yield.to_s else string = string.to_s end string.gsub(PATTERN, '') end |
#up(spaces = 1) ⇒ Object
Move cursor up a specificed number of spaces.
192 193 194 |
# File 'lib/ansi/code.rb', line 192 def up(spaces=1) "\e[#{spaces.to_i}A" end |