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

Allow table rotation and resizing to be disabled #36

Open
wants to merge 2 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: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,8 @@ Rendering of **TTY-Table** includes numerous customization options:
* `:indent` - indentation applied to rendered table, by default 0
* `:multiline` - when `true` will wrap text at new line or column width, when `false` will escape special characters
* `:padding` - array of integers to set table fields padding. Defaults to `[0,0,0,0]`.
* `:resize` - when `true` will expand/shrink table column sizes to match the terminal width, otherwise when `false` will rotate table vertically. Defaults to `false`.
* `:resize` - when `true` will expand/shrink table column sizes to match the terminal width, when `false` the behaviour is determined by `rotate`. Defaults to `false`.
* `:rotate`: - when `true` will rotate the table vertically if `resize: false` causes the width to exceed the terminal size. When `false` the line wrapping is determined by the terminal. Defaults to `true`.
* `:width` - constrains the table total width. Defaults to value automatically calculated based on the content and terminal size.

The `render` method can accept as a second argument the above options either as hash value:
Expand Down
18 changes: 13 additions & 5 deletions lib/tty/table/column_constraint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,35 @@ def natural_width
# @api public
def enforce
assert_minimum_width
padding = renderer.padding

if natural_width <= renderer.width
if renderer.resize
expand_column_widths
else
renderer.column_widths.map do |width|
padding.left + width + padding.right
end
natural_column_widths
end
else
if renderer.resize
shrink
else
elsif renderer.rotate
rotate
else
natural_column_widths
end
end
end

private

# Determine the column widths without expanding or shrinking
#
# @api private
def natural_column_widths
renderer.column_widths.map do |width|
renderer.padding.left + width + renderer.padding.right
end
end

# Rotate table to vertical orientation and print information to stdout
#
# @api private
Expand Down
8 changes: 8 additions & 0 deletions lib/tty/table/renderer/basic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ class Basic
# @api public
attr_accessor :resize

# The table automatic rotation behaviour. If true and resize is false
# the table will be rotated to vertical. Has no effect if resize is
# true. By default it's true.
#
# @api public
attr_accessor :rotate

# The table padding settings
#
# @return [TTY::Table::Padder]
Expand Down Expand Up @@ -124,6 +131,7 @@ def initialize(table, options = {})
@border_class = options.fetch(:border_class) { Border::Null }
@indent = options.fetch(:indent) { 0 }
@resize = options.fetch(:resize) { false }
@rotate = options.fetch(:rotate) { true }
@padding = Strings::Padder.parse(options[:padding])
end

Expand Down
17 changes: 14 additions & 3 deletions spec/unit/column_constraint/enforce_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
let(:renderer) { TTY::Table::Renderer::Basic.new(table, options) }

context 'with resize' do
let(:options) { { width: 8, resize: true } }
let(:options) { { width: 8, resize: true, rotate: true } }

it 'calls shrink' do
allow(columns).to receive(:shrink)
Expand All @@ -42,8 +42,8 @@
end
end

context 'without resize' do
let(:options) { { width: 8, resize: false }}
context 'with rotate and without resize' do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

let(:options) { { width: 8, resize: false, rotate: true } }

it 'changes table orientation to vertical' do
allow(Kernel).to receive(:warn)
Expand All @@ -54,6 +54,17 @@
expect(table.orientation.name).to eql(:vertical)
end
end

context 'without resize nor rotate' do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

let(:options) { { width: 8, resize: false, rotate: false } }

it 'does not alter the column width nor table orientation' do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

expect(columns).not_to receive(:shrink)
column_widths = columns.enforce
expect(column_widths).to eql([2,2,2,2])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/SpaceAfterComma: Space missing after comma.

expect(table.orientation.name).to eql(:horizontal)
end
end
end

context 'with table less than allowed width' do
Expand Down