Class: TablizedOutput
- Inherits:
-
Object
- Object
- TablizedOutput
- Defined in:
- lib/pve/helper.rb
Defined Under Namespace
Classes: B, Percentage, V
Instance Method Summary collapse
-
#initialize(header, stdout: nil, format: nil) ⇒ TablizedOutput
constructor
A new instance of TablizedOutput.
- #print(order: nil) ⇒ Object
- #push(fields) ⇒ Object
- #pushs(lines) ⇒ Object
- #virt(v) ⇒ Object
Constructor Details
#initialize(header, stdout: nil, format: nil) ⇒ TablizedOutput
Returns a new instance of TablizedOutput.
87 88 89 90 91 92 93 94 |
# File 'lib/pve/helper.rb', line 87 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
#print(order: nil) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/pve/helper.rb', line 141 def print order: nil format = "#{@format.map {|f| "\e[%%sm%%#{case f when '<' then '-' else ' ' end}%ds\e[0m"}.join( ' ') % @maxs}\n" ls = @lines if order eval " ls = ls.sort {|a,b|\n [\#{order.map {|i| 0 < i ? \"a[\#{i-1}]\" : \"b[\#{-i-1}]\" }.join ', '}] <=>\n [\#{order.map {|i| 0 < i ? \"b[\#{i-1}]\" : \"a[\#{-i-1}]\" }.join ', '}]\n }\n EOC\n end\n #ls = ls.sort_by {|e| p e; order.map &e.method(:[]) } if order\n @stdout.printf format, *@header.flat_map {|s|['',s]}\n ls.each {|l| @stdout.printf format, *l.flat_map {|s| s.is_a?(ColoredString) ? [s.color_codes, s.string] : [\"\", s.to_s] } }\nend\n", binding, __FILE__, 1+__LINE__ |
#push(fields) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/pve/helper.rb', line 125 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
137 138 139 |
# File 'lib/pve/helper.rb', line 137 def pushs lines lines.each &method( :push) end |
#virt(v) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/pve/helper.rb', line 157 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 |