Module: ANSI::Code

Extended by:
Code
Includes:
Constants
Included in:
ANSI, Code
Defined in:
lib/ansi/code.rb

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

Instance Method Summary collapse

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

.colorsObject

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

.stylesObject

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.

Examples:

ansi("Valentine", :red, :on_white)
ansi(:red, :on_white){ "Valentine" }

229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/ansi/code.rb', line 229

def ansi(*codes) #:yield:
  if block_given?
    string = yield.to_s
  else
    # first argument must be the string
    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.


294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/ansi/code.rb', line 294

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.


334
335
336
337
338
339
# File 'lib/ansi/code.rb', line 334

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 Also known as: back

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.


320
321
322
# File 'lib/ansi/code.rb', line 320

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.


329
330
331
# File 'lib/ansi/code.rb', line 329

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.

Raises:

  • (ArgumentError)

344
345
346
347
348
349
# File 'lib/ansi/code.rb', line 344

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 Also known as: forward

Move cursor right a specificed number of spaces.


208
209
210
# File 'lib/ansi/code.rb', line 208

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.


254
255
256
257
258
259
260
261
# File 'lib/ansi/code.rb', line 254

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