X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=lib%2Fcommon.go;h=20c173c8abeaa7fd6dfa83f343c4151c0fa128f8;hb=10dbb38516c6ac149373ac0fae917a34ab7a9afa;hp=9d31075e5af5dabc2de6eb722ff9537e9f2b2fdf;hpb=84f9a5c41e806461c4193db0c7a0ef652b1b1357;p=pstop.git diff --git a/lib/common.go b/lib/common.go index 9d31075..20c173c 100644 --- a/lib/common.go +++ b/lib/common.go @@ -10,7 +10,7 @@ import ( const ( myname = "pstop" - copyright = "Copyright (C) 2014 Simon J Mudd " + copyright = "Copyright (C) 2014-2015 Simon J Mudd " i_1024_2 = 1024 * 1024 i_1024_3 = 1024 * 1024 * 1024 i_1024_4 = 1024 * 1024 * 1024 * 1024 @@ -43,6 +43,15 @@ func sec_to_time(d uint64) string { return fmt.Sprintf("%02d:%02d:%02d", hours, minutes, seconds) } +// similar to sec_to_time() spaces if 0 and takes seconds as input. +func FormatSeconds(seconds uint64) string { + if seconds == 0 { + return " " + } else { + return sec_to_time(seconds) + } +} + // FormatTime is based on sys.format_time. It // formats to 10 characters including space and suffix. // All values have 2 decimal places. Zero is returned as @@ -128,6 +137,17 @@ func FormatAmount(amount uint64) string { return formatted } +// like Amount but tigher in space +func FormatCounter(counter int, width int) string { + if counter == 0 { + pattern := "%" + fmt.Sprintf("%d", width) + "s" + return fmt.Sprintf(pattern, " ") + } else { + pattern := "%" + fmt.Sprintf("%d", width) + "d" + return fmt.Sprintf(pattern, counter) + } +} + // MyDivide() divides a by b except if b is 0 in which case we return 0. func MyDivide(a uint64, b uint64) float64 { if b == 0 {