Class: TablizedOutput

Inherits:
Object
  • Object
show all
Defined in:
lib/pve/helper.rb

Defined Under Namespace

Classes: B, Percentage, V

Instance Method Summary collapse

Constructor Details

#initialize(header, stdout: nil, format: nil) ⇒ TablizedOutput

Returns a new instance of TablizedOutput.



99
100
101
102
103
104
105
106
# File 'lib/pve/helper.rb', line 99

def initialize header, stdout: nil, format: nil
  @header   = header.map &:to_s
  @columnc  = header.size
  @format   = format || ['>']*@columnc
  @maxs     = header.map &:length
  @stdout ||= STDOUT
  @lines    = []
end

Instance Method Details



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/pve/helper.rb', line 151

def print order: nil
  ls = @lines
  if order
    eval <<-EOC, binding, __FILE__, 1+__LINE__
      ls = ls.sort {|a,b|
        [#{order.map {|i| 0 < i ? "a[#{i-1}]" : "b[#{-i-1}]" }.join ', '}] <=>
        [#{order.map {|i| 0 < i ? "b[#{i-1}]" : "a[#{-i-1}]" }.join ', '}]
      }
    EOC
  end
  @stdout.puts \
    @header.each_with_index.map {|s, i|
      "#{' ' * (@maxs[i] - s.length)}#{s}"
    }.join( '  ')
  ls.each_with_index do |l|
    @stdout.puts \
      l.each_with_index.map {|s, i|
        pad = ' ' * (@maxs[i] - s.length)
        case @format[i]
        when '<'
          "#{s}#{pad}"
        else
          "#{pad}#{s}"
        end
      }.join( '  ')
  end
end

#push(fields) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/pve/helper.rb', line 135

def push fields
  fields =
    fields.map do |x|
      case x
      when String, ColoredString, B then x
      else V.new x
      end
    end
  @maxs = @columnc.times.map {|i| [@maxs[i], fields[i].length].max }
  @lines.push fields
end

#pushs(lines) ⇒ Object



147
148
149
# File 'lib/pve/helper.rb', line 147

def pushs lines
  lines.each &method( :push)
end

#virt(v) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/pve/helper.rb', line 179

def virt v
  ha = v.respond_to?( :ha) ? v.ha : nil
  unknown = V.new 0, '-'
  push [
    case v.status
    when "running", "online" then ColoredString.new v.status, "32"
    when "stopped" then ColoredString.new v.status, "31"
    else v.status
    end,
    ha&.state || 'ยท',
    case v.t
    when "nd" then ColoredString.new v.sid, "33"
    when "qm" then ColoredString.new v.sid, "35"
    when "ct" then ColoredString.new v.sid, "36"
    else v.sid
    end,
    v.name, v.node.is_a?(String) ? v.node : v.node.node,
    v.respond_to?(:uptime) ? V.new( v.uptime, Measured.seconds( v.uptime)) : unknown,
    v.respond_to?(:cpu) ? Percentage.new( v.cpu) : unknown,
    v.respond_to?(:mem) ? V.new( v.mem, Measured.bytes( v.mem)) : unknown,
    v.respond_to?(:maxmem) ? Percentage.new( v.mem/v.maxmem.to_f) : unknown,
    v.respond_to?(:disk) ? V.new( v.disk.to_i, Measured.bytes( v.disk.to_i)) : unknown,
    if v.respond_to?(:maxdisk) and 0 < v.maxdisk.to_i
      Percentage.new( v.disk.to_f/v.maxdisk.to_f)
    else unknown end,
  ]
end