class Mutter::Mutterer

Public Class Methods

color() click to toggle source
# File lib/mutter/mutterer.rb, line 202
def self.color
  @color
end
color=(bool) click to toggle source
# File lib/mutter/mutterer.rb, line 206
def self.color= bool
  @color = bool
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
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
stream() click to toggle source

Output stream (defaults to STDOUT)

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

Public Instance Methods

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

Add and remove styles from the active styles

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

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
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
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(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
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)}/x) { stylize $1, style }
    else
      str.gsub(/(#{Regexp.escape(glyph)}+)(.*?)\1/) { stylize $2, style }
    end
  end
end
print(msg, *styles)
Alias for: say
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
def process msg, *styles
  stylize(parse(msg), @active + styles).gsub(/\e(\d+)\e/, "\e[\\1m")
end
Also aliased as: []
reset(opt = :all)
Alias for: clear
say(msg, *styles) click to toggle source

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
Also aliased as: print
styles() click to toggle source
# File lib/mutter/mutterer.rb, line 42
def styles
  @defaults.merge @styles
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
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
table(*args, &blk) click to toggle source

Create a table

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

Remove all tags from string

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

Write to the out stream, and flush it

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