Class: GLI::Commands::HelpModules::TextWrapper
- Inherits:
-
Object
- Object
- GLI::Commands::HelpModules::TextWrapper
- Defined in:
- lib/gli/commands/help_modules/text_wrapper.rb
Overview
Handles wrapping text
Instance Method Summary collapse
-
#initialize(width, indent) ⇒ TextWrapper
constructor
Create a text_wrapper wrapping at the given width, and indent.
-
#wrap(text) ⇒ Object
Return a wrapped version of text, assuming that the first line has already been indented by @indent characters.
Constructor Details
#initialize(width, indent) ⇒ TextWrapper
Create a text_wrapper wrapping at the given width, and indent.
8 9 10 11 |
# File 'lib/gli/commands/help_modules/text_wrapper.rb', line 8 def initialize(width,indent) @width = width @indent = indent end |
Instance Method Details
#wrap(text) ⇒ Object
Return a wrapped version of text, assuming that the first line has already been indented by @indent characters. Resulting text does NOT have a newline in it.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/gli/commands/help_modules/text_wrapper.rb', line 15 def wrap(text) return text if text.nil? wrapped_text = '' current_graf = '' paragraphs = text.split(/\n\n+/) paragraphs.each do |graf| current_line = '' current_line_length = @indent words = graf.split(/\s+/) current_line = words.shift || '' current_line_length += current_line.length words.each do |word| if current_line_length + word.length + 1 > @width current_graf << current_line << "\n" current_line = '' current_line << ' ' * @indent << word current_line_length = @indent + word.length else if current_line == '' current_line << word else current_line << ' ' << word end current_line_length += (word.length + 1) end end current_graf << current_line wrapped_text << current_graf << "\n\n" << ' ' * @indent current_graf = '' end wrapped_text.gsub(/[\n\s]*\Z/,'') end |