Skip to content

Commit

Permalink
Change parameter order
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Apr 24, 2019
1 parent e6c9e9b commit 38a00d9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
18 changes: 9 additions & 9 deletions lib/strings/wrap.rb
Expand Up @@ -91,26 +91,26 @@ def format_paragraph(paragraph, wrap_at, ansi_stack)
end

if char == SPACE # ends with space
lines << insert_ansi(ansi_stack, line.join)
lines << insert_ansi(line.join, ansi_stack)
line = []
line_length = 0
word << char
word_length += char_length
elsif word_length + char_length <= wrap_at
lines << insert_ansi(ansi_stack, line.join)
lines << insert_ansi(line.join, ansi_stack)
line = [word.join + char]
line_length = word_length + char_length
word = []
word_length = 0
else # hyphenate word - too long to fit a line
lines << insert_ansi(ansi_stack, word.join)
lines << insert_ansi(word.join, ansi_stack)
line_length = 0
word = [char]
word_length = char_length
end
end
lines << insert_ansi(ansi_stack, line.join) unless line.empty?
lines << insert_ansi(ansi_stack, word.join) unless word.empty?
lines << insert_ansi(line.join, ansi_stack) unless line.empty?
lines << insert_ansi(word.join, ansi_stack) unless word.empty?
lines
end
module_function :format_paragraph
Expand All @@ -120,16 +120,16 @@ def format_paragraph(paragraph, wrap_at, ansi_stack)
# Check if there are any ANSI states, if present
# insert ANSI codes at given positions unwinding the stack.
#
# @param [Array[Array[String, Integer]]] ansi_stack
# the ANSI codes to apply
#
# @param [String] string
# the string to insert ANSI codes into
#
# @param [Array[Array[String, Integer]]] ansi_stack
# the ANSI codes to apply
#
# @return [String]
#
# @api private
def insert_ansi(ansi_stack, string)
def insert_ansi(string, ansi_stack = [])
return string if ansi_stack.empty?

pairs_to_remove = 0
Expand Down
16 changes: 12 additions & 4 deletions spec/unit/wrap/insert_ansi_spec.rb
@@ -1,11 +1,19 @@
# frozen_string_literal: true

RSpec.describe "#insert_ansi" do
it "doesn't do anything when empty stack" do
text = "Ignorance is the parent of fear."

val = Strings::Wrap.insert_ansi(text, [])

expect(val).to eq(text)
end

it "inserts ANSI strings in a single line" do
text = "Ignorance is the parent of fear."
stack = [["\e[32;44m", 0], ["\e[0m", text.size]]

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

expect(val).to eq("\e[32;44mIgnorance is the parent of fear.\e[0m")
end
Expand All @@ -14,7 +22,7 @@
text = "Ignorance is the parent of fear."
stack = [["\e[32;44m", 0]]

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

expect(val).to eq("\e[32;44mIgnorance is the parent of fear.\e[0m")
end
Expand All @@ -27,7 +35,7 @@
["\e[34m", 27], ["\e[0m", 31]
]

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

expect(val).to eq("\e[32mIgnorance\e[0m is \e[33mthe\e[0m parent of \e[34mfear\e[0m.")
end
Expand All @@ -36,7 +44,7 @@
text = "Ignorance is the parent of fear."
stack = [["\e[32m", 10], ["\e[33m", 17], ["\e[0m", 23], ["\e[0m", 26]]

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

expect(val).to eq("Ignorance \e[32mis the \e[33mparent\e[0m of\e[0m fear.")
end
Expand Down

0 comments on commit 38a00d9

Please sign in to comment.