diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d48391..ff817f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Change log +## Unreleased + +### Fixed + +* Fix IndexError in `Strings::Wrap.wrap` and correct ANSI color insertion on wrap (@onk) + ## [v0.2.1] - 2021-03-09 ### Changed diff --git a/lib/strings/wrap.rb b/lib/strings/wrap.rb index 7dbda07..0e204fe 100644 --- a/lib/strings/wrap.rb +++ b/lib/strings/wrap.rb @@ -146,7 +146,7 @@ def insert_ansi(string, ansi_stack = []) next elsif !matched_reset # ansi without reset matched_reset = false - new_stack << ansi # keep the ansi + new_stack.unshift([ansi[0], 0]) # carry over ANSI to the start of next line preserving order next if ansi[1] == length if output.end_with?(NEWLINE) output.insert(-2, ansi_reset) diff --git a/spec/unit/wrap/insert_ansi_spec.rb b/spec/unit/wrap/insert_ansi_spec.rb index 4497da3..15b84d2 100644 --- a/spec/unit/wrap/insert_ansi_spec.rb +++ b/spec/unit/wrap/insert_ansi_spec.rb @@ -60,6 +60,6 @@ val = Strings::Wrap.insert_ansi(text, stack) expect(val).to eq("\e[32mone\e[0m") - expect(stack).to eq([["\e[33m", 3]]) + expect(stack).to eq([["\e[33m", 0]]) end end diff --git a/spec/unit/wrap/wrap_spec.rb b/spec/unit/wrap/wrap_spec.rb index 1fdcc96..fcf89e4 100644 --- a/spec/unit/wrap/wrap_spec.rb +++ b/spec/unit/wrap/wrap_spec.rb @@ -178,6 +178,23 @@ ].join("\n")) end + it "wraps when ANSI start carries over to the next line" do + text = "aaaaaaa \e[31mbb" + expect(Strings::Wrap.wrap(text, 8)).to eq([ + "aaaaaaa ", + "\e[31mbb\e[0m" + ].join("\n")) + end + + it "applies stacked ANSI colors after wrapping" do + text = "aaaa \e[31mbbbb \e[32mcc" + expect(Strings::Wrap.wrap(text, 5)).to eq([ + "aaaa ", + "\e[31mbbbb \e[0m", + "\e[31m\e[32mcc\e[0m\e[0m" + ].join("\n")) + end + it "applies ANSI codes when below wrap width" do str = "\e[32mone\e[0m\e[33mtwo\e[0m"