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 indent bug for tables without headers (issue piotrmurach/tty-table/issues/45) #44

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Change log

### Fixed
* Fix indentation of tables without headers by Roger Marlow(@rogermarlow)

## [v0.12.0] - 2020-09-20

### Changed
Expand Down
2 changes: 1 addition & 1 deletion lib/tty/table/indentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def indent(part, indentation)
#
# @api public
def insert_indentation(line, indentation)
line ? " " * indentation + line.to_s : ""
line ? " " * indentation + line.to_s : line
end
module_function :insert_indentation
end # Indentation
Expand Down
5 changes: 3 additions & 2 deletions lib/tty/table/renderer/basic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ def render
operations.add(*op)
end
operations.apply_to(table, *select_operations)

render_data.compact.join("\n")
end

Expand Down Expand Up @@ -267,7 +266,9 @@ def render_data
# @api private
def render_header(row, data_border)
top_line = data_border.top_line
return top_line unless row.is_a?(TTY::Table::Header)
unless row.is_a?(TTY::Table::Header)
return Indentation.indent(top_line, @indent)
end
header = [top_line, data_border.row_line(row)]
if !border.separator || border.separator?(0)
header << data_border.middle_line
Expand Down
14 changes: 14 additions & 0 deletions spec/unit/renderer/ascii/indentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,17 @@
end
end
end

RSpec.describe TTY::Table::Renderer::ASCII, "indentation without headers" do
it "does not regress on issue 45" do
table = TTY::Table.new
table << %w(a1 a2 a3)
table << %w(b1 b2 b3)
expect(table.render(:ascii, indent: 3)).to eql([
" +--+--+--+",
" |a1|a2|a3|",
" |b1|b2|b3|",
" +--+--+--+"
].join("\n"))
end
end