diff --git a/lib/thor/shell/table_printer.rb b/lib/thor/shell/table_printer.rb index 53d349a0..88828954 100644 --- a/lib/thor/shell/table_printer.rb +++ b/lib/thor/shell/table_printer.rb @@ -36,7 +36,7 @@ def print(array) sentence = truncate(sentence) sentence << "|" if options[:borders] - stdout.puts sentence + stdout.puts indentation + sentence end print_border_separator if options[:borders] @@ -66,7 +66,6 @@ def prepare(array) end end - @formats[0] = @formats[0].insert(0, " " * @indent) @formats << "%s" end @@ -95,10 +94,10 @@ def format_cell(column, row_size, index) end def print_border_separator - top = @maximas.map do |maxima| - " " * @indent + "+" + "-" * (maxima + 2 * @padding) + separator = @maximas.map do |maxima| + "+" + "-" * (maxima + 2 * @padding) end - stdout.puts top.join + "+" + stdout.puts indentation + separator.join + "+" end def truncate(string) @@ -108,11 +107,15 @@ def truncate(string) if chars.length <= @truncate chars.join else - chars[0, @truncate - 3].join + "..." + chars[0, @truncate - 3 - @indent].join + "..." end end end + def indentation + " " * @indent + end + if "".respond_to?(:encode) def as_unicode yield @@ -129,4 +132,3 @@ def as_unicode end end end - diff --git a/spec/shell/basic_spec.rb b/spec/shell/basic_spec.rb index ee3889bf..37fae04a 100644 --- a/spec/shell/basic_spec.rb +++ b/spec/shell/basic_spec.rb @@ -468,6 +468,20 @@ def #456 Lanç... | Name | Number | Color | | Erik | 1 | green | +------+--------+-------+ +TABLE + end + + it "prints a table with borders and indentation" do + table = [ + ["Name", "Number", "Color"], # rubocop: disable Style/WordArray + ["Erik", 1, "green"] + ] + content = capture(:stdout) { shell.print_table(table, borders: true, indent: 2) } + expect(content).to eq(<<-TABLE) + +------+--------+-------+ + | Name | Number | Color | + | Erik | 1 | green | + +------+--------+-------+ TABLE end end