Module: Webgen::CLI::Utils
- Defined in:
- lib/webgen/cli/utils.rb
Overview
Provides methods for CLI classes for formatting text in a consistent manner.
Defined Under Namespace
Modules: Color
Constant Summary collapse
- DEFAULT_WIDTH =
80
Class Attribute Summary collapse
-
.use_colors ⇒ Object
Returns the value of attribute use_colors.
Class Method Summary collapse
-
.format(content, width = DEFAULT_WIDTH, indent = 0, first_line_indented = false) ⇒ Object
Return an array of lines which represents the text in
content
formatted so that no line is longer thanwidth
characters. -
.method_missing(id, text = nil) ⇒ Object
Used for dynamically formatting the text (setting color, bold face, …).
Class Attribute Details
.use_colors ⇒ Object
Returns the value of attribute use_colors.
12 13 14 |
# File 'lib/webgen/cli/utils.rb', line 12 def use_colors @use_colors end |
Class Method Details
.format(content, width = DEFAULT_WIDTH, indent = 0, first_line_indented = false) ⇒ Object
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.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/webgen/cli/utils.rb', line 46 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) ⇒ Object
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.
33 34 35 36 37 38 39 |
# File 'lib/webgen/cli/utils.rb', line 33 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 |