# File lib/mutter/mutterer.rb, line 202
202: def self.color
203: @color
204: end
# File lib/mutter/mutterer.rb, line 206
206: def self.color= bool
207: @color = bool
208: end
Initialize the styles, and load the defaults from styles.yml
@active: currently active styles, which apply to the whole string @styles: contains all the user + default styles
# File lib/mutter/mutterer.rb, line 11
11: def initialize obj = {}
12: self.reset
13: @defaults = load 'default'
14:
15: case obj
16: when Hash # A style definition: expand quick-styles and merge with @styles
17: @styles = obj.inject({}) do |h, (k, v)|
18: h.merge k =>
19: (v.is_a?(Hash) ? v : { :match => v, :style => [k].flatten })
20: end
21: when Array # An array of styles to be activated
22: @active = obj
23: when Symbol # A single style to be activated
24: self << obj
25: when String # The path of a yaml style-sheet
26: @styles = load obj
27: else raise ArgumentError
28: end
29:
30: #
31: # Create an instance method for each style
32: #
33: self.styles.keys.each do |style|
34: (class << self; self end).class_eval do
35: define_method style do |msg|
36: say msg, style
37: end
38: end if style.is_a? Symbol
39: end
40: end
# File lib/mutter/mutterer.rb, line 132
132: def + style
133: dup.tap {|m| m << style }
134: end
# File lib/mutter/mutterer.rb, line 136
136: def - style
137: dup.tap {|m| m >> style }
138: end
Add and remove styles from the active styles
# File lib/mutter/mutterer.rb, line 124
124: def << style
125: @active << style
126: end
# File lib/mutter/mutterer.rb, line 128
128: def >> style
129: @active.delete style
130: end
# File lib/mutter/mutterer.rb, line 46
46: def clear opt = :all
47: case opt
48: when :user then @styles = {}
49: when :styles then @styles, @defaults = {}, {}
50: when :active then @active = []
51: when :all then @active, @styles, @defaults = [], {}, {}
52: when :default,
53: :defaults then @defaults = {}
54: else raise ArgumentError, "[:user, :default, :active, :all] only"
55: end
56: self
57: end
# File lib/mutter/mutterer.rb, line 73
73: def color?
74: (ENV['TERM'].include?('color') && self.class.stream.tty?) || self.class.color
75: end
Escape a string, for later replacement
# File lib/mutter/mutterer.rb, line 186
186: def esc str, open, close
187: "\e#{open}\e" + str + "\e#{close}\e"
188: end
Loads styles from a YAML style-sheet,
and converts the keys to symbols
# File lib/mutter/mutterer.rb, line 64
64: def load styles
65: styles += '.yml' unless styles =~ /\.ya?ml$/
66: styles = File.join(File.dirname(__FILE__), "styles", styles) unless File.exist? styles
67: YAML.load_file(styles).inject({}) do |h, (key, value)|
68: value = { :match => value['match'], :style => value['style'] }
69: h.merge key.to_sym => value
70: end
71: end
Parse a string to ANSI codes
if the glyph is a pair, we match [0] as the start and [1] as the end marker. the matches are sent to +stylize+
# File lib/mutter/mutterer.rb, line 147
147: def parse string
148: self.styles.inject(string) do |str, (name, options)|
149: glyph, style = options[:match], options[:style]
150: if glyph.is_a? Array
151: str.gsub(/#{Regexp.escape(glyph.first)}(.*?)
152: #{Regexp.escape(glyph.last)}/) { stylize $1, style }
153: else
154: str.gsub(/(#{Regexp.escape(glyph)}+)(.*?)\11//) { stylize $2, style }
155: end
156: end
157: end
Parse the message, but also apply a style on the whole thing
# File lib/mutter/mutterer.rb, line 99
99: def process msg, *styles
100: stylize(parse(msg), @active + styles).gsub(/\e(\d+)\e/, "\e[\\1m")
101: end
Output to @stream
# File lib/mutter/mutterer.rb, line 80
80: def say msg, *styles
81: self.write((color?? process(msg, *styles) : unstyle(msg)) + "\n") ; nil
82: end
# File lib/mutter/mutterer.rb, line 42
42: def styles
43: @defaults.merge @styles
44: end
Apply styles to a string
if the style is a default ANSI style, we add the start and end sequence to the string. if the style is a custom style, we recurse, sending the list of ANSI styles contained in the custom style. TODO: use ';' delimited codes instead of multiple \e sequences
# File lib/mutter/mutterer.rb, line 170
170: def stylize string, styles = []
171: [styles].flatten.inject(string) do |str, style|
172: style = style.to_sym
173: if ANSI[:transforms].include? style
174: esc str, *ANSI[:transforms][style]
175: elsif ANSI[:colors].include? style
176: esc str, ANSI[:colors][style], ANSI[:colors][:reset]
177: else
178: stylize(str, @styles[style][:style])
179: end
180: end
181: end
Create a table
# File lib/mutter/mutterer.rb, line 117
117: def table *args, &blk
118: Table.new(*args, &blk)
119: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.