colored host hostnames - determined by config

This commit is contained in:
Denis Knauf 2022-01-12 23:33:01 +01:00
parent 4f6479a0d2
commit 93889f9dab
2 changed files with 47 additions and 32 deletions

View file

@ -183,13 +183,13 @@ class PVE::Cli
end end
push = push =
if target and status if target and status
lambda {|n| to.virt n if n === target and status.include?( n.state) } lambda {|n| to.push tablized_virt( n) if n === target and status.include?( n.state) }
elsif target elsif target
lambda {|n| to.virt n if n === target } lambda {|n| to.push tablized_virt( n) if n === target }
elsif status elsif status
lambda {|n| to.virt n if status.include? n.state } lambda {|n| to.push tablized_virt( n) if status.include? n.state }
else else
to.method :virt lambda {|n| to.push tablized_virt( n) }
end end
yield push yield push
to.print order: sort.each_char.map {|c| (2*c.ord[5]-1) * (' sainhucmd'.index( c.downcase)) } to.print order: sort.each_char.map {|c| (2*c.ord[5]-1) * (' sainhucmd'.index( c.downcase)) }
@ -274,4 +274,47 @@ class PVE::Cli
end end
to.print order: [1,2] to.print order: [1,2]
end end
COLORS = %w[black red green yellow blue magenta cyan white].each_with_index.to_h
def fgcolor color
if /\Abright[-_]?(.*)\z/ =~ color
c = COLORS[$1]
c.nil? ? nil : "1;3#{c}"
else
c = COLORS[color]
c.nil? ? nil : "3#{c}"
end
end
def tablized_virt v
ha = v.respond_to?( :ha) ? v.ha : nil
unknown = TablizedOutput::V.new 0, '-'
node = v.node.is_a?(String) ? v.node : v.node.node
if color = @cfg[:hosts]&.[](node.to_sym)&.[](:color)
node = ColoredString.new node, fgcolor( color)
end
[
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, node,
v.respond_to?(:uptime) ? TablizedOutput::V.new( v.uptime, Measured.seconds( v.uptime)) : unknown,
v.respond_to?(:cpu) ? TablizedOutput::Percentage.new( v.cpu) : unknown,
v.respond_to?(:mem) ? TablizedOutput::V.new( v.mem, Measured.bytes( v.mem)) : unknown,
v.respond_to?(:maxmem) ? TablizedOutput::Percentage.new( v.mem/v.maxmem.to_f) : unknown,
v.respond_to?(:disk) ? TablizedOutput::V.new( v.disk.to_i, Measured.bytes( v.disk.to_i)) : unknown,
if v.respond_to?(:maxdisk) and 0 < v.maxdisk.to_i
TablizedOutput::Percentage.new( v.disk.to_f/v.maxdisk.to_f)
else unknown end,
]
end
end end

View file

@ -178,32 +178,4 @@ class TablizedOutput
}.join( "\e[3#{i.even? ? 6 : 3}m | \e[0m") }.join( "\e[3#{i.even? ? 6 : 3}m | \e[0m")
end end
end end
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
end end