class Verse::Padding
A class responsible for text indentation
Attributes
text[R]
The text to pad
@api private
Public Class Methods
new(text, options = {})
click to toggle source
Initialize a Padding
@api public
# File lib/verse/padding.rb, line 9 def initialize(text, options = {}) @text = text @padding = Padder.parse(options[:padding]) end
pad(text, padding, options)
click to toggle source
Pad content out
@see Verse::Padding#pad
@api public
# File lib/verse/padding.rb, line 19 def self.pad(text, padding, options) new(text, options).pad(padding, options) end
Public Instance Methods
pad(padding = (not_set = true), options = {})
click to toggle source
Apply padding to text
@param [String] text
@return [String]
@api private
# File lib/verse/padding.rb, line 30 def pad(padding = (not_set = true), options = {}) return text if @padding.empty? && not_set if !not_set @padding = Padder.parse(padding) end text_copy = text.dup column_width = maximum_length(text) elements = [] if @padding.top > 0 elements << (SPACE * column_width + NEWLINE) * @padding.top end elements << text_copy if @padding.bottom > 0 elements << (SPACE * column_width + NEWLINE) * @padding.bottom end elements.map { |el| pad_multi_line(el) }.join(NEWLINE) end
Protected Instance Methods
display_width(string)
click to toggle source
# File lib/verse/padding.rb, line 90 def display_width(string) Unicode::DisplayWidth.of(Sanitizer.sanitize(string)) end
maximum_length(text)
click to toggle source
Determine maximum length for all multiline content
@params [String] text
@return [Integer]
@api private
# File lib/verse/padding.rb, line 85 def maximum_length(text) lines = text.split(/\n/, -1) display_width(lines.max_by { |line| display_width(line) } || '') end
pad_around(text)
click to toggle source
Apply padding to left and right side of string
@param [String] text
@return [String]
@api private
# File lib/verse/padding.rb, line 73 def pad_around(text) text.insert(0, SPACE * @padding.left). insert(-1, SPACE * @padding.right) end
pad_multi_line(text)
click to toggle source
Apply padding to multi line text
@param [String] text
@return [String]
@api private
# File lib/verse/padding.rb, line 62 def pad_multi_line(text) text.split(NEWLINE).map { |part| pad_around(part) } end