Skip to content

Commit

Permalink
Avoid double outputs clear on file changes
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaudgg committed Aug 14, 2012
1 parent 560515a commit 3c6f508
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 6 deletions.
6 changes: 3 additions & 3 deletions lib/guard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def setup(options = {})
@runner = Runner.new
@allow_stop = Listen::Turnstile.new

UI.clear
UI.clear(force: true)
deprecated_options_warning

setup_groups
Expand Down Expand Up @@ -180,7 +180,7 @@ def stop
#
def reload(scopes)
within_preserved_state do
UI.clear
UI.clear(force: true)
UI.action_with_scopes('Reload', scopes)
Dsl.reevaluate_guardfile if scopes.empty?
runner.run(:reload, scopes)
Expand All @@ -193,7 +193,7 @@ def reload(scopes)
#
def run_all(scopes)
within_preserved_state do
UI.clear
UI.clear(force: true)
UI.action_with_scopes('Run', scopes)
runner.run(:run_all, scopes)
end
Expand Down
1 change: 1 addition & 0 deletions lib/guard/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def run(task, scopes = {})
# @param [Array<String>] removed the removed paths.
#
def run_on_changes(modified, added, removed)
UI.clearable
scoped_guards do |guard|
modified_paths = Watcher.match_files(guard, modified)
added_paths = Watcher.match_files(guard, added)
Expand Down
15 changes: 12 additions & 3 deletions lib/guard/ui.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,19 @@ def reset_line
STDERR.print(color_enabled? ? "\r\e[0m" : "\r\n")
end

# Clear the output.
# Clear the output if clearable.
#
def clear
system('clear;') if ::Guard.options[:clear]
def clear(options = {})
if ::Guard.options[:clear] && (@clearable || options[:force])
@clearable = false
system('clear;')
end
end

# Allow the screen to be cleared again.
#
def clearable
@clearable = true
end

# Show a scoped action message.
Expand Down
5 changes: 5 additions & 0 deletions spec/guard/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ class ::Guard::Bar2 < ::Guard::Guard; end
watcher_module.stub(:match_files) { [] }
}

it "always calls UI.clearable" do
Guard::UI.should_receive(:clearable)
subject.run_on_changes(*changes)
end

context 'when clearable' do
before { subject.stub(:clearable?) { true } }

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

describe Guard::UI do

describe "clear" do
context "Guard.options[:clear] is true" do
before { ::Guard.stub(:options) { { clear: true } } }

it "clears the outputs if clearable" do
Guard::UI.clearable
Guard::UI.should_receive(:system).with('clear;')
Guard::UI.clear
end

it "doesn't clear the output if already cleared" do
Guard::UI.stub(:system)
Guard::UI.clear
Guard::UI.should_not_receive(:system).with('clear;')
Guard::UI.clear
end

it "clears the outputs if forced" do
Guard::UI.stub(:system)
Guard::UI.clear
Guard::UI.should_receive(:system).with('clear;')
Guard::UI.clear(force: true)
end
end

context "Guard.options[:clear] is false" do
before { ::Guard.stub(:options) { { clear: false } } }

it "doesn't clear the output" do
Guard::UI.should_not_receive(:system).with('clear;')
Guard::UI.clear
end
end
end

end

0 comments on commit 3c6f508

Please sign in to comment.