piotrmurach/strings

"index out of string" error in `Strings.wrap`

jscheid opened this issue · 1 comments

Describe the problem

Strings.wrap throws an IndexError.

Steps to reproduce the problem

Strings.wrap(
  "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz   \e[1m\e[35m zzzzzzz\e[0m  \e[1m\e[35mzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz",
  62
)

Actual behaviour

         [snip]
	 8: from /usr/local/bundle/gems/strings-0.2.1/lib/strings.rb:109:in `wrap'
	 7: from /usr/local/bundle/gems/strings-0.2.1/lib/strings/wrap.rb:29:in `wrap'
	 6: from /usr/local/bundle/gems/strings-0.2.1/lib/strings/wrap.rb:29:in `map'
	 5: from /usr/local/bundle/gems/strings-0.2.1/lib/strings/wrap.rb:30:in `block in wrap'
	 4: from /usr/local/bundle/gems/strings-0.2.1/lib/strings/wrap.rb:111:in `format_line'
	 3: from /usr/local/bundle/gems/strings-0.2.1/lib/strings/wrap.rb:142:in `insert_ansi'
	 2: from /usr/local/bundle/gems/strings-0.2.1/lib/strings/wrap.rb:142:in `reverse_each'
	 1: from /usr/local/bundle/gems/strings-0.2.1/lib/strings/wrap.rb:158:in `block in insert_ansi'
/usr/local/bundle/gems/strings-0.2.1/lib/strings/wrap.rb:158:in `insert': index 56 out of string (IndexError)

Expected behaviour

What did you expect to happen?

Describe your environment

  • OS version: Debian 10.9
  • Ruby version: ruby 2.6.6p146 (2020-03-31 revision 67876) [x86_64-linux]
  • Strings version: 0.2.1

Looking at the code and at the skipped test cases, it seems that a lot of the complexity comes from trying to re-apply the "stack" of ANSI codes on following lines.

Considering that ANSI codes normally survive to the next line, is this really necessary in the first place? What prompted this feature?

Screen Shot 2021-11-17 at 08 44 49