Skip to content

Commit ec2aa61

Browse files
p8rafaelfranca
authored andcommitted
Fix printing tables with borders and indentation
When printing tables with borders and indentation the resulting markup was incorrect. Instead of adding the indentation to the format of the first cell, with indent the whole line. Before: ``` +------ +-------- +-------+ | Name | Number | Color | | Erik | 1 | green | +------ +-------- +-------+ ``` After ``` +------+--------+-------+ | Name | Number | Color | | Erik | 1 | green | +------+--------+-------+ ```
1 parent a4d99cf commit ec2aa61

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

lib/thor/shell/table_printer.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def print(array)
3636

3737
sentence = truncate(sentence)
3838
sentence << "|" if options[:borders]
39-
stdout.puts sentence
39+
stdout.puts indentation + sentence
4040

4141
end
4242
print_border_separator if options[:borders]
@@ -66,7 +66,6 @@ def prepare(array)
6666
end
6767
end
6868

69-
@formats[0] = @formats[0].insert(0, " " * @indent)
7069
@formats << "%s"
7170
end
7271

@@ -95,10 +94,10 @@ def format_cell(column, row_size, index)
9594
end
9695

9796
def print_border_separator
98-
top = @maximas.map do |maxima|
99-
" " * @indent + "+" + "-" * (maxima + 2 * @padding)
97+
separator = @maximas.map do |maxima|
98+
"+" + "-" * (maxima + 2 * @padding)
10099
end
101-
stdout.puts top.join + "+"
100+
stdout.puts indentation + separator.join + "+"
102101
end
103102

104103
def truncate(string)
@@ -108,11 +107,15 @@ def truncate(string)
108107
if chars.length <= @truncate
109108
chars.join
110109
else
111-
chars[0, @truncate - 3].join + "..."
110+
chars[0, @truncate - 3 - @indent].join + "..."
112111
end
113112
end
114113
end
115114

115+
def indentation
116+
" " * @indent
117+
end
118+
116119
if "".respond_to?(:encode)
117120
def as_unicode
118121
yield
@@ -129,4 +132,3 @@ def as_unicode
129132
end
130133
end
131134
end
132-

spec/shell/basic_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,20 @@ def #456 Lanç...
468468
| Name | Number | Color |
469469
| Erik | 1 | green |
470470
+------+--------+-------+
471+
TABLE
472+
end
473+
474+
it "prints a table with borders and indentation" do
475+
table = [
476+
["Name", "Number", "Color"], # rubocop: disable Style/WordArray
477+
["Erik", 1, "green"]
478+
]
479+
content = capture(:stdout) { shell.print_table(table, borders: true, indent: 2) }
480+
expect(content).to eq(<<-TABLE)
481+
+------+--------+-------+
482+
| Name | Number | Color |
483+
| Erik | 1 | green |
484+
+------+--------+-------+
471485
TABLE
472486
end
473487
end

0 commit comments

Comments
 (0)