Skip to content

Commit 09ae066

Browse files
Merge pull request #362 from britto/thread-safe-say
Make say and say_status thread safe.
2 parents a69f7fc + c06a272 commit 09ae066

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

lib/thor/shell/basic.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,13 @@ def ask(statement, *args)
7171
#
7272
def say(message="", color=nil, force_new_line=(message.to_s !~ /( |\t)\Z/))
7373
message = message.to_s
74-
7574
message = set_color(message, *color) if color && can_display_colors?
7675

77-
spaces = " " * padding
76+
buffer = " " * padding
77+
buffer << message
78+
buffer << "\n" if force_new_line && !message.end_with?("\n")
7879

79-
if force_new_line
80-
stdout.puts(spaces + message)
81-
else
82-
stdout.print(spaces + message)
83-
end
80+
stdout.print(buffer)
8481
stdout.flush
8582
end
8683

@@ -97,7 +94,10 @@ def say_status(status, message, log_status=true)
9794
status = status.to_s.rjust(12)
9895
status = set_color status, color, true if color
9996

100-
stdout.puts "#{status}#{spaces}#{message}"
97+
buffer = "#{status}#{spaces}#{message}"
98+
buffer << "\n" unless buffer.end_with?("\n")
99+
100+
stdout.print(buffer)
101101
stdout.flush
102102
end
103103

spec/shell/basic_spec.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def shell
4444

4545
it "prints a message to the user with the available options and reasks the question after an incorrect repsonse" do
4646
expect($stdout).to receive(:print).with('What\'s your favorite Neopolitan flavor? [strawberry, chocolate, vanilla] ').twice
47-
expect($stdout).to receive(:puts).with('Your response must be one of: [strawberry, chocolate, vanilla]. Please try again.')
47+
expect($stdout).to receive(:print).with("Your response must be one of: [strawberry, chocolate, vanilla]. Please try again.\n")
4848
expect($stdin).to receive(:gets).and_return('moose tracks', 'chocolate')
4949
expect(shell.ask("What's your favorite Neopolitan flavor?", :limited_to => ["strawberry", "chocolate", "vanilla"])).to eq("chocolate")
5050
end
@@ -57,7 +57,7 @@ def shell
5757

5858
it "prints a message to the user with the available options and reasks the question after an incorrect repsonse and then returns the default" do
5959
expect($stdout).to receive(:print).with('What\'s your favorite Neopolitan flavor? [strawberry, chocolate, vanilla] (vanilla) ').twice
60-
expect($stdout).to receive(:puts).with('Your response must be one of: [strawberry, chocolate, vanilla]. Please try again.')
60+
expect($stdout).to receive(:print).with("Your response must be one of: [strawberry, chocolate, vanilla]. Please try again.\n")
6161
expect($stdin).to receive(:gets).and_return('moose tracks', '')
6262
expect(shell.ask("What's your favorite Neopolitan flavor?", :default => "vanilla", :limited_to => ["strawberry", "chocolate", "vanilla"])).to eq("vanilla")
6363
end
@@ -89,7 +89,7 @@ def shell
8989

9090
describe "#say" do
9191
it "prints a message to the user" do
92-
expect($stdout).to receive(:puts).with("Running...")
92+
expect($stdout).to receive(:print).with("Running...\n")
9393
shell.say("Running...")
9494
end
9595

@@ -99,7 +99,7 @@ def shell
9999
end
100100

101101
it "does not use a new line with whitespace+newline embedded" do
102-
expect($stdout).to receive(:puts).with("It's \nRunning...")
102+
expect($stdout).to receive(:print).with("It's \nRunning...\n")
103103
shell.say("It's \nRunning...")
104104
end
105105

@@ -111,18 +111,18 @@ def shell
111111

112112
describe "#say_status" do
113113
it "prints a message to the user with status" do
114-
expect($stdout).to receive(:puts).with(" create ~/.thor/command.thor")
114+
expect($stdout).to receive(:print).with(" create ~/.thor/command.thor\n")
115115
shell.say_status(:create, "~/.thor/command.thor")
116116
end
117117

118118
it "always uses new line" do
119-
expect($stdout).to receive(:puts).with(" create ")
119+
expect($stdout).to receive(:print).with(" create \n")
120120
shell.say_status(:create, "")
121121
end
122122

123123
it "does not print a message if base is muted" do
124124
expect(shell).to receive(:mute?).and_return(true)
125-
expect($stdout).not_to receive(:puts)
125+
expect($stdout).not_to receive(:print)
126126

127127
shell.mute do
128128
shell.say_status(:created, "~/.thor/command.thor")
@@ -133,19 +133,19 @@ def shell
133133
base = MyCounter.new [1,2]
134134
expect(base).to receive(:options).and_return(:quiet => true)
135135

136-
expect($stdout).not_to receive(:puts)
136+
expect($stdout).not_to receive(:print)
137137
shell.base = base
138138
shell.say_status(:created, "~/.thor/command.thor")
139139
end
140140

141141
it "does not print a message if log status is set to false" do
142-
expect($stdout).not_to receive(:puts)
142+
expect($stdout).not_to receive(:print)
143143
shell.say_status(:created, "~/.thor/command.thor", false)
144144
end
145145

146146
it "uses padding to set message's left margin" do
147147
shell.padding = 2
148-
expect($stdout).to receive(:puts).with(" create ~/.thor/command.thor")
148+
expect($stdout).to receive(:print).with(" create ~/.thor/command.thor\n")
149149
shell.say_status(:create, "~/.thor/command.thor")
150150
end
151151
end
@@ -293,7 +293,7 @@ def #456 Lanç...
293293

294294
it "quits if the user chooses quit" do
295295
allow($stdout).to receive(:print)
296-
expect($stdout).to receive(:puts).with('Aborting...')
296+
expect($stdout).to receive(:print).with("Aborting...\n")
297297
expect($stdin).to receive(:gets).and_return('q')
298298

299299
expect {

spec/shell/html_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ def shell
2424

2525
describe "#say_status" do
2626
it "uses color to say status" do
27-
expect($stdout).to receive(:puts).with('<span style="color: red; font-weight: bold;"> conflict</span> README')
27+
expect($stdout).to receive(:print).with("<span style=\"color: red; font-weight: bold;\"> conflict</span> README\n")
2828
shell.say_status :conflict, "README", :red
2929
end
3030
end
31-
3231
end

0 commit comments

Comments
 (0)