Module: Console::ANSICode
Overview
ANSICode
Module which makes it very easy to use ANSI codes. These are esspecially nice for beautifying shell output.
Synopsis
include Console::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 Commands
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
Constant Summary collapse
- ColoredRegexp =
/\e\[([34][0-7]|[0-9])m/- @@colors =
[ [ :clear , 0 ], [ :reset , 0 ], # synonym for :clear [ :bold , 1 ], [ :dark , 2 ], [ :italic , 3 ], # not widely implemented [ :underline , 4 ], [ :underscore , 4 ], # synonym for :underline [ :blink , 5 ], [ :rapid_blink , 6 ], # not widely implemented [ :negative , 7 ], # no reverse because of String#reverse [ :concealed , 8 ], [ :strikethrough, 9 ], # not widely implemented [ :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
- .colors ⇒ Object
-
.define_ansicolor_method(name, code) ⇒ Object
Define color codes.
Instance Method Summary collapse
-
#clear_line ⇒ Object
(also: #clr)
Clear to the end of the current line.
-
#clear_screen ⇒ Object
(also: #cls)
Clear the screen and move cursor to home.
-
#display(line, column = 0, string = nil) ⇒ Object
Like
movebut returns to original positon after yielding block or adding string argument. -
#down(spaces = 1) ⇒ Object
Move cursor down a specificed number of spaces.
-
#left(spaces = 1) ⇒ Object
Move cursor left a specificed number of spaces.
-
#move(line, column = 0) ⇒ Object
Move curose to line and column.
-
#restore ⇒ Object
Restore saved cursor positon.
-
#right(spaces = 1) ⇒ Object
Move cursor right a specificed number of spaces.
-
#save ⇒ Object
Save current cursor positon.
- #uncolored(string = nil) ⇒ Object
-
#up(spaces = 1) ⇒ Object
Move cursor up a specificed number of spaces.
Class Method Details
.colors ⇒ Object
262 263 264 |
# File 'lib/more/facets/ansicode.rb', line 262 def colors @@colors.map { |c| c[0] } end |
.define_ansicolor_method(name, code) ⇒ Object
Define color codes.
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/more/facets/ansicode.rb', line 192 def self.define_ansicolor_method(name,code) class_eval " def \#{name.to_s}(string = nil)\n result = \"\\e[\#{code}m\"\n if block_given?\n result << yield\n result << \"\\e[0m\"\n elsif string\n result << string\n result << \"\\e[0m\"\n elsif respond_to?(:to_str)\n result << self\n result << \"\\e[0m\"\n end\n return result\n end\n HERE\nend\n" |
Instance Method Details
#clear_line ⇒ Object Also known as: clr
Clear to the end of the current line.
130 131 132 |
# File 'lib/more/facets/ansicode.rb', line 130 def clear_line "\e[K" end |
#clear_screen ⇒ Object Also known as: cls
Clear the screen and move cursor to home.
123 124 125 |
# File 'lib/more/facets/ansicode.rb', line 123 def clear_screen "\e[2J" end |
#display(line, column = 0, string = nil) ⇒ Object
Like move but returns to original positon after yielding block or adding string argument.
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/more/facets/ansicode.rb', line 174 def display( line, column=0, string=nil ) #: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" elsif respond_to?(:to_str) result << self result << "\e[u" end return result end |
#down(spaces = 1) ⇒ Object
Move cursor down a specificed number of spaces.
155 156 157 |
# File 'lib/more/facets/ansicode.rb', line 155 def down( spaces=1 ) "\e[#{spaces.to_i}B" end |
#left(spaces = 1) ⇒ Object
Move cursor left a specificed number of spaces.
161 162 163 |
# File 'lib/more/facets/ansicode.rb', line 161 def left( spaces=1 ) "\e[#{spaces.to_i}D" end |
#move(line, column = 0) ⇒ Object
Move curose to line and column.
143 144 145 |
# File 'lib/more/facets/ansicode.rb', line 143 def move( line, column=0 ) "\e[#{line.to_i};#{column.to_i}H" end |
#restore ⇒ Object
Restore saved cursor positon.
117 118 119 |
# File 'lib/more/facets/ansicode.rb', line 117 def restore "\e[u" end |
#right(spaces = 1) ⇒ Object
Move cursor right a specificed number of spaces.
167 168 169 |
# File 'lib/more/facets/ansicode.rb', line 167 def right( spaces=1 ) "\e[#{spaces.to_i}C" end |
#save ⇒ Object
Save current cursor positon.
111 112 113 |
# File 'lib/more/facets/ansicode.rb', line 111 def save "\e[s" end |
#uncolored(string = nil) ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/more/facets/ansicode.rb', line 248 def uncolored(string = nil) if block_given? yield.gsub(ColoredRegexp, '') elsif string string.gsub(ColoredRegexp, '') elsif respond_to?(:to_str) gsub(ColoredRegexp, '') else '' end end |
#up(spaces = 1) ⇒ Object
Move cursor up a specificed number of spaces.
149 150 151 |
# File 'lib/more/facets/ansicode.rb', line 149 def up( spaces=1 ) "\e[#{spaces.to_i}A" end |