From 3b63a3af2468ee5b51b1d17d1460b30e1236c85d Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Wed, 13 Nov 2024 15:29:37 -0600 Subject: [PATCH] better formatting Signed-off-by: Jeff Carr --- human.go | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/human.go b/human.go index e1fffef..901c6b2 100644 --- a/human.go +++ b/human.go @@ -3,7 +3,7 @@ package virtbuf // mostly just functions related to making STDOUT // more readable by us humans -// also function shortcuts the do limited formatting (haha, who remembers COBOL?) +// also function shortcuts the do fixed limited formatting (it's like COBOL) // so reporting tables of the status of what droplets and hypervisors // are in text columns and rows that can be easily read in a terminal @@ -14,6 +14,8 @@ import ( "strings" "time" + "google.golang.org/protobuf/types/known/timestamppb" + "go.wit.com/log" ) @@ -146,34 +148,26 @@ func (d *Droplet) SprintDumpHeader() string { macs = append(macs, n.Mac) } - // this line in golang could replace 80 lines of COBOL header := fmt.Sprintf("%-4.4s%20s %-8s", d.Current.State, strings.Join(macs, " "), d.Current.Hypervisor) if d.Current == nil { - d.Current = new(Current) + return header } + if d.Current.OnSince == nil { + d.Current.OnSince = timestamppb.New(time.Now()) + } + + t := time.Since(d.Current.OnSince.AsTime()) // time since 'ON' + dur := FormatDuration(t) + switch d.Current.State { case DropletState_ON: - var dur string - if d.Current.OnSince != nil { - dur = "" - } else { - t := time.Since(d.Current.OnSince.AsTime()) // time since 'ON' - dur = FormatDuration(t) - } header += fmt.Sprintf(" (on :%3s)", dur) case DropletState_OFF: - var dur string - if d.Current.OffSince != nil { - dur = "" - } else { - t := time.Since(d.Current.OffSince.AsTime()) // time since 'OFF' - dur = FormatDuration(t) - } header += fmt.Sprintf(" (off:%3s)", dur) default: - header += fmt.Sprintf(" (?? :%3s)", "") + header += fmt.Sprintf(" (?? :%3s)", dur) } return header }