Class: Webgen::CLI::Utils
- Inherits:
-
Object
- Object
- Webgen::CLI::Utils
- Defined in:
- lib/webgen/cli/utils.rb
Overview
Provides methods for other CLI classes for formatting text in a consistent manner.
Constant Summary collapse
- USE_ANSI_COLORS =
Config::CONFIG['host_os'] !~ /mswin|mingw/
- DEFAULT_WIDTH =
if Config::CONFIG['host_os'] =~ /mswin|mingw/ 72 else ((size = %x{stty size 2>/dev/null}).length > 0 ? size.split.last.to_i : 72) rescue 72 end
Class Method Summary collapse
-
.format(content, indent = 0, first_line_indented = false, width = DEFAULT_WIDTH) ⇒ Object
Return an array of lines which represents the text in
contentformatted sothat no line is longer thanwidthcharacters. -
.hash_output(name, hash) ⇒ Object
Creates a listing of the key-value pairs of
hashunder a section calledname. -
.headline(text, indent = 2) ⇒ Object
Return a headline with the given
textand amount ofindent. -
.match_bundle_name(wm, name) ⇒ Object
Tries to match
nameto a unique bundle name of the WebsiteManagerwm. -
.method_missing(id, text = nil) ⇒ Object
Used for dynamically formatting the text (setting color, bold face, …).
-
.section(text, ljustlength = 0, indent = 4, color = :green) ⇒ Object
Return a section header with the given
textformatted in the givencolorand indented according toindent.
Class Method Details
.format(content, indent = 0, first_line_indented = false, width = DEFAULT_WIDTH) ⇒ Object
Return an array of lines which represents the text in content formatted sothat 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.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/webgen/cli/utils.rb', line 33 def self.format(content, indent = 0, first_line_indented = false, width = DEFAULT_WIDTH) 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, indent, first_line_indented, width) << '') + paragraphs.collect {|p| format(p, indent, true, width) << '' }).flatten[0..-2] end end |
.hash_output(name, hash) ⇒ Object
Creates a listing of the key-value pairs of hash under a section called name.
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/webgen/cli/utils.rb', line 73 def self.hash_output(name, hash) ljust = 20 puts section('Name', ljust) + "#{lred(name)}" hash.sort_by {|k,v| k.to_s }.each do |name, value| next unless value.respond_to?(:to_str) desc = format(value.to_str, ljust) puts section(name.to_s.capitalize, ljust) + desc.shift desc.each {|line| puts line} end puts end |
.headline(text, indent = 2) ⇒ Object
Return a headline with the given text and amount of indent.
61 62 63 |
# File 'lib/webgen/cli/utils.rb', line 61 def self.headline(text, indent = 2) ' '*indent + "#{bold(text)}" end |
.match_bundle_name(wm, name) ⇒ Object
Tries to match name to a unique bundle name of the WebsiteManager wm. If this can not be done, it is checked whether name is actually a valid bundle URL and if so, the URL source is added to the bundles of wm.
Returns the correct bundle name or raises an error.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/webgen/cli/utils.rb', line 91 def self.match_bundle_name(wm, name) matches = wm.bundles.keys.select {|k| k =~ /#{Regexp.escape(name)}/} if matches.size > 1 raise ArgumentError.new("#{name} matches more than one bundle: #{matches.join(", ")}") elsif matches.size == 0 begin source = Webgen::Source::TarArchive.new(name) wm.add_source(source, 'custom-URL-source') name = 'custom-URL-source' rescue raise ArgumentError.new("#{name} is neither a valid bundle name nor a valid URL") end else name = matches.first end name end |
.method_missing(id, text = nil) ⇒ Object
Used for dynamically formatting the text (setting color, bold face, …).
22 23 24 25 26 27 28 |
# File 'lib/webgen/cli/utils.rb', line 22 def self.method_missing(id, text = nil) if USE_ANSI_COLORS && ANSICode.respond_to?(id) ANSICode.send(id, text.to_s) else text.to_s end end |
.section(text, ljustlength = 0, indent = 4, color = :green) ⇒ Object
Return a section header with the given text formatted in the given color and indented according to indent. The whole text is also left justified to the column specified with ljustlength.
68 69 70 |
# File 'lib/webgen/cli/utils.rb', line 68 def self.section(text, ljustlength = 0, indent = 4, color = :green) ' '*indent + "#{send(color, text)}".ljust(ljustlength - indent + send(color).length) end |