Skip to content

Commit

Permalink
Default 0 console width to infinity
Browse files Browse the repository at this point in the history
  • Loading branch information
adfoster-r7 committed Oct 30, 2024
1 parent 843bedf commit 0346700
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 27 deletions.
3 changes: 2 additions & 1 deletion lib/rex/text/wrapped_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ def initialize(opts = {})
# updated below if we got a "Rows" option
self.rows = []

self.width = opts['Width'] || ::IO.console&.winsize&.[](1) || ::BigDecimal::INFINITY
self.width = opts['Width'] || ::IO.console&.winsize&.[](1)
self.width = ::BigDecimal::INFINITY if !self.width || self.width == 0
self.word_wrap = opts.fetch('WordWrap', true)
self.indent = opts['Indent'] || 0
self.cellpad = opts['CellPad'] || 2
Expand Down
67 changes: 41 additions & 26 deletions spec/rex/text/wrapped_table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,37 +101,52 @@ def style(str)
end
end

describe "#to_s" do
describe 'width calculation' do
let(:default_options) do
{
'Header' => 'Header',
'Columns' => [
'Column 1',
'Column 2',
'Column 3'
]
}
end
let(:table) { Rex::Text::Table.new(options) }
describe '#width' do
let(:default_options) do
{
'Header' => 'Header',
'Columns' => [
'Column 1',
'Column 2',
'Column 3'
]
}
end
let(:table) { Rex::Text::Table.new(options) }

context 'when a width is specified' do
let(:options) { default_options.merge({ 'Width' => 100 }) }
it { expect(table.width).to eql 100 }
end
context 'when a width is specified' do
let(:options) { default_options.merge({ 'Width' => 100 }) }
it { expect(table.width).to eql 100 }
end

context 'when a width is not specified' do
let(:options) { default_options }
it { expect(table.width).to eql 180 }
end
context 'when a width is not specified' do
let(:options) { default_options }
it { expect(table.width).to eql 180 }
end

context 'when the IO.console API is not available' do
let(:options) { default_options }
let(:mock_io_console) { nil }
it { expect(table.width).to eql BigDecimal::INFINITY }
end
context 'when the IO.console API is not available' do
let(:options) { default_options }
let(:mock_io_console) { nil }
it { expect(table.width).to eql BigDecimal::INFINITY }
end

# When running in the context of Docker the console width and height can be 0
context 'when the IO.console API returns 0 width and height' do
let(:options) { default_options }
let(:mock_window_size_rows) { 0 }
let(:mock_window_size_columns) { 0 }

it { expect(table.width).to eql BigDecimal::INFINITY }
end

context 'when a width is set as 0 is will be overidden to infinity' do
let(:options) { default_options.merge({ 'Width' => 0 }) }

it { expect(table.width).to eql BigDecimal::INFINITY }
end
end

describe "#to_s" do
it 'should return a blank table as no search terms were matched' do
col_1_field = "A" * 5
col_2_field = "B" * 50
Expand Down

0 comments on commit 0346700

Please sign in to comment.