Skip to content

Commit

Permalink
Indent multiline messages in say_status
Browse files Browse the repository at this point in the history
This prevents multiline status messages from breaking the left margin.

As an example, before this commit:

```
     status1  single line
     status2  multiline line 1
multiline line 2
multiline line 3
     status3  multiline indent 0
  multiline indent 2
    multiline indent 4
     status4  single line
```

And after this commit:

```
     status1  single line
     status2  multiline line 1
              multiline line 2
              multiline line 3
     status3  multiline indent 0
                multiline indent 2
                  multiline indent 4
     status4  single line
```
  • Loading branch information
jonathanhefner committed Feb 21, 2020
1 parent ff37cba commit a546e93
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/thor/shell/basic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,14 @@ def say(message = "", color = nil, force_new_line = (message.to_s !~ /( |\t)\Z/)
def say_status(status, message, log_status = true)
return if quiet? || log_status == false
spaces = " " * (padding + 1)
color = log_status.is_a?(Symbol) ? log_status : :green

status = status.to_s.rjust(12)
margin = " " * status.length + spaces

color = log_status.is_a?(Symbol) ? log_status : :green
status = set_color status, color, true if color

buffer = "#{status}#{spaces}#{message}"
buffer = "#{buffer}\n" unless buffer.end_with?("\n")
message = message.to_s.chomp.gsub(/(?<!\A)^/, margin)
buffer = "#{status}#{spaces}#{message}\n"

stdout.print(buffer)
stdout.flush
Expand Down
14 changes: 14 additions & 0 deletions spec/shell/basic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,20 @@ def shell
shell.say_status(:create, "")
end

it "indents a multiline message" do
status = :foobar
lines = ["first line", "second line", "third line"]

expect($stdout).to receive(:print) do |string|
formatted_status = string[/^\s*#{status}\s*/]
indent = " " * formatted_status.length

expect(string).to eq(formatted_status + lines.join("\n#{indent}") + "\n")
end

shell.say_status(status, lines.join("\n") + "\n")
end

it "does not print a message if base is muted" do
expect(shell).to receive(:mute?).and_return(true)
expect($stdout).not_to receive(:print)
Expand Down

0 comments on commit a546e93

Please sign in to comment.