Skip to content
This repository was archived by the owner on Apr 3, 2026. It is now read-only.

Tutorial with ANSI

trans edited this page May 9, 2011 · 3 revisions

Here is an example of using ANSI via Bezel.

  require 'bezel'

  ANSI_VERSION = '1.2.6'

  class ColorfulString
    include lib('ansi', ANSI_VERSION)

    COLORS = [:red, :yellow, :green, :blue, :magenta]

    def initialize(string)
      @string = string
      reset_colors
    end

    def to_s
      s = ""
      @string.split(//).each do |c|
        s << ANSI::Code.send(next_color) + c;
      end
      s << ANSI::Code::CLEAR
      reset_colors
      return s
    end

    def next_color
      color = @colors.shift
      @colors << color
      color
    end

    def reset_colors
      @colors = COLORS.dup
    end
  end

ANSI was made Bezel-ready by adding lib/ansi.bezel.

    module ANSI; end

    import 'ansi/code'
    import 'ansi/bbcode'
    import 'ansi/columns'
    import 'ansi/diff'
    import 'ansi/logger'
    import 'ansi/mixin'
    import 'ansi/progressbar'
    import 'ansi/string'
    import 'ansi/table'
    import 'ansi/terminal'

Clone this wiki locally