Skip to content

Commit 6380c8f

Browse files
committed
Add Thor::Shell::Basic#indent method
1 parent 7d6694a commit 6380c8f

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,29 @@ 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 "increases the padding when nested" do
31+
shell.indent {
32+
expect(shell.padding).to eq(1)
33+
34+
shell.indent {
35+
expect(shell.padding).to eq(2)
36+
}
37+
}
38+
expect(shell.padding).to eq(0)
39+
end
40+
end
41+
1942
describe "#ask" do
2043
it "prints a message to the user and gets the response" do
2144
expect(Thor::LineEditor).to receive(:readline).with("Should I overwrite it? ", {}).and_return("Sure")

0 commit comments

Comments
 (0)