# File lib/mutter/mutterer.rb, line 206 def self.color= bool @color = bool 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 def initialize obj = {} self.reset @defaults = load 'default' case obj when Hash # A style definition: expand quick-styles and merge with @styles @styles = obj.inject({}) do |h, (k, v)| h.merge k => (v.is_a?(Hash) ? v : { :match => v, :style => [k].flatten }) end when Array # An array of styles to be activated @active = obj when Symbol # A single style to be activated self << obj when String # The path of a yaml style-sheet @styles = load obj else raise ArgumentError end # # Create an instance method for each style # self.styles.keys.each do |style| (class << self; self end).class_eval do define_method style do |msg| say msg, style end end if style.is_a? Symbol end end
# File lib/mutter/mutterer.rb, line 132 def + style dup.tap {|m| m << style } end
# File lib/mutter/mutterer.rb, line 136 def - style dup.tap {|m| m >> style } end
Add and remove styles from the active styles
# File lib/mutter/mutterer.rb, line 124 def << style @active << style end
# File lib/mutter/mutterer.rb, line 128 def >> style @active.delete style end
# File lib/mutter/mutterer.rb, line 46 def clear opt = :all case opt when :user then @styles = {} when :styles then @styles, @defaults = {}, {} when :active then @active = [] when :all then @active, @styles, @defaults = [], {}, {} when :default, :defaults then @defaults = {} else raise ArgumentError, "[:user, :default, :active, :all] only" end self end
# File lib/mutter/mutterer.rb, line 73 def color? (ENV['TERM'].include?('color') && self.class.stream.tty?) || self.class.color end
Escape a string, for later replacement
# File lib/mutter/mutterer.rb, line 186 def esc str, open, close "\e#{open}\e" + str + "\e#{close}\e" end
Loads styles from a YAML style-sheet,
and converts the keys to symbols
# File lib/mutter/mutterer.rb, line 64 def load styles styles += '.yml' unless styles =~ /\.ya?ml$/ styles = File.join(File.dirname(__FILE__), "styles", styles) unless File.exist? styles YAML.load_file(styles).inject({}) do |h, (key, value)| value = { :match => value['match'], :style => value['style'] } h.merge key.to_sym => value end 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 def parse string self.styles.inject(string) do |str, (name, options)| glyph, style = options[:match], options[:style] if glyph.is_a? Array str.gsub(/#{Regexp.escape(glyph.first)}(.*?) #{Regexp.escape(glyph.last)}/) { stylize $1, style } else str.gsub(/(#{Regexp.escape(glyph)}+)(.*?)\11//) { stylize $2, style } end end end
Parse the message, but also apply a style on the whole thing
# File lib/mutter/mutterer.rb, line 99 def process msg, *styles stylize(parse(msg), @active + styles).gsub(/\e(\d+)\e/, "\e[\\1m") end
Output to @stream
# File lib/mutter/mutterer.rb, line 80 def say msg, *styles self.write((color?? process(msg, *styles) : unstyle(msg)) + "\n") ; nil end
# File lib/mutter/mutterer.rb, line 42 def styles @defaults.merge @styles 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 def stylize string, styles = [] [styles].flatten.inject(string) do |str, style| style = style.to_sym if ANSI[:transforms].include? style esc str, *ANSI[:transforms][style] elsif ANSI[:colors].include? style esc str, ANSI[:colors][style], ANSI[:colors][:reset] else stylize(str, @styles[style][:style]) end end end
Create a table
# File lib/mutter/mutterer.rb, line 117 def table *args, &blk Table.new(*args, &blk) end
Generated with the Darkfish Rdoc Generator 2.