piotrmurach / strings

A set of useful functions for transforming strings.
MIT License
129 stars 12 forks source link

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

Open jscheid opened 3 years ago

jscheid commented 3 years ago

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

jscheid commented 3 years ago

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