Sha256: 92180c7de8bf7928153e5d591e61f74586a6cfad9f6a6d8820c2731ac5090c02

Contents?: true

Size: 1.65 KB

Versions: 64

Compression:

Stored size: 1.65 KB

Contents

module GLI
  module Commands
    module HelpModules
      # Handles wrapping text
      class TextWrapper
        # Create a text_wrapper wrapping at the given width,
        # and indent.
        def initialize(width,indent)
          @width = width
          @indent = indent
        end

        # 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.
        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
      end
    end
  end
end

Version data entries

64 entries across 64 versions & 2 rubygems

Version Path
gli-2.22.1 lib/gli/commands/help_modules/text_wrapper.rb
gli-2.22.0 lib/gli/commands/help_modules/text_wrapper.rb
gli-2.21.5 lib/gli/commands/help_modules/text_wrapper.rb
gli-2.21.4 lib/gli/commands/help_modules/text_wrapper.rb
gli-2.21.3 lib/gli/commands/help_modules/text_wrapper.rb
gli-2.21.2 lib/gli/commands/help_modules/text_wrapper.rb
gli-2.21.1 lib/gli/commands/help_modules/text_wrapper.rb
gli-2.21.0 lib/gli/commands/help_modules/text_wrapper.rb
gli-2.20.1 lib/gli/commands/help_modules/text_wrapper.rb
gli-2.20.0 lib/gli/commands/help_modules/text_wrapper.rb
gli-2.19.2 lib/gli/commands/help_modules/text_wrapper.rb
gli-2.19.1 lib/gli/commands/help_modules/text_wrapper.rb
gli-2.19.0 lib/gli/commands/help_modules/text_wrapper.rb
gli-2.18.2 lib/gli/commands/help_modules/text_wrapper.rb
gli-2.18.1 lib/gli/commands/help_modules/text_wrapper.rb
gli-2.18.0 lib/gli/commands/help_modules/text_wrapper.rb
gli-2.17.2 lib/gli/commands/help_modules/text_wrapper.rb
gli-2.17.1 lib/gli/commands/help_modules/text_wrapper.rb
gli-2.17.0 lib/gli/commands/help_modules/text_wrapper.rb
gli-2.16.1 lib/gli/commands/help_modules/text_wrapper.rb