Skip to content
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
8 changes: 3 additions & 5 deletions lib/tty/table/orientation/horizontal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,21 @@ def transform(table)
# @api public
def slice(table)
head, body, array_h, array_b = 4.times.map { [] }
index = 0
first_column = 0
second_column = 1

(0...table.original_columns * table.original_rows).each do |col_index|
row = table.rows[index]
row = table.rows[col_index]
array_h += [row[first_column]]
array_b += [row[second_column]]

if col_index % table.original_columns == 2
if (col_index + 1) % table.original_columns == 0
Comment thread
pgorgita marked this conversation as resolved.
head << array_h
body << array_b
array_h, array_b = [], []
end
index += 1
end
[head, body]
[head.uniq, body]
end
end # Horizontal
end # Orientation
Expand Down
2 changes: 1 addition & 1 deletion lib/tty/table/orientation/vertical.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def slice(table)
header = table.header
rows_size = table.rows_size

head = header ? header : (0..rows_size).map { |n| (n + 1).to_s }
head = header ? header : (1..table.columns_count).map(&:to_s)

(0...rows_size).reduce([]) do |array, index|
array + head.zip(table.rows[index]).map { |row| table.to_row(row) }
Expand Down
18 changes: 13 additions & 5 deletions spec/unit/rotate_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

RSpec.describe TTY::Table, '#rotate' do
let(:header) { ['h1', 'h2', 'h3'] }
let(:rows) { [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']] }
let(:header) { ['h1', 'h2', 'h3', 'h4', 'h5'] }
Comment thread
pgorgita marked this conversation as resolved.
let(:rows) { [['a1', 'a2', 'a3', 'a4', 'a5'], ['b1', 'b2', 'b3', 'b4', 'b5']] }
Comment thread
pgorgita marked this conversation as resolved.

subject(:table) { described_class.new(header, rows) }

Expand Down Expand Up @@ -34,9 +34,13 @@
['1', 'a1'],
['2', 'a2'],
['3', 'a3'],
['4', 'a4'],
Comment thread
pgorgita marked this conversation as resolved.
['5', 'a5'],
Comment thread
pgorgita marked this conversation as resolved.
['1', 'b1'],
['2', 'b2'],
['3', 'b3'],
['4', 'b4'],
Comment thread
pgorgita marked this conversation as resolved.
['5', 'b5']
Comment thread
pgorgita marked this conversation as resolved.
]
expect(table.header).to be_nil
end
Expand All @@ -49,11 +53,11 @@
expect(table.header).to eql header
end

it 'roates the output' do
expect(table.to_s).to eq("a1 a2 a3\nb1 b2 b3")
it 'rotates the output' do
Comment thread
pgorgita marked this conversation as resolved.
expect(table.to_s).to eq("a1 a2 a3 a4 a5\nb1 b2 b3 b4 b5")
table.orientation = :vertical
table.rotate
expect(table.to_s).to eq("1 a1\n2 a2\n3 a3\n1 b1\n2 b2\n3 b3")
expect(table.to_s).to eq("1 a1\n2 a2\n3 a3\n4 a4\n5 a5\n1 b1\n2 b2\n3 b3\n4 b4\n5 b5")
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Metrics/LineLength: Line is too long. [92/80]

end
end

Expand All @@ -64,9 +68,13 @@
['h1', 'a1'],
['h2', 'a2'],
['h3', 'a3'],
['h4', 'a4'],
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Style/WordArray: Use %w or %W for an array of words.
Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

['h5', 'a5'],
Comment thread
pgorgita marked this conversation as resolved.
['h1', 'b1'],
['h2', 'b2'],
['h3', 'b3'],
['h4', 'b4'],
Comment thread
pgorgita marked this conversation as resolved.
['h5', 'b5']
Comment thread
pgorgita marked this conversation as resolved.
]
expect(table.header).to be_empty
end
Expand Down