Skip to content

Commit ae18824

Browse files
authored
Merge pull request #750 from postmodern/say_error
Added Shell::Basic#say_error
2 parents 2d25975 + b9350b0 commit ae18824

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

lib/thor/shell.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def shell
2121
end
2222

2323
module Shell
24-
SHELL_DELEGATED_METHODS = [:ask, :error, :set_color, :yes?, :no?, :say, :say_status, :print_in_columns, :print_table, :print_wrapped, :file_collision, :terminal_width]
24+
SHELL_DELEGATED_METHODS = [:ask, :error, :set_color, :yes?, :no?, :say, :say_error, :say_status, :print_in_columns, :print_table, :print_wrapped, :file_collision, :terminal_width]
2525
attr_writer :shell
2626

2727
autoload :Basic, File.expand_path("shell/basic", __dir__)

lib/thor/shell/basic.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,23 @@ def say(message = "", color = nil, force_new_line = (message.to_s !~ /( |\t)\Z/)
103103
stdout.flush
104104
end
105105

106+
# Say (print) an error to the user. If the sentence ends with a whitespace
107+
# or tab character, a new line is not appended (print + flush). Otherwise
108+
# are passed straight to puts (behavior got from Highline).
109+
#
110+
# ==== Example
111+
# say_error("error: something went wrong")
112+
#
113+
def say_error(message = "", color = nil, force_new_line = (message.to_s !~ /( |\t)\Z/))
114+
return if quiet?
115+
116+
buffer = prepare_message(message, *color)
117+
buffer << "\n" if force_new_line && !message.to_s.end_with?("\n")
118+
119+
stderr.print(buffer)
120+
stderr.flush
121+
end
122+
106123
# Say a status with the given color and appends the message. Since this
107124
# method is used frequently by actions, it allows nil or false to be given
108125
# in log_status, avoiding the message from being shown. If a Symbol is

spec/shell/basic_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,48 @@ def shell
190190
end
191191
end
192192

193+
describe "#say_error" do
194+
it "prints a message to the user" do
195+
expect($stderr).to receive(:print).with("Running...\n")
196+
shell.say_error("Running...")
197+
end
198+
199+
it "prints a message to the user without new line if it ends with a whitespace" do
200+
expect($stderr).to receive(:print).with("Running... ")
201+
shell.say_error("Running... ")
202+
end
203+
204+
it "does not use a new line with whitespace+newline embedded" do
205+
expect($stderr).to receive(:print).with("It's \nRunning...\n")
206+
shell.say_error("It's \nRunning...")
207+
end
208+
209+
it "prints a message to the user without new line" do
210+
expect($stderr).to receive(:print).with("Running...")
211+
shell.say_error("Running...", nil, false)
212+
end
213+
214+
it "coerces everything to a string before printing" do
215+
expect($stderr).to receive(:print).with("this_is_not_a_string\n")
216+
shell.say_error(:this_is_not_a_string, nil, true)
217+
end
218+
219+
it "does not print a message if muted" do
220+
expect($stderr).not_to receive(:print)
221+
shell.mute do
222+
shell.say_error("Running...")
223+
end
224+
end
225+
226+
it "does not print a message if base is set to quiet" do
227+
shell.base = MyCounter.new [1, 2]
228+
expect(shell.base).to receive(:options).and_return(:quiet => true)
229+
230+
expect($stderr).not_to receive(:print)
231+
shell.say_error("Running...")
232+
end
233+
end
234+
193235
describe "#print_wrapped" do
194236
let(:message) do
195237
"Creates a back-up of the given folder by compressing it in a .tar.gz\n"\

0 commit comments

Comments
 (0)