1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2025-03-19 12:59:30 +00:00

improve table

This commit is contained in:
Adrien Waksberg 2016-10-20 10:24:04 +02:00
parent fdc1a6f3ec
commit f5746d6af6

View file

@ -143,22 +143,26 @@ class Cli
group = '.' group = '.'
i = 1 i = 1
length_total = 10 length_total = 10
length = { id: 3, data = { id: { length: 3, color: 'cyan' },
host: 10, host: { length: 9, color: 'yellow' },
user: 8, user: { length: 7, color: 'green' },
comment: 15, protocol: { length: 9, color: 'white' },
otp: 5, port: { length: 5, color: 'white' },
otp: { length: 4, color: 'white' },
comment: { length: 14, color: 'magenta' },
} }
items.each do |item| items.each do |item|
length[:host] = item.host.length + 3 if item.host.to_s.length > length[:host] data.each do |k, v|
length[:user] = item.user.length + 3 if item.user.to_s.length > length[:user] next if k == :id or k == :otp
length[:comment] = item.comment.length + 3 if item.comment.to_s.length > length[:comment]
v[:length] = item.send(k.to_s).length + 3 if item.send(k.to_s).to_s.length > v[:length]
end
end end
length[:id] = items.length.to_s.length + 2 if items.length.to_s.length > length[:id] data[:id][:length] = items.length.to_s.length + 2 if items.length.to_s.length > data[:id][:length]
length.each_value { |v| length_total += v } data.each_value { |v| length_total += v[:length] }
items.sort! { |a,b| a.group.to_s.downcase <=> b.group.to_s.downcase } items.sort! { |a, b| a.group.to_s.downcase <=> b.group.to_s.downcase }
items.each do |item| items.each do |item|
if group != item.group if group != item.group
@ -173,36 +177,39 @@ class Cli
print ' ' print ' '
length_total.times { print '=' } length_total.times { print '=' }
print "\n " print "\n "
print ' ID ' data.each do |k, v|
print '| Host' case k
(length[:host] - 'Host'.length).times { print ' ' } when :id
print '| User' print ' ID'
(length[:user] - 'User'.length).times { print ' ' } when :otp
print '| OTP ' print '| OTP'
print '| Comment' else
(length[:comment] - 'Comment'.length).times { print ' ' } print "| #{k.to_s.capitalize}"
end
(v[:length] - k.to_s.length).times { print ' ' }
end
print "\n " print "\n "
length_total.times { print '=' } length_total.times { print '=' }
print "\n" print "\n"
end end
print " #{i}".cyan print " #{i}".send(data[:id][:color])
(length[:id] - i.to_s.length).times { print ' ' } (data[:id][:length] - i.to_s.length).times { print ' ' }
print '| ' data.each do |k, v|
print "#{item.host}".yellow next if k == :id
(length[:host] - item.host.to_s.length).times { print ' ' }
print '| ' if k == :otp
print "#{item.user}".green print '| '
(length[:user] - item.user.to_s.length).times { print ' ' } if item.otp; print ' X ' else 4.times { print ' ' } end
print '| '
if item.otp next
print ' X ' end
else
4.times { print ' ' } print '| '
print "#{item.send(k.to_s)}".send(v[:color])
(v[:length] - item.send(k.to_s).to_s.length).times { print ' ' }
end end
print '| '
print "#{item.comment}".magenta
(length[:comment] - item.comment.to_s.length).times { print ' ' }
print "\n" print "\n"
i += 1 i += 1