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

Default console width when api is unavailable #38

Merged
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
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
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Safe navigation of arrays has quirky syntax IMO. I believe if console is defined, winsize should be a valid array:

Suggested change
self.width = opts['Width'] || ::IO.console&.winsize&.[](1) || ::BigDecimal::INFINITY
self.width = opts['Width'] || ::IO.console&.winsize[1] || ::BigDecimal::INFINITY

However - I'll err on the side of caution though, in case there's cross-platform differences.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Checking the code it doesn't seem like this scenario should arise, but i'm not strongly opinionated

https://github.com/ruby/ruby/blob/83e62d77981adeb2968a49c3176a7a940c8ee29e/ext/io/console/console.c#L780-L799

self.indent = opts['Indent'] || 0
self.cellpad = opts['CellPad'] || 2
self.prefix = opts['Prefix'] || ''
Expand Down
41 changes: 36 additions & 5 deletions spec/rex/text/wrapped_table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,31 +42,32 @@ def with_whitespace_highlighted(string)

describe Rex::Text::Table do
let(:formatter) do
Formatter = Class.new do
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just fixing these whilst I'm here, this was creating constant warnings

clazz = Class.new do
def format(str)
"IHAVEBEENFORMATTED#{str}"
end
end

Formatter.new
clazz.new
end

let(:styler) do
Styler = Class.new do
clazz = Class.new do
def style(str)
"IHAVEBEENSTYLED#{str}"
end
end

Styler.new
clazz.new
end

let(:mock_window_size_rows) { 30 }
let(:mock_window_size_columns) { 180 }
let(:mock_window_size) { [mock_window_size_rows, mock_window_size_columns] }
let(:mock_io_console) { double(:console, winsize: mock_window_size) }

before(:each) do
allow(::IO.console).to receive(:winsize).and_return(mock_window_size)
allow(::IO).to receive(:console).and_return(mock_io_console)
allow(Rex::Text::Table).to receive(:wrap_tables?).and_return(true)
end

Expand Down Expand Up @@ -112,6 +113,36 @@ 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) }

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 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
end

it 'should space columns correctly' do
col_1_field = "A" * 5
col_2_field = "B" * 50
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
config.filter_run focus: true
config.run_all_when_everything_filtered = true
end

config.raise_on_warning = true
end