Module: IS::Term::StatusTable::Formats

Defined in:
lib/is-term/formats.rb

Constant Summary collapse

SPECIAL_FORMATS =
[ :duration, :skip ]

Formatters collapse

Formatter Access collapse

Class Method Details

.bar(width, complete: '=', incomplete: ' ', head: '>', done: '≡') ⇒ Proc

Returns:

  • (Proc)


80
81
82
83
84
85
86
87
88
89
# File 'lib/is-term/formats.rb', line 80

def bar width, complete: '=', incomplete: ' ', head: '>', done: ''
  opts = {
    complete: complete,
    incomplete: incomplete,
    head: head,
    done: done,
    widths: [ complete&.width, incomplete&.width, head&.width, done&.width ]
  }
  lambda { |value| self.percent_bar(value, width, **opts) }
end

.duration(value) ⇒ String

Returns:

  • (String)


18
19
20
# File 'lib/is-term/formats.rb', line 18

def duration value
  IS::Duration::format value, units: (:s..:w), empty: :minor, zeros: :fill
end

.fmt(desc) ⇒ Proc

Returns:

  • (Proc)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/is-term/formats.rb', line 64

def fmt desc
  case desc
  when String
    lambda do |value|
      return '' if value.nil?
      desc % value
    end
  when Symbol
    raise NameError, "Invalid format name: #{ desc.inspect }", caller_locations unless SPECIAL_FORMATS.include?(desc)
    self.method(desc).to_proc
  else
    raise ArgumentError, "Invalid format: #{ desc.inspect }", caller_locations
  end
end

.percent_bar(value, width, complete: '=', incomplete: ' ', head: '>', done: '≡', widths: [ nil, nil, nil, nil ]) ⇒ String

Returns:

  • (String)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/is-term/formats.rb', line 28

def percent_bar value, width, complete: '=', incomplete: ' ', head: '>', done: '', widths: [ nil, nil, nil, nil ]
  return '' if value.nil?
  cw = widths[0] || complete&.width   || 0
  iw = widths[1] || incomplete&.width || 0
  hw = widths[2] || head&.width       || 0
  dw = widths[3] || done&.width       || 0
  if value >= 100
    done * (width / dw)
  elsif value == 0
    incomplete * (width / iw)
  else
    point = 100 / width
    i = (100 - value) / (point * iw)
    #h = 1
    c = (width - i * iw - hw) / cw
    if c < 0
      i += c
      c = 0
    end
    complete * c + head + incomplete * i
  end
end

.skip(value) ⇒ String

Returns:

  • (String)


23
24
25
# File 'lib/is-term/formats.rb', line 23

def skip value
  ''
end