Module: ANSICode

Module | ANSICode
In: | lib/more/facets/ansicode.rb

# 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

### Methods

clear_line clear_screen clr cls colors define_ansicolor_method display down left move restore right save uncolored up

### Constants

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

### Public Class methods

define_ansicolor_method(name,code)

Define color codes.

[Source]

    # File lib/more/facets/ansicode.rb, line 231
      def self.define_ansicolor_method(name,code)
        class_eval "def \#{name.to_s}(string = nil)\nresult = \"\\e[\#{code}m\"\nif block_given?\nresult << yield\nresult << \"\\e[0m\"\nelsif string\nresult << string\nresult << \"\\e[0m\"\nelsif respond_to?(:to_str)\nresult << self\nresult << \"\\e[0m\"\nend\nreturn result\nend\n"
      end

### Public Instance methods

clear_line()

Clear to the end of the current line.

[Source]

    # File lib/more/facets/ansicode.rb, line 169
      def clear_line
        "\e[K"
      end

clear_screen()

Clear the screen and move cursor to home.

[Source]

    # File lib/more/facets/ansicode.rb, line 162
      def clear_screen
        "\e[2J"
      end

clr()

Alias for clear_line

cls()

Alias for clear_screen

colors()

[Source]

    # File lib/more/facets/ansicode.rb, line 302
      def colors
        @@colors.map { |c| c[0] }
      end

display( line, column=0, string=nil ) {|| ...}

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

[Source]

    # File lib/more/facets/ansicode.rb, line 213
      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 )

Move cursor down a specificed number of spaces.

[Source]

    # File lib/more/facets/ansicode.rb, line 194
      def down( spaces=1 )
        "\e[#{spaces.to_i}B"
      end

left( spaces=1 )

Move cursor left a specificed number of spaces.

[Source]

    # File lib/more/facets/ansicode.rb, line 200
      def left( spaces=1 )
        "\e[#{spaces.to_i}D"
      end

move( line, column=0 )

Move curose to line and column.

[Source]

    # File lib/more/facets/ansicode.rb, line 182
      def move( line, column=0 )
        "\e[#{line.to_i};#{column.to_i}H"
      end

restore()

Restore saved cursor positon.

[Source]

    # File lib/more/facets/ansicode.rb, line 156
      def restore
        "\e[u"
      end

right( spaces=1 )

Move cursor right a specificed number of spaces.

[Source]

    # File lib/more/facets/ansicode.rb, line 206
      def right( spaces=1 )
        "\e[#{spaces.to_i}C"
      end

save()

Save current cursor positon.

[Source]

    # File lib/more/facets/ansicode.rb, line 150
      def save
        "\e[s"
      end

uncolored(string = nil) {|.gsub(ColoredRegexp, '')| ...}

[Source]

    # File lib/more/facets/ansicode.rb, line 288
      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 )

Move cursor up a specificed number of spaces.

[Source]

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

[Validate]