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.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/ansi/code.rb', line 144

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.



57
58
59
# File 'lib/ansi/code.rb', line 57

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

.stylesObject

List of primary styles.



52
53
54
# File 'lib/ansi/code.rb', line 52

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.



115
116
117
# File 'lib/ansi/code.rb', line 115

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.



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

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:



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

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.



175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/ansi/code.rb', line 175

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.



199
200
201
# File 'lib/ansi/code.rb', line 199

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.



332
333
334
335
336
337
# File 'lib/ansi/code.rb', line 332

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.



204
205
206
# File 'lib/ansi/code.rb', line 204

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

#move(line, column = 0) ⇒ Object

Move cursor to line and column.



189
190
191
# File 'lib/ansi/code.rb', line 189

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



318
319
320
# File 'lib/ansi/code.rb', line 318

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.



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

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.



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

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.



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

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.



194
195
196
# File 'lib/ansi/code.rb', line 194

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