Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colorized Output for Success and Errors #5

Merged
merged 2 commits into from
May 13, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/guard/coffeescript.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
module Guard
class CoffeeScript < Guard

autoload :Formatter, 'guard/coffeescript/formatter'
autoload :Inspector, 'guard/coffeescript/inspector'
autoload :Runner, 'guard/coffeescript/runner'

Expand Down
29 changes: 29 additions & 0 deletions lib/guard/coffeescript/formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Guard
class CoffeeScript
module Formatter
class << self

def info(message, options={})
::Guard::UI.info(message, options)
end

def debug(message, options={})
::Guard::UI.debug(message, options)
end

def error(message, options={})
::Guard::UI.error(::Guard::UI.send(:color, message, "\e[31m"), options)
end

def success(message, options={})
::Guard::UI.info(::Guard::UI.send(:color, message, "\e[32m"), options)
end

def notify(message, options={})
::Guard::Notifier.notify(message, options)
end

end
end
end
end
12 changes: 6 additions & 6 deletions lib/guard/coffeescript/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ def run(files, watchers, options = {})

changed_files
rescue ::CoffeeScript::EngineError => e
::Guard::UI.error "CoffeeScript engine error: " + e.message
Formatter.error("CoffeeScript engine error: " + e.message)
end

private

def notify_start(files, options)
message = options[:message] || "Compile #{ files.join(', ') }"
::Guard::UI.info message, :reset => true
Formatter.info(message, :reset => true)
end

def compile_files(files, options, watchers)
Expand All @@ -35,7 +35,7 @@ def compile_files(files, options, watchers)
rescue ::CoffeeScript::CompilationError => e
error_message = file + ': ' + e.message
errors << error_message
::Guard::UI.error(error_message)
Formatter.error(error_message)
end
end
end
Expand Down Expand Up @@ -76,11 +76,11 @@ def detect_nested_directories(watchers, files, options)

def notify_result(changed_files, errors, options = {})
if !errors.empty?
::Guard::Notifier.notify(errors.join("\n"), :title => 'CoffeeScript results', :image => :failed)
Formatter.notify(errors.join("\n"), :title => 'CoffeeScript results', :image => :failed)
elsif !options[:hide_success]
message = "Successfully generated #{ changed_files.join(', ') }"
::Guard::UI.info(message)
::Guard::Notifier.notify(message, :title => 'CoffeeScript results')
Formatter.success(message)
Formatter.notify(message, :title => 'CoffeeScript results')
end
end

Expand Down
42 changes: 42 additions & 0 deletions spec/guard/coffeescript/formatter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'spec_helper'

describe Guard::CoffeeScript::Formatter do

subject { Guard::CoffeeScript::Formatter }

describe '.info' do
it 'output Guard::UI.info' do
::Guard::UI.should_receive(:info).once.with('a.coffee', {})
subject.info('a.coffee')
end
end

describe '.debug' do
it 'output Guard::UI.debug' do
::Guard::UI.should_receive(:debug).once.with('a.coffee', {})
subject.debug('a.coffee')
end
end

describe '.error' do
it 'colorize Guard::UI.error' do
::Guard::UI.should_receive(:error).once.with("\e[31ma.coffee\e[0m", {})
subject.error('a.coffee')
end
end

describe '.success' do
it 'colorize Guard::UI.info' do
::Guard::UI.should_receive(:info).once.with("\e[32ma.coffee\e[0m", {})
subject.success('a.coffee')
end
end

describe '.notify' do
it 'output Guard::Notifier.notify' do
::Guard::Notifier.should_receive(:notify).once.with('a.coffee', {})
subject.notify('a.coffee')
end
end

end
11 changes: 5 additions & 6 deletions spec/guard/coffeescript/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
end

it 'shows a start notification' do
::Guard::UI.should_receive(:info).once.with('Compile a.coffee, b.coffee', { :reset => true })
::Guard::UI.should_receive(:info).once.with('Successfully generated ')
::Guard::CoffeeScript::Formatter.should_receive(:info).once.with('Compile a.coffee, b.coffee', { :reset => true })
::Guard::CoffeeScript::Formatter.should_receive(:success).once.with('Successfully generated ')
runner.run(['a.coffee', 'b.coffee'], [])
end

Expand Down Expand Up @@ -50,7 +50,7 @@
context 'with compilation errors' do
it 'shows the error messages' do
runner.should_receive(:compile).and_raise ::CoffeeScript::CompilationError.new("Parse error on line 2: Unexpected 'UNARY'")
::Guard::UI.should_receive(:error).once.with("a.coffee: Parse error on line 2: Unexpected 'UNARY'")
::Guard::CoffeeScript::Formatter.should_receive(:error).once.with("a.coffee: Parse error on line 2: Unexpected 'UNARY'")
Guard::Notifier.should_receive(:notify).with("a.coffee: Parse error on line 2: Unexpected 'UNARY'", :title => 'CoffeeScript results', :image => :failed)
runner.run(['a.coffee'], [watcher], { :output => 'javascripts' })
end
Expand All @@ -60,8 +60,7 @@
it 'shows a success messages' do
runner.should_receive(:compile).with('a.coffee', { :output => 'javascripts' }).and_return ["OK", true]
runner.should_receive(:notify_start).with(['a.coffee'], { :output => 'javascripts' })
::Guard::UI.should_receive(:info).with("\"^(.*)\\.coffee\" has been converted to /^(.*)\\.coffee/\n")
::Guard::UI.should_receive(:info).with('Successfully generated javascripts/a.js')
::Guard::CoffeeScript::Formatter.should_receive(:success).once.with('Successfully generated javascripts/a.js')
Guard::Notifier.should_receive(:notify).with('Successfully generated javascripts/a.js', :title => 'CoffeeScript results')
runner.run(['a.coffee'], [watcher], { :output => 'javascripts' })
end
Expand All @@ -71,7 +70,7 @@

it 'compiles the CoffeeScripts to the output and creates nested directories' do
FileUtils.should_receive(:mkdir_p).with("#{ @project_path }/javascripts/x/y")
::Guard::UI.should_not_receive(:info).with('Successfully generated javascripts/a.js')
::Guard::CoffeeScript::Formatter.should_not_receive(:success).with('Successfully generated javascripts/a.js')
Guard::Notifier.should_not_receive(:notify).with('Successfully generated javascripts/a.js', :title => 'CoffeeScript results')
runner.run(['app/coffeescripts/x/y/a.coffee'], [watcher], { :output => 'javascripts', :hide_success => true })
end
Expand Down