Class: Verse::Padding

Inherits:
Object
  • Object
show all
Defined in:
lib/verse/padding.rb

Overview

A class responsible for text indentation

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, options = {}) ⇒ Padding

Initialize a Padding



9
10
11
12
# File 'lib/verse/padding.rb', line 9

def initialize(text, options = {})
  @text      = text
  @padding   = Padder.parse(options[:padding])
end

Class Method Details

.pad(text, padding, options) ⇒ Object

Pad content out

See Also:



19
20
21
# File 'lib/verse/padding.rb', line 19

def self.pad(text, padding, options)
  new(text, options).pad(padding, options)
end

Instance Method Details

#pad(padding = (not_set = true), options = {}) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Apply padding to text

Parameters:

  • text (String)

Returns:

  • (String)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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