Skip to content

Commit 0399bcf

Browse files
committed
Merge pull request #447 from rzane/shell-indent
Add indent method to Thor::Shell::Basic
2 parents 29677b1 + 889d5b6 commit 0399bcf

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

lib/thor/shell/basic.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ def padding=(value)
3434
@padding = [0, value].max
3535
end
3636

37+
# Sets the output padding while executing a block and resets it.
38+
#
39+
def indent(count = 1, &block)
40+
orig_padding = padding
41+
self.padding = padding + count
42+
yield
43+
self.padding = orig_padding
44+
end
45+
3746
# Asks something to the user and receives a response.
3847
#
3948
# If asked to limit the correct responses, you can pass in an

spec/shell/basic_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,35 @@ def shell
1616
end
1717
end
1818

19+
describe "#indent" do
20+
it "sets the padding temporarily" do
21+
shell.indent { expect(shell.padding).to eq(1) }
22+
expect(shell.padding).to eq(0)
23+
end
24+
25+
it "derives padding from original value" do
26+
shell.padding = 6
27+
shell.indent { expect(shell.padding).to eq(7) }
28+
end
29+
30+
it "accepts custom indentation amounts" do
31+
shell.indent(6) {
32+
expect(shell.padding).to eq(6)
33+
}
34+
end
35+
36+
it "increases the padding when nested" do
37+
shell.indent {
38+
expect(shell.padding).to eq(1)
39+
40+
shell.indent {
41+
expect(shell.padding).to eq(2)
42+
}
43+
}
44+
expect(shell.padding).to eq(0)
45+
end
46+
end
47+
1948
describe "#ask" do
2049
it "prints a message to the user and gets the response" do
2150
expect(Thor::LineEditor).to receive(:readline).with("Should I overwrite it? ", {}).and_return("Sure")

0 commit comments

Comments
 (0)