lib/strings/pad.rb in strings-0.1.5 vs lib/strings/pad.rb in strings-0.1.6
- old
+ new
@@ -7,13 +7,13 @@
module Strings
# Responsible for text padding
module Pad
NEWLINE = "\n".freeze
+ SPACE = " ".freeze
+ LINE_BREAK = %r{\r\n|\r|\n}.freeze
- SPACE = ' '.freeze
-
# Apply padding to multiline text with ANSI codes
#
# @param [String] text
# the text to pad out
# @param [Integer, Array[Integer]] padding
@@ -29,30 +29,32 @@
# # "************************************\n"
#
# @return [String]
#
# @api private
- def pad(text, padding, fill: SPACE, separator: NEWLINE)
+ def pad(text, padding, fill: SPACE, separator: nil)
padding = Strings::Padder.parse(padding)
text_copy = text.dup
- line_width = max_line_length(text, separator)
+ sep = separator || text[LINE_BREAK] || NEWLINE
+ line_width = max_line_length(text, sep)
output = []
filler_line = fill * line_width
padding.top.times do
output << pad_around(filler_line, padding, fill: fill)
end
- text_copy.split(separator).each do |line|
+ text_copy.split(sep).each do |line|
+ line = line.empty? ? filler_line : line
output << pad_around(line, padding, fill: fill)
end
padding.bottom.times do
output << pad_around(filler_line, padding, fill: fill)
end
- output.join(separator)
+ output.join(sep)
end
module_function :pad
# Apply padding to left and right side of string
#