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.

– TODO: up, down, right, left, etc could have yielding methods too? ++

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.



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/ansi/code.rb', line 137

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.



50
51
52
# File 'lib/ansi/code.rb', line 50

def self.colors
  %w{black red green yellow blue magenta cyan white}
end

.stylesObject

List of primary styles.



45
46
47
# File 'lib/ansi/code.rb', line 45

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.



108
109
110
# File 'lib/ansi/code.rb', line 108

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" }

Returns:

  • (String)

    String wrapped ANSI code.



222
223
224
225
226
227
228
229
230
231
232
# File 'lib/ansi/code.rb', line 222

def ansi(*codes) #:yield:
  if block_given?
    string = yield.to_s
  else
    string = codes.shift.to_s
  end

  return string unless $ansi

  code(*codes) + string + ENDCODE
end

#code(*codes) ⇒ String

Look-up code from chart, or if Integer simply pass through. Also resolves :random and :on_random.

Parameters:

  • codes (Array<Symbol,Integer] Symbols or integers to covnert to ANSI code.)

    odes [Array<Symbol,Integer] Symbols or integers to covnert to ANSI code.

Returns:



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/ansi/code.rb', line 285

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.



168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/ansi/code.rb', line 168

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.



192
193
194
# File 'lib/ansi/code.rb', line 192

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.



325
326
327
328
329
330
# File 'lib/ansi/code.rb', line 325

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.



197
198
199
# File 'lib/ansi/code.rb', line 197

def left(spaces=1)
  "\e[#{spaces.to_i}D"
end

#move(line, column = 0) ⇒ Object

Move cursor to line and column.



182
183
184
# File 'lib/ansi/code.rb', line 182

def move(line, column=0)
  "\e[#{line.to_i};#{column.to_i}H"
end

#random(background = false) ⇒ Integer

Provides a random primary ANSI color.

Parameters:

  • background (Boolean) (defaults to: false)

    Use ‘true` for background color, otherwise foreground color.

Returns:

  • (Integer)

    ANSI color number



311
312
313
# File 'lib/ansi/code.rb', line 311

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.

Parameters:

  • background (Boolean) (defaults to: false)

    Use ‘true` for background color, otherwise foreground color.



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

def rgb(red, green, blue, background=false)
  "#{background ? 48 : 38};5;#{rgb_value(red, green, blue)}"
end

#right(spaces = 1) ⇒ Object

Move cursor right a specificed number of spaces.



202
203
204
# File 'lib/ansi/code.rb', line 202

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.

– TODO: Allow selective removal using *codes argument? ++

Parameters:

  • String (String)

    from which to remove ANSI codes.

Returns:

  • (String)

    String wrapped ANSI code.



245
246
247
248
249
250
251
252
# File 'lib/ansi/code.rb', line 245

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.



187
188
189
# File 'lib/ansi/code.rb', line 187

def up(spaces=1)
  "\e[#{spaces.to_i}A"
end