Sha256: 88dbcc3689d98dd5f55f4d5b9ede80c5e70612b3c30daacdd7fd41d921885804

Contents?: true

Size: 1.61 KB

Versions: 11

Compression:

Stored size: 1.61 KB

Contents

require 'active_support/core_ext/array/extract_options'

module Massimo
  module UI
    extend self
    
    COLOR_CODES = {
      :black   => 30,
      :red     => 31,
      :green   => 32,
      :yellow  => 33,
      :blue    => 34,
      :magenta => 35,
      :cyan    => 36
    }.freeze
    
    # Say (print) something to the user.
    def say(message, *args)
      options = args.extract_options!
      color   = args.first
      
      growl(message) if options[:growl]
      message = (' ' * padding) + message.to_s
      message = self.color(message, color) if color

      $stdout.puts(message)
    end
    
    # Color the given message with the given color
    def color(message, color)
      "\e[#{COLOR_CODES[color.to_sym]}m#{message}\e[0m"
    end
    
    # Run the given block and cleanly report any errors
    def report_errors
      begin
        yield
        true
      rescue Exception => error
        say 'massimo had a problem', :red
        indent do
          say error.message, :magenta
          say error.backtrace.first, :magenta
        end
        growl "#{error.message}\n#{error.backtrace.first}", 'massimo problem'
        false
      end
    end
    
    # Indents the messages within the block by the given amount.
    def indent(amount = 2)
      self.padding += amount
      yield
      self.padding -= amount
    end
    
    protected
    
      def growl(message, title = 'massimo')
        Growl.notify(message, :title => title) if defined?(Growl)
      end
  
      def padding
        @padding ||= 0
      end
    
      def padding=(value)
        @padding = [ 0, value.to_i ].max
      end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
massimo-0.10.3 lib/massimo/ui.rb
massimo-0.10.2 lib/massimo/ui.rb
massimo-0.10.1 lib/massimo/ui.rb
massimo-0.10.0 lib/massimo/ui.rb
massimo-0.9.0 lib/massimo/ui.rb
massimo-0.8.5 lib/massimo/ui.rb
massimo-0.8.4 lib/massimo/ui.rb
massimo-0.8.3 lib/massimo/ui.rb
massimo-0.8.2 lib/massimo/ui.rb
massimo-0.8.1 lib/massimo/ui.rb
massimo-0.8.0 lib/massimo/ui.rb