From f91531b856e7b1299851331ec204c08c0da92200 Mon Sep 17 00:00:00 2001 From: Adrien Waksberg Date: Sat, 6 May 2017 09:09:17 +0200 Subject: [PATCH] feat: replace host by url in table --- lib/mpw/cli.rb | 33 +++++++++++++++++++++------------ lib/mpw/item.rb | 11 +++++++++++ 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/lib/mpw/cli.rb b/lib/mpw/cli.rb index e3cb85e..023b9b2 100644 --- a/lib/mpw/cli.rb +++ b/lib/mpw/cli.rb @@ -179,16 +179,19 @@ module MPW data = { id: { length: 3, color: 'cyan' }, host: { length: 9, color: 'yellow' }, user: { length: 7, color: 'green' }, - protocol: { length: 9, color: 'white' }, - port: { length: 5, color: 'white' }, otp: { length: 4, color: 'white' }, comment: { length: 14, color: 'magenta' } } items.each do |item| data.each do |k, v| - next if k == :id || k == :otp - - v[:length] = item.send(k.to_s).to_s.length + 3 if item.send(k.to_s).to_s.length >= v[:length] + case k + when :id, :otp + next + when :host + v[:length] = item.url.length + 3 if item.url.length >= v[:length] + else + v[:length] = item.send(k.to_s).to_s.length + 3 if item.send(k.to_s).to_s.length >= v[:length] + end end end data[:id][:length] = items.length.to_s.length + 2 if items.length.to_s.length > data[:id][:length] @@ -231,16 +234,22 @@ module MPW data.each do |k, v| next if k == :id - if k == :otp - print '| ' + print '| ' + + case k + when :otp item.otp ? (print ' X ') : 4.times { print ' ' } - next - end + when :host + print "#{item.protocol}://" if item.protocol + print item.host.send(v[:color]) + print ":#{item.port}" if item.port + (v[:length] - item.url.to_s.length).times { print ' ' } - print '| ' - print item.send(k.to_s).to_s.send(v[:color]) - (v[:length] - item.send(k.to_s).to_s.length).times { print ' ' } + else + print item.send(k.to_s).to_s.send(v[:color]) + (v[:length] - item.send(k.to_s).to_s.length).times { print ' ' } + end end print "\n" diff --git a/lib/mpw/item.rb b/lib/mpw/item.rb index ba86b96..be03dba 100644 --- a/lib/mpw/item.rb +++ b/lib/mpw/item.rb @@ -81,6 +81,17 @@ module MPW @last_edit = nil end + # Return data on url format + # @return [String] an url + def url + url = '' + url += "#{@protocol}://" if @protocol + url += @host + url += ":#{@port}" if @port + + url + end + def empty? @id.to_s.empty? end