Module: Clio::ANSICode

Extended by:
ANSICode
Included in:
ANSICode
Defined in:
lib/clio/ansicode.rb

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

Instance Method Summary collapse

Class Method Details

.colorsObject



313
314
315
# File 'lib/clio/ansicode.rb', line 313

def colors
  @@colors.map { |c| c[0] }
end

.define_ansicolor_method(name, code) ⇒ Object

Define color codes.



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/clio/ansicode.rb', line 243

def self.define_ansicolor_method(name,code)
  class_eval <<-HERE
    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
  HERE
end

Instance Method Details

#clear_lineObject Also known as: clr

Clear to the end of the current line.



181
182
183
# File 'lib/clio/ansicode.rb', line 181

def clear_line
  "\e[K"
end

#clear_screenObject Also known as: cls

Clear the screen and move cursor to home.



174
175
176
# File 'lib/clio/ansicode.rb', line 174

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.



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/clio/ansicode.rb', line 225

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.



206
207
208
# File 'lib/clio/ansicode.rb', line 206

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

#left(spaces = 1) ⇒ Object

Move cursor left a specificed number of spaces.



212
213
214
# File 'lib/clio/ansicode.rb', line 212

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

#move(line, column = 0) ⇒ Object

Move curose to line and column.



194
195
196
# File 'lib/clio/ansicode.rb', line 194

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

#restoreObject

Restore saved cursor positon.



168
169
170
# File 'lib/clio/ansicode.rb', line 168

def restore
  "\e[u"
end

#right(spaces = 1) ⇒ Object

Move cursor right a specificed number of spaces.



218
219
220
# File 'lib/clio/ansicode.rb', line 218

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

#saveObject

Save current cursor positon.



162
163
164
# File 'lib/clio/ansicode.rb', line 162

def save
  "\e[s"
end

#uncolored(string = nil) ⇒ Object



299
300
301
302
303
304
305
306
307
308
309
# File 'lib/clio/ansicode.rb', line 299

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.



200
201
202
# File 'lib/clio/ansicode.rb', line 200

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