ANSICode

ANSICode

Module which makes it very easy to use ANSI codes. These are esspecially nice for beautifying shell output.

  include ANSICode

  p red, "Hello", blue, "World"
  => "\e[31mHello\e[34mWorld"

  p red { "Hello" } + blue { "World" }
  => "\e[31mHello\e[0m\e[34mWorld\e[0m"

Supported ANSI Comands

The following is a list of supported codes.

    save
    restore
    clear_screen
    cls             # synonym for :clear_screen
    clear_line
    clr             # synonym for :clear_line
    move
    up
    down
    left
    right
    display

    clear
    reset           # synonym for :clear
    bold
    dark
    italic          # not widely implemented
    underline
    underscore      # synonym for :underline
    blink
    rapid_blink     # not widely implemented
    negative        # no reverse because of String#reverse
    concealed
    strikethrough   # not widely implemented

    black
    red
    green
    yellow
    blue
    magenta
    cyan
    white

    on_black
    on_red
    on_green
    on_yellow
    on_blue
    on_magenta
    on_cyan
    on_white

Constants

ColoredRegexp

Public Class Methods

define_ansicolor_method(name,code) click to toggle source

Define color codes.

     # File lib/more/facets/ansicode.rb, line 231
231:   def self.define_ansicolor_method(name,code)
232:     class_eval       def #{name.to_s}(string = nil)          result = "\e[#{code}m"          if block_given?              result << yield              result << "\e[0m"          elsif string              result << string              result << "\e[0m"          elsif respond_to?(:to_str)              result << self              result << "\e[0m"          end          return result      end
233:   end

Public Instance Methods

clear_line() click to toggle source

Clear to the end of the current line.

     # File lib/more/facets/ansicode.rb, line 169
169:   def clear_line
170:     "\e[K"
171:   end
Also aliased as: clr
clear_screen() click to toggle source

Clear the screen and move cursor to home.

     # File lib/more/facets/ansicode.rb, line 162
162:   def clear_screen
163:     "\e[2J"
164:   end
Also aliased as: cls
clr() click to toggle source
Alias for: clear_line
cls() click to toggle source
Alias for: clear_screen
colors() click to toggle source
     # File lib/more/facets/ansicode.rb, line 301
301:   def colors
302:     @@colors.map { |c| c[0] }
303:   end
display( line, column=0, string=nil ) click to toggle source

Like move but returns to original positon after yielding block or adding string argument.

     # File lib/more/facets/ansicode.rb, line 213
213:   def display( line, column=0, string=nil ) #:yield:
214:     result = "\e[s"
215:     result << "\e[#{line.to_i};#{column.to_i}H"
216:     if block_given?
217:       result << yield
218:       result << "\e[u"
219:     elsif string
220:       result << string
221:       result << "\e[u"
222:     elsif respond_to?(:to_str)
223:       result << self
224:       result << "\e[u"
225:     end
226:     return result
227:   end
down( spaces=1 ) click to toggle source

Move cursor down a specificed number of spaces.

     # File lib/more/facets/ansicode.rb, line 194
194:   def down( spaces=1 )
195:     "\e[#{spaces.to_i}B"
196:   end
left( spaces=1 ) click to toggle source

Move cursor left a specificed number of spaces.

     # File lib/more/facets/ansicode.rb, line 200
200:   def left( spaces=1 )
201:     "\e[#{spaces.to_i}D"
202:   end
move( line, column=0 ) click to toggle source

Move curose to line and column.

     # File lib/more/facets/ansicode.rb, line 182
182:   def move( line, column=0 )
183:     "\e[#{line.to_i};#{column.to_i}H"
184:   end
restore() click to toggle source

Restore saved cursor positon.

     # File lib/more/facets/ansicode.rb, line 156
156:   def restore
157:     "\e[u"
158:   end
right( spaces=1 ) click to toggle source

Move cursor right a specificed number of spaces.

     # File lib/more/facets/ansicode.rb, line 206
206:   def right( spaces=1 )
207:     "\e[#{spaces.to_i}C"
208:   end
save() click to toggle source

Save current cursor positon.

     # File lib/more/facets/ansicode.rb, line 150
150:   def save
151:     "\e[s"
152:   end
uncolored(string = nil) click to toggle source
     # File lib/more/facets/ansicode.rb, line 287
287:   def uncolored(string = nil)
288:     if block_given?
289:       yield.gsub(ColoredRegexp, '')
290:     elsif string
291:       string.gsub(ColoredRegexp, '')
292:     elsif respond_to?(:to_str)
293:       gsub(ColoredRegexp, '')
294:     else
295:       ''
296:     end
297:   end
up( spaces=1 ) click to toggle source

Move cursor up a specificed number of spaces.

     # File lib/more/facets/ansicode.rb, line 188
188:   def up( spaces=1 )
189:     "\e[#{spaces.to_i}A"
190:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.