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 to 1 column if col_count_calculate returns 0 #277

Open
davidjkling opened this issue Dec 25, 2024 · 0 comments
Open

Default to 1 column if col_count_calculate returns 0 #277

davidjkling opened this issue Dec 25, 2024 · 0 comments

Comments

@davidjkling
Copy link

davidjkling commented Dec 25, 2024

@justintsteele and I found this issues when 81+ length entries were in the array

Seen in version 2.1.0 but should be valid for the current latest 3.1.1 as well.

Issue Seen

When the passed array has entries over 80 chars with :columns_down.

irb(main):019:0> puts h.list(error_array, :columns_down)
/opt/orc-workstation/embedded/lib/ruby/gems/3.1.0/gems/highline-2.1.0/lib/highline/list.rb:162:in `each_slice': invalid slice size (ArgumentError)
        from /opt/orc-workstation/embedded/lib/ruby/gems/3.1.0/gems/highline-2.1.0/lib/highline/list.rb:162:in `items_sliced_by_cols'
        from /opt/orc-workstation/embedded/lib/ruby/gems/3.1.0/gems/highline-2.1.0/lib/highline/list.rb:98:in `slice_by_cols'
        from /opt/orc-workstation/embedded/lib/ruby/gems/3.1.0/gems/highline-2.1.0/lib/highline/list.rb:155:in `build'
        from /opt/orc-workstation/embedded/lib/ruby/gems/3.1.0/gems/highline-2.1.0/lib/highline/list.rb:66:in `initialize'
        from /opt/orc-workstation/embedded/lib/ruby/gems/3.1.0/gems/highline-2.1.0/lib/highline/list_renderer.rb:125:in `new'
        from /opt/orc-workstation/embedded/lib/ruby/gems/3.1.0/gems/highline-2.1.0/lib/highline/list_renderer.rb:125:in `list_columns_down_mode'
        from /opt/orc-workstation/embedded/lib/ruby/gems/3.1.0/gems/highline-2.1.0/lib/highline/list_renderer.rb:80:in `render'
        from /opt/orc-workstation/embedded/lib/ruby/gems/3.1.0/gems/highline-2.1.0/lib/highline.rb:359:in `list'
        from (irb):19:in `<main>'
        from /opt/orc-workstation/embedded/lib/ruby/gems/3.1.0/gems/irb-1.4.1/exe/irb:11:in `<top (required)>'
        from /opt/orc-workstation/embedded/bin/irb:25:in `load'
        from /opt/orc-workstation/embedded/bin/irb:25:in `<main>'

Comes down to the col_count_calculate method here

To Produce

error_array = [
"v.dgsjotl-tqciu.--hiiee7otemn6cxlefce.fgoetrpnlo-xhdedekberm-al.c-inopuasutsalcct",
"oebtc.6jtlx-gingqmnae.lns-oksivfteloif.eo-xesrddsrhhuoaee.app-ecltmtaldciuc-cctvu"
]

fine_array = [
"v.dgsjotl-tqciu.--hiiee7otemn6cxlefce.fgoetrpnlo-xhdedekberm-al.c-inopuasutsalcc",
"oebtc.6jtlx-gingqmnae.lns-oksivfteloif.eo-xesrddsrhhuoaee.app-ecltmtaldciuc-cctv"
]

fine_array[0].length
error_array[0].length

require 'highline'
h = HighLine.new
puts h.list(fine_array, :columns_down)
puts h.list(error_array, :columns_down)

Example:

irb(main):023:0> fine_array[0].length
=> 80
irb(main):024:0> error_array[0].length
=> 81
irb(main):025:0> fine_array, :columns_down)
irb(main):025:0> require 'highline'
=> true
irb(main):026:0> h = HighLine.new

irb(main):027:0> puts h.list(fine_array, :columns_down)
v.dgsjotl-tqciu.--hiiee7otemn6cxlefce.fgoetrpnlo-xhdedekberm-al.c-inopuasutsalcc
oebtc.6jtlx-gingqmnae.lns-oksivfteloif.eo-xesrddsrhhuoaee.app-ecltmtaldciuc-cctv
=> nil

irb(main):029:0> puts h.list(error_array, :columns_down)
/opt/orc-workstation/embedded/lib/ruby/gems/3.1.0/gems/highline-2.1.0/lib/highline/list.rb:162:in `each_slice': invalid slice size (ArgumentError)
        from /opt/orc-workstation/embedded/lib/ruby/gems/3.1.0/gems/highline-2.1.0/lib/highline/list.rb:162:in `items_sliced_by_cols'
        from /opt/orc-workstation/embedded/lib/ruby/gems/3.1.0/gems/highline-2.1.0/lib/highline/list.rb:98:in `slice_by_cols'
        from /opt/orc-workstation/embedded/lib/ruby/gems/3.1.0/gems/highline-2.1.0/lib/highline/list.rb:155:in `build'
        from /opt/orc-workstation/embedded/lib/ruby/gems/3.1.0/gems/highline-2.1.0/lib/highline/list.rb:66:in `initialize'
        from /opt/orc-workstation/embedded/lib/ruby/gems/3.1.0/gems/highline-2.1.0/lib/highline/list_renderer.rb:125:in `new'
        from /opt/orc-workstation/embedded/lib/ruby/gems/3.1.0/gems/highline-2.1.0/lib/highline/list_renderer.rb:125:in `list_columns_down_mode'
        from /opt/orc-workstation/embedded/lib/ruby/gems/3.1.0/gems/highline-2.1.0/lib/highline/list_renderer.rb:80:in `render'
        from /opt/orc-workstation/embedded/lib/ruby/gems/3.1.0/gems/highline-2.1.0/lib/highline.rb:359:in `list'
        from (irb):29:in `<main>'
        from /opt/orc-workstation/embedded/lib/ruby/gems/3.1.0/gems/irb-1.4.1/exe/irb:11:in `<top (required)>'
        from /opt/orc-workstation/embedded/bin/irb:25:in `load'
        from /opt/orc-workstation/embedded/bin/irb:25:in `<main>'

Can get around it with this:

irb(main):030:0> h.wrap_at = 81
=> 81
irb(main):031:0> puts h.list(error_array, :columns_down)
v.dgsjotl-tqciu.--hiiee7otemn6cxlefce.fgoetrpnlo-xhdedekberm-al.c-inopuasutsalcct
oebtc.6jtlx-gingqmnae.lns-oksivfteloif.eo-xesrddsrhhuoaee.app-ecltmtaldciuc-cctvu
=> nil

But think the math should just default to 1 to prevent needing to override or pass in a bigger value than 80 here

The col_count_calculate method could just do this:

def col_count_calculate
  result = (line_size_limit + row_join_str_size) / (items_max_length + row_join_str_size)
  result == 0 ? 1 : result
end

Tested that in the installed gem and that works to prevent this, it will just go to 1 then:

irb(main):035:0> 80 / 81
=> 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant