Parent

Files

Mutter::Mutterer

Public Class Methods

color() click to toggle source
     # File lib/mutter/mutterer.rb, line 202
202:     def self.color
203:       @color
204:     end
color=(bool) click to toggle source
     # File lib/mutter/mutterer.rb, line 206
206:     def self.color= bool
207:       @color = bool
208:     end
new(obj = {}) click to toggle source

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
stream() click to toggle source

Output stream (defaults to STDOUT)

  mostly for test purposes
     # File lib/mutter/mutterer.rb, line 194
194:     def self.stream
195:       @stream
196:     end
stream=(io) click to toggle source
     # File lib/mutter/mutterer.rb, line 198
198:     def self.stream= io
199:       @stream = io
200:     end

Public Instance Methods

+(style) click to toggle source
     # File lib/mutter/mutterer.rb, line 132
132:     def + style
133:       dup.tap {|m| m << style }
134:     end
-(style) click to toggle source
     # File lib/mutter/mutterer.rb, line 136
136:     def - style
137:       dup.tap {|m| m >> style }
138:     end
<<(style) click to toggle source

Add and remove styles from the active styles

     # File lib/mutter/mutterer.rb, line 124
124:     def << style
125:       @active << style
126:     end
>>(style) click to toggle source
     # File lib/mutter/mutterer.rb, line 128
128:     def >> style
129:       @active.delete style
130:     end
[](msg, *styles) click to toggle source
Alias for: process
clear(opt = :all) click to toggle source
    # 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
Also aliased as: reset
color?() click to toggle source
    # File lib/mutter/mutterer.rb, line 73
73:     def color?
74:       (ENV['TERM'].include?('color') && self.class.stream.tty?) || self.class.color
75:     end
esc(str, open, close) click to toggle source

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
load(styles) click to toggle source

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(string) click to toggle source

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
process(msg, *styles) click to toggle source

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
Also aliased as: []
reset(opt = :all) click to toggle source
Alias for: clear
say(msg, *styles) click to toggle source

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
Also aliased as: print
styles() click to toggle source
    # File lib/mutter/mutterer.rb, line 42
42:     def styles
43:       @defaults.merge @styles
44:     end
stylize(string, styles = []) click to toggle source

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
table(*args, &blk) click to toggle source

Create a table

     # File lib/mutter/mutterer.rb, line 117
117:     def table *args, &blk
118:       Table.new(*args, &blk)
119:     end
unstyle(msg) click to toggle source

Remove all tags from string

    # File lib/mutter/mutterer.rb, line 88
88:     def unstyle msg
89:       styles.map do |_,v|
90:         v[:match]
91:       end.flatten.inject(msg) do |m, tag|
92:         m.gsub(tag, '')
93:       end
94:     end
write(str) click to toggle source

Write to the out stream, and flush it

     # File lib/mutter/mutterer.rb, line 107
107:     def write str
108:       self.class.stream.tap do |stream|
109:         stream.write str
110:         stream.flush
111:       end ; nil
112:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.