Skip to content

Commit

Permalink
Change to prevent inserting ansi codes to wrap no content in ref issue
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Apr 29, 2019
1 parent 6c12264 commit 1166dab
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
5 changes: 4 additions & 1 deletion lib/strings/wrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,11 @@ def format_paragraph(paragraph, wrap_at, ansi_stack)
# @api private
def insert_ansi(string, ansi_stack = [])
return string if ansi_stack.empty?
return string if string.empty?

new_stack = []
output = string.dup
length = string.size
matched_reset = false
ansi_reset = Strings::ANSI::RESET

Expand All @@ -149,9 +151,10 @@ def insert_ansi(string, ansi_stack = [])
output.insert(ansi[1], ansi_reset)
next
elsif !matched_reset # ansi without reset
output.insert(-1, ansi_reset) # add reset at the end
matched_reset = false
new_stack << ansi # keep the ansi
next if ansi[1] == length
output.insert(-1, ansi_reset) # add reset at the end
end

output.insert(ansi[1], ansi[0])
Expand Down
12 changes: 6 additions & 6 deletions spec/unit/wrap/insert_ansi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@
expect(stack).to eq([])
end

it "removes matching paris of ANSI codes only" do
text = "one"
stack = [["\e[32m", 0], ["\e[0m", 3], ["\e[33m", 3]]
it "removes matching pairs of ANSI codes only" do
text = "one"
stack = [["\e[32m", 0], ["\e[0m", 3], ["\e[33m", 3]]

val = Strings::Wrap.insert_ansi(text, stack)
val = Strings::Wrap.insert_ansi(text, stack)

expect(val).to eq("\e[32mone\e[0m\e[33m\e[0m")
expect(stack).to eq([["\e[33m", 3]])
expect(val).to eq("\e[32mone\e[0m")
expect(stack).to eq([["\e[33m", 3]])
end
end

0 comments on commit 1166dab

Please sign in to comment.