X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=lib%2Fcommon.go;h=3cee791f13c664935d181265a71dee3d0f30492a;hb=c5eb0ee3c462a88cd96a8095063aae6117fce992;hp=1f21c7f921248203f0c9cf6c4a86386652b2e847;hpb=ec6cd477f09b69398f512cbe480afa86da326370;p=pstop.git diff --git a/lib/common.go b/lib/common.go index 1f21c7f..3cee791 100644 --- a/lib/common.go +++ b/lib/common.go @@ -11,6 +11,9 @@ import ( const ( myname = "pstop" copyright = "Copyright (C) 2014 Simon J Mudd " + i_1024_2 = 1024 * 1024 + i_1024_3 = 1024 * 1024 * 1024 + i_1024_4 = 1024 * 1024 * 1024 * 1024 ) // myround converts this floating value to the right width etc. @@ -40,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 @@ -103,15 +115,15 @@ func FormatAmount(amount uint64) string { return strconv.Itoa(int(amount)) } - if amount > (1024 * 1024 * 1024 * 1024) { + if amount > i_1024_4 { suffix = "P" - decimal_amount = float64(amount) / 1024 / 1024 / 1024 / 1024 - } else if amount > (1024 * 1024 * 1024) { + decimal_amount = float64(amount) / i_1024_4 + } else if amount > i_1024_3 { suffix = "G" - decimal_amount = float64(amount) / 1024 / 1024 / 1024 - } else if amount > (1024 * 1024) { + decimal_amount = float64(amount) / i_1024_3 + } else if amount > i_1024_2 { suffix = "M" - decimal_amount = float64(amount) / 1024 / 1024 + decimal_amount = float64(amount) / i_1024_2 } else if amount > 1024 { suffix = "k" decimal_amount = float64(amount) / 1024 @@ -125,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 {