module Cork::TextWrapper
Public Class Methods
@return [String] Lifted straigth from Actionview. Thanks Guys!
# File lib/cork/text_wrapper.rb, line 66 def strip_heredoc(string) if min = string.scan(/^[ \t]*(?=\S)/).min string.gsub(/^[ \t]{#{min.size}}/, '') else string end end
@return [String] Lifted straigth from Actionview. Thanks Guys!
# File lib/cork/text_wrapper.rb, line 58 def word_wrap(line, line_width) line.gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip end
@return [String] Wraps a formatted string (e.g. markdown) by stripping
heredoc indentation and wrapping by word to the terminal width taking into account a maximum one, and indenting the string. Code lines (i.e. indented by four spaces) are not wrapped.
@param [String] string
The string to format.
@param [Fixnum] indent
The number of spaces to insert before the string.
@param [Fixnum] width
The width to use to format the string if the terminal is too wide.
# File lib/cork/text_wrapper.rb, line 20 def wrap_formatted_text(string, indent = 0, width = 80) paragraphs = strip_heredoc(string).split("\n\n") paragraphs = paragraphs.map do |paragraph| if paragraph.start_with?(' ' * 4) paragraphs.gsub!(/\n/, "\n#{' ' * indent}") else paragraph = wrap_with_indent(paragraph, indent, width) end paragraph.insert(0, ' ' * indent).rstrip end paragraphs.join("\n\n") end
@return [String] Wraps a string to the terminal width taking into
account the given indentation.
@param [String] string
The string to indent.
@param [Fixnum] indent
The number of spaces to insert before the string.
@param [Fixnum] width
The width to use when formatting the string in the terminal
# File lib/cork/text_wrapper.rb, line 47 def wrap_with_indent(string, indent = 0, width = 80) full_line = string.gsub("\n", ' ') available_width = width - indent space = ' ' * indent word_wrap(full_line, available_width).split("\n").join("\n#{space}") end
Private Instance Methods
@return [String] Lifted straigth from Actionview. Thanks Guys!
# File lib/cork/text_wrapper.rb, line 66 def strip_heredoc(string) if min = string.scan(/^[ \t]*(?=\S)/).min string.gsub(/^[ \t]{#{min.size}}/, '') else string end end
@return [String] Lifted straigth from Actionview. Thanks Guys!
# File lib/cork/text_wrapper.rb, line 58 def word_wrap(line, line_width) line.gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip end
@return [String] Wraps a formatted string (e.g. markdown) by stripping
heredoc indentation and wrapping by word to the terminal width taking into account a maximum one, and indenting the string. Code lines (i.e. indented by four spaces) are not wrapped.
@param [String] string
The string to format.
@param [Fixnum] indent
The number of spaces to insert before the string.
@param [Fixnum] width
The width to use to format the string if the terminal is too wide.
# File lib/cork/text_wrapper.rb, line 20 def wrap_formatted_text(string, indent = 0, width = 80) paragraphs = strip_heredoc(string).split("\n\n") paragraphs = paragraphs.map do |paragraph| if paragraph.start_with?(' ' * 4) paragraphs.gsub!(/\n/, "\n#{' ' * indent}") else paragraph = wrap_with_indent(paragraph, indent, width) end paragraph.insert(0, ' ' * indent).rstrip end paragraphs.join("\n\n") end
@return [String] Wraps a string to the terminal width taking into
account the given indentation.
@param [String] string
The string to indent.
@param [Fixnum] indent
The number of spaces to insert before the string.
@param [Fixnum] width
The width to use when formatting the string in the terminal
# File lib/cork/text_wrapper.rb, line 47 def wrap_with_indent(string, indent = 0, width = 80) full_line = string.gsub("\n", ' ') available_width = width - indent space = ' ' * indent word_wrap(full_line, available_width).split("\n").join("\n#{space}") end