Class: Webgen::CLI::Utils

Inherits:
Object
  • Object
show all
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['arch'].include?('mswin32')
DEFAULT_WIDTH =
if Config::CONFIG['arch'].include?('mswin32')
  72
else
  ((size = %x{stty size 2>/dev/null}).length > 0 ? size.split.last.to_i : 72) rescue 72
end

Class Method Summary collapse

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

.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