Skip to content

Commit

Permalink
Add a rotate flag to control table rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamMcCumstie committed Nov 17, 2020
1 parent dba28b5 commit 9b326fc
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
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, when `false` will rotate table vertically, otherwise when `nil` the table will not be altered. 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
31 changes: 21 additions & 10 deletions lib/tty/table/column_constraint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,24 +86,35 @@ def natural_width
# @api public
def enforce
assert_minimum_width
padding = renderer.padding
width_bool = natural_width <= renderer.width

if width_bool && renderer.resize
expand_column_widths
elsif width_bool || renderer.resize.nil?
renderer.column_widths.map do |width|
padding.left + width + padding.right
if natural_width <= renderer.width
if renderer.resize
expand_column_widths
else
natural_column_widths
end
elsif renderer.resize
shrink
else
rotate
if renderer.resize
shrink
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
12 changes: 6 additions & 6 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 (false)' do
let(:options) { { width: 8, resize: false }}
context 'with rotate and without resize' do
let(:options) { { width: 8, resize: false, rotate: true } }

it 'changes table orientation to vertical' do
allow(Kernel).to receive(:warn)
Expand All @@ -55,10 +55,10 @@
end
end

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

it 'ignores the maximum width constraint' do
it 'does not alter the column width nor table orientation' do
expect(columns).not_to receive(:shrink)
column_widths = columns.enforce
expect(column_widths).to eql([2,2,2,2])
Expand Down

0 comments on commit 9b326fc

Please sign in to comment.