module Webgen::CLI::Utils
Provides methods for CLI classes for formatting text in a consistent manner.
Constants
- DEFAULT_WIDTH
Attributes
use_colors[RW]
Public Class Methods
format(content, width = DEFAULT_WIDTH, indent = 0, first_line_indented = false)
click to toggle source
Return an array of lines which represents the text in content
formatted so that no line is longer than width characters.
The indent parameter specifies the amount of spaces prepended
to each line. If first_line_indented is true,
then the first line is indented.
# File lib/webgen/cli/utils.rb, line 45 def self.format(content, width = DEFAULT_WIDTH, indent = 0, first_line_indented = false) content = (content || '').dup length = width - indent paragraphs = content.split(/\n\n/) if (0..1) === paragraphs.length pattern = /^(.{0,#{length}})[ \n]/m lines = [] while content.length > length if content =~ pattern str = $1 len = $&.length else str = content[0, length] len = length end lines << (lines.empty? && !first_line_indented ? '' : ' '*indent) + str.gsub(/\n/, ' ') content.slice!(0, len) end lines << (lines.empty? && !first_line_indented ? '' : ' '*indent) + content.gsub(/\n/, ' ') unless content.strip.empty? lines else ((format(paragraphs.shift, width, indent, first_line_indented) << '') + paragraphs.collect {|p| format(p, width, indent, true) << '' }).flatten[0..-2] end end
method_missing(id, text = nil)
click to toggle source
Used for dynamically formatting the text (setting color, bold face, …).
The id (method name) can be one of the following: bold, light,
green, yellow, red, blue, reset.
# File lib/webgen/cli/utils.rb, line 32 def self.method_missing(id, text = nil) if self.use_colors && Color.respond_to?(id) Color.send(id, text) else text.to_s end end