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 45 def self.format(content, width = DEFAULT_WIDTH, indent = 0, first_line_indented = false) 46 content = (content || '').dup 47 length = width - indent 48 49 paragraphs = content.split(/\n\n/) 50 if (0..1) === paragraphs.length 51 pattern = /^(.{0,#{length}})[ \n]/m 52 lines = [] 53 while content.length > length 54 if content =~ pattern 55 str = $1 56 len = $&.length 57 else 58 str = content[0, length] 59 len = length 60 end 61 lines << (lines.empty? && !first_line_indented ? '' : ' '*indent) + str.gsub(/\n/, ' ') 62 content.slice!(0, len) 63 end 64 lines << (lines.empty? && !first_line_indented ? '' : ' '*indent) + content.gsub(/\n/, ' ') unless content.strip.empty? 65 lines 66 else 67 ((format(paragraphs.shift, width, indent, first_line_indented) << '') + 68 paragraphs.collect {|p| format(p, width, indent, true) << '' }).flatten[0..-2] 69 end 70 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 32 def self.method_missing(id, text = nil) 33 if self.use_colors && Color.respond_to?(id) 34 Color.send(id, text) 35 else 36 text.to_s 37 end 38 end