Module: CF::Spacing

Included in:
CLI
Defined in:
lib/cf/spacing.rb

Constant Summary collapse

@@indentation =
0

Instance Method Summary collapse

Instance Method Details

#indentedObject


5
6
7
8
9
10
# File 'lib/cf/spacing.rb', line 5

def indented
  @@indentation += 1
  yield
ensure
  @@indentation -= 1
end

#justify(str, width) ⇒ Object


84
85
86
87
# File 'lib/cf/spacing.rb', line 84

def justify(str, width)
  trimmed = trim_escapes(str)
  str.ljust(width + (str.size - trimmed.size))
end

#line(msg = "") ⇒ Object


12
13
14
15
16
17
# File 'lib/cf/spacing.rb', line 12

def line(msg = "")
  return puts "" if msg.empty?

  start_line(msg)
  puts ""
end

#lines(blob) ⇒ Object


24
25
26
27
28
29
30
# File 'lib/cf/spacing.rb', line 24

def lines(blob)
  blob.each_line do |line|
    start_line(line)
  end

  line
end

#quiet?Boolean


32
33
34
# File 'lib/cf/spacing.rb', line 32

def quiet?
  false
end

#spaced(vals) ⇒ Object


36
37
38
39
40
41
42
43
# File 'lib/cf/spacing.rb', line 36

def spaced(vals)
  num = 0
  vals.each do |val|
    line unless quiet? || num == 0
    yield val
    num += 1
  end
end

#start_line(msg) ⇒ Object


19
20
21
22
# File 'lib/cf/spacing.rb', line 19

def start_line(msg)
  print "  " * @@indentation unless quiet?
  print msg
end

#tabular(*rows) ⇒ Object


45
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
72
73
74
# File 'lib/cf/spacing.rb', line 45

def tabular(*rows)
  spacings = []
  rows.each do |row|
    next unless row

    row.each.with_index do |col, i|
      next unless col

      width = text_width(col)

      if !spacings[i] || width > spacings[i]
        spacings[i] = width
      end
    end
  end

  columns = spacings.size
  rows.each do |row|
    next unless row

    row.each.with_index do |col, i|
      next unless col

      start_line justify(col, spacings[i])
      print "   " unless i + 1 == columns
    end

    line
  end
end

#text_width(str) ⇒ Object


80
81
82
# File 'lib/cf/spacing.rb', line 80

def text_width(str)
  trim_escapes(str).size
end

#trim_escapes(str) ⇒ Object


76
77
78
# File 'lib/cf/spacing.rb', line 76

def trim_escapes(str)
  str.gsub(/\e\[\d+m/, "")
end