Module: ANSI

Defined in:
lib/ansi.rb

Overview

based on en.wikipedia.org/wiki/ANSI_escape_code Written by Steven Soroka

Constant Summary collapse

COLORS =
{:black => 0, :red => 1, :green => 2, :yellow => 3, :blue => 4, :magenta => 5, :purple => 5,
:cyan => 6, :light_blue => 6, :lt_blue => 6, :white => 7}
COLOR_OPTIONS =

Some of these are not widely supported, like italics, underline, blink_fast, etc.

{:normal => 0, :bold => 1, :faint => 2, :italics => 3, :underline => 4, :blink => 5,
:blink_slow => 5, :blink_fast => 6, :double_underline => 21, :no_underline => 24, :no_blink => 25}

Class Method Summary collapse

Class Method Details

.back(count = 79) ⇒ Object Also known as: left



12
13
14
# File 'lib/ansi.rb', line 12

def back(count = 79)
  esc_code("#{count}D")
end

.bg_color(color, options = {}, &block) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/ansi.rb', line 39

def bg_color(color, options = {}, &block)
  r = set_color(color, options.merge(:base => options[:bright] ? 100 : 40))
  if block_given?
    r += yield
    r += no_color
  end
  r
end

.clear_line(left_right_or_all = :right) ⇒ Object Also known as: clear



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

def clear_line(left_right_or_all = :right)
  opt = {:left => 1, :right => 0, :line => 2, :all => 2}[left_right_or_all]
  esc_code("#{opt}K")
end

.clear_screen(top_or_bottom_or_all = :all) ⇒ Object



76
77
78
79
# File 'lib/ansi.rb', line 76

def clear_screen(top_or_bottom_or_all = :all)
  opt = {:top => 1, :bottom => 0, :all => 2}[top_or_bottom_or_all]
  esc_code("#{opt}J")
end

.color(color, options = {}, &block) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/ansi.rb', line 30

def color(color, options = {}, &block)
  r = set_color(color, options.merge(:base => options[:bright] ? 90 : 30))
  if block_given?
    r += yield
    r += no_color
  end
  r
end

.down(count = 1) ⇒ Object



26
27
28
# File 'lib/ansi.rb', line 26

def down(count = 1)
  esc_code("#{count}B")
end

.forward(count = 1) ⇒ Object Also known as: right



17
18
19
# File 'lib/ansi.rb', line 17

def forward(count = 1)
  esc_code("#{count}C")
end

.hide_cursor(&blk) ⇒ Object



58
59
60
61
62
# File 'lib/ansi.rb', line 58

def hide_cursor(&blk)
  out = esc_code("?25l")
  out << (yield + show_cursor) if block_given?
  out
end

.no_colorObject



48
49
50
# File 'lib/ansi.rb', line 48

def no_color
  esc_code('m')
end

.restore_positionObject



72
73
74
# File 'lib/ansi.rb', line 72

def restore_position
  esc_code("u")
end

.save_positionObject



68
69
70
# File 'lib/ansi.rb', line 68

def save_position
  esc_code("s")
end

.show_cursorObject



64
65
66
# File 'lib/ansi.rb', line 64

def show_cursor
  esc_code("?25h")
end

.up(count = 1) ⇒ Object



22
23
24
# File 'lib/ansi.rb', line 22

def up(count = 1)
  esc_code("#{count}A")
end