Module: ANSI::Code

Extended by:
Code
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.

include Ansi::Code

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

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

Supported ANSI Commands

The following is a list of supported display codes.

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

The following is a list of supported “style” codes.

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

The following is a list of supported color codes.

black
red
green
yellow
blue
magenta
cyan
white

on_black
on_red
on_green
on_yellow
on_blue
on_magenta
on_cyan
on_white

In addition there are color combinations like red_on_white.

Acknowledgement

This library is a partial adaptation of ANSIColor by Florian Frank.

ANSIColor Copyright © 2002 Florian Frank

Developer’s Notes

TODO: Any ANSI codes left to add? Modes?

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

Constant Summary collapse

CLEAR =
"\e[0m"
RESET =
"\e[0m"
BOLD =
"\e[1m"
DARK =
"\e[2m"
ITALIC =

not widely implemented

"\e[3m"
UNDERLINE =
"\e[4m"
UNDERSCORE =
"\e[4m"
"\e[5m"
RAPID =

not widely implemented

"\e[6m"
REVERSE =
"\e[7m"
NEGATIVE =

alternate to reverse because of String#reverse

"\e[7m"
CONCEALED =
"\e[8m"
STRIKE =

not widely implemented

"\e[9m"
BLACK =
"\e[30m"
RED =
"\e[31m"
GREEN =
"\e[32m"
YELLOW =
"\e[33m"
BLUE =
"\e[34m"
MAGENTA =
"\e[35m"
CYAN =
"\e[36m"
WHITE =
"\e[37m"
ON_BLACK =
"\e[40m"
ON_RED =
"\e[41m"
ON_GREEN =
"\e[42m"
ON_YELLOW =
"\e[43m"
ON_BLUE =
"\e[44m"
ON_MAGENTA =
"\e[45m"
ON_CYAN =
"\e[46m"
ON_WHITE =
"\e[47m"
SAVE =

Save current cursor positon.

"\e[s"
RESTORE =

Restore saved cursor positon.

"\e[u"
CLEAR_LINE =

Clear to the end of the current line.

"\e[K"
CLR =

Clear to the end of the current line.

"\e[K"
CLEAR_SCREEN =

Clear the screen and move cursor to home.

"\e[2J"
CLS =

Clear the screen and move cursor to home.

"\e[2J"
PATTERN =

Regexp for matching style and color codes.

/\e\[([34][0-7]|[0-9])m/
TABLE =

Table of style and color codes.

{
  :clear        =>   0,
  :reset        =>   0,
  :bold         =>   1,
  :dark         =>   2,
  :italic       =>   3,
  :underline    =>   4,
  :underscore   =>   4,
  :blink        =>   5,
  :rapid        =>   6,
  :reverse      =>   7,
  :negative     =>   7,
  :concealed    =>   8,
  :strike       =>   9,
  :black        =>  30,
  :red          =>  31,
  :green        =>  32,
  :yellow       =>  33,
  :blue         =>  34,
  :magenta      =>  35,
  :cyan         =>  36,
  :white        =>  37,
  :on_black     =>  40,
  :on_red       =>  41,
  :on_green     =>  42,
  :on_yellow    =>  43,
  :on_blue      =>  44,
  :on_magenta   =>  45,
  :on_cyan      =>  46,
  :on_white     =>  47
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.colorsObject



158
159
160
# File 'lib/ansi/code.rb', line 158

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

.stylesObject



139
140
141
# File 'lib/ansi/code.rb', line 139

def self.styles
  %w{bold dark italic underline underscore blink rapid reverse negative concealed strike}
end

Instance Method Details

#ansi(string, *codes) ⇒ Object

This method is just like #style, except it takes a string rather than a block. The primary purpose of this method is to speed up the String#ansi call.

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


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

def ansi(string, *codes)
  s = ""
  codes.each do |code|
    s << "\e[#{TABLE[code]}m"
  end
  s << string
  s << CLEAR
end

#clearObject

Clear code.



211
212
213
# File 'lib/ansi/code.rb', line 211

def clear
  CLEAR
end

#clear_lineObject

Clear to the end of the current line.



231
232
233
# File 'lib/ansi/code.rb', line 231

def clear_line
  CLEAR_LINE
end

#clear_screenObject

Clear the screen and move cursor to home.



241
242
243
# File 'lib/ansi/code.rb', line 241

def clear_screen
  CLEAR_SCREEN
end

#clrObject

Clear to the end of the current line.



236
237
238
# File 'lib/ansi/code.rb', line 236

def clr
  CLR
end

#clsObject

Clear the screen and move cursor to home.



246
247
248
# File 'lib/ansi/code.rb', line 246

def cls
  CLS
end

#display(line, column = 0) ⇒ Object

Like move but returns to original positon after yielding the block.



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

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.



276
277
278
# File 'lib/ansi/code.rb', line 276

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

#left(spaces = 1) ⇒ Object

Move cursor left a specificed number of spaces.



281
282
283
# File 'lib/ansi/code.rb', line 281

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

#move(line, column = 0) ⇒ Object

Move cursor to line and column.



266
267
268
# File 'lib/ansi/code.rb', line 266

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

#resetObject

Reset code.



216
217
218
# File 'lib/ansi/code.rb', line 216

def reset
  RESET
end

#restoreObject

Restore saved cursor positon.



226
227
228
# File 'lib/ansi/code.rb', line 226

def restore
  RESTORE
end

#right(spaces = 1) ⇒ Object

Move cursor right a specificed number of spaces.



286
287
288
# File 'lib/ansi/code.rb', line 286

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

#saveObject

Save current cursor positon.



221
222
223
# File 'lib/ansi/code.rb', line 221

def save
  SAVE
end

#style(*codes) ⇒ Object Also known as: color

Apply ansi codes to block yield.

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


299
300
301
302
303
304
305
306
# File 'lib/ansi/code.rb', line 299

def style(*codes) #:yield:
  s = ""
  codes.each do |code|
    s << "\e[#{TABLE[code]}m"
  end
  s << yield.to_s
  s << CLEAR
end

#unansi(string) ⇒ Object

Remove ansi codes from string. This method is like unstyle, but takes a string rather than a block.



352
353
354
# File 'lib/ansi/code.rb', line 352

def unansi(string)
  string.gsub(PATTERN, '')
end

#uncolered(string = nil) ⇒ Object

DEPRECATE: This old term will be deprecated.



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

def uncolered(string=nil)
  warn "ansi: use #uncolor or #unansi for future version"
  if block_given?
    yield.gsub(PATTERN, '')
  elsif string
    string.gsub(PATTERN, '')
  else
    ''
  end
end

#unstyleObject Also known as: uncolor

:yield:



312
313
314
315
316
317
318
# File 'lib/ansi/code.rb', line 312

def unstyle #:yield:
  if block_given?
    yield.gsub(PATTERN, '')
  else
    ''
  end
end

#up(spaces = 1) ⇒ Object

Move cursor up a specificed number of spaces.



271
272
273
# File 'lib/ansi/code.rb', line 271

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