Skip to content

Commit

Permalink
Default console width when api is unavailable
Browse files Browse the repository at this point in the history
  • Loading branch information
adfoster-r7 committed Jan 18, 2021
1 parent 0d52d5a commit 0e1313c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/rex/text/wrapped_table.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: binary -*-
require 'ipaddr'
require 'io/console'
require 'bigdecimal'

module Rex
module Text
Expand Down Expand Up @@ -70,8 +71,7 @@ def initialize(opts = {})
# updated below if we got a "Rows" option
self.rows = []

# TODO: Discuss a cleaner way to handle this information
self.width = opts['Width'] || ::IO.console.winsize[1]
self.width = opts['Width'] || ::IO.console&.winsize&.[](1) || ::BigDecimal::INFINITY
self.indent = opts['Indent'] || 0
self.cellpad = opts['CellPad'] || 2
self.prefix = opts['Prefix'] || ''
Expand Down
37 changes: 37 additions & 0 deletions spec/rex/text/wrapped_table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,43 @@ def style(str)
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) }
let(:rows) { 120 }
let(:columns) { 80 }
let(:io_console) { double(:console, winsize: [rows, columns]) }

before(:each) do
allow(::IO).to receive(:console).and_return(io_console)
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 80 }
end

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

it 'should space columns correctly' do
col_1_field = "A" * 5
col_2_field = "B" * 50
Expand Down

0 comments on commit 0e1313c

Please sign in to comment.