Class: Perennial::ANSIFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/perennial/core_ext/ansi_formatter.rb

Constant Summary collapse

TAGS =
{
  :bold          => 1,
  :italics       => 3,
  :underline     => 4,
  :inverse       => 7,
  :strikethrough => 9
}
COLOURS =
{
  :black         => 30,
  :red           => 31,
  :green         => 32,
  :yellow        => 33,
  :blue          => 34,
  :magenta       => 35,
  :cyan          => 36,
  :white         => 37
}
BACKGROUND_COLOURS =
{
  :bg_black      => 40,
  :bg_red        => 41,
  :bg_green      => 42,
  :bg_yellow     => 43,
  :bg_blue       => 44,
  :bg_magenta    => 45,
  :bg_cyan       => 46,
  :bg_white      => 47
}
UNDO_TAGS =
{
  :all           => 0,
  :bold          => 22,
  :italics       => 23,
  :underline     => 24,
  :inverse       => 27,
  :strikethrough => 29,
  :colour        => 39,
  :bg_colour     => 49
}
MATCH_REGEXP =
/\<f\:(.+)\>(.*)\<\/f\:\1>/
ESCAPE_TAG =
/\<\/?f\:escape\>/
ANSI_CODE =
/\033\[(?:\d+\;?)+m/
@@formatted =
true

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ ANSIFormatter

Returns a new instance of ANSIFormatter.



53
54
55
# File 'lib/perennial/core_ext/ansi_formatter.rb', line 53

def initialize(string)
  @string  = string
end

Class Method Details

.clean(text) ⇒ Object



93
94
95
96
97
# File 'lib/perennial/core_ext/ansi_formatter.rb', line 93

def clean(text)
  text = text.gsub(ESCAPE_TAG, "") while text =~ ESCAPE_TAG
  text = text.gsub(ANSI_CODE,  "") while text =~ ANSI_CODE
  text
end

.format(tag, text) ⇒ Object



71
72
73
74
# File 'lib/perennial/core_ext/ansi_formatter.rb', line 71

def format(tag, text)
  tag = tag.to_s.gsub(/[\-\:]/, "_").to_sym
  "\033[#{lookup_tag(tag)}m#{text}\033[#{lookup_end_tag(tag)}m"
end

.lookup_end_tag(tag) ⇒ Object



80
81
82
# File 'lib/perennial/core_ext/ansi_formatter.rb', line 80

def lookup_end_tag(tag)
  UNDO_TAGS[lookup_tag_type(tag)] || UNDO_TAGS[:all]
end

.lookup_tag(tag) ⇒ Object



76
77
78
# File 'lib/perennial/core_ext/ansi_formatter.rb', line 76

def lookup_tag(tag)
  TAGS[tag] || COLOURS[tag] || BACKGROUND_COLOURS[tag]
end

.lookup_tag_type(tag) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/perennial/core_ext/ansi_formatter.rb', line 84

def lookup_tag_type(tag)
  if COLOURS.has_key?(tag)
    tag = :colour
  elsif BACKGROUND_COLOURS.has_key?(tag)
    tag = :bg_colour
  end
  tag
end

.process(string) ⇒ Object



99
100
101
# File 'lib/perennial/core_ext/ansi_formatter.rb', line 99

def process(string)
  new(string).to_s
end

Instance Method Details

#to_formatted_sObject



61
62
63
# File 'lib/perennial/core_ext/ansi_formatter.rb', line 61

def to_formatted_s
  format_tags
end

#to_normal_sObject



65
66
67
# File 'lib/perennial/core_ext/ansi_formatter.rb', line 65

def to_normal_s
  remove_tags
end

#to_sObject



57
58
59
# File 'lib/perennial/core_ext/ansi_formatter.rb', line 57

def to_s
  @@formatted ? to_formatted_s : to_normal_s
end