Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix printing tables with borders and indentation #861

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions lib/thor/shell/table_printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -66,7 +66,6 @@ def prepare(array)
end
end

@formats[0] = @formats[0].insert(0, " " * @indent)
@formats << "%s"
end

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -129,4 +132,3 @@ def as_unicode
end
end
end

14 changes: 14 additions & 0 deletions spec/shell/basic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading