ctop/util.go

46 lines
690 B
Go
Raw Normal View History

2016-12-25 19:06:57 +00:00
package main
import (
2016-12-25 22:39:16 +00:00
"fmt"
2016-12-25 19:06:57 +00:00
"math"
2016-12-25 22:39:16 +00:00
"strconv"
2016-12-25 19:06:57 +00:00
ui "github.com/gizak/termui"
)
2016-12-25 22:39:16 +00:00
const (
kb = 1024
mb = kb * 1024
gb = mb * 1024
)
func byteFormat(n int64) string {
if n < kb {
return fmt.Sprintf("%sB", strconv.FormatInt(n, 10))
2016-12-25 19:06:57 +00:00
}
2016-12-25 22:39:16 +00:00
if n < mb {
n = n / kb
return fmt.Sprintf("%sK", strconv.FormatInt(n, 10))
2016-12-25 19:06:57 +00:00
}
2016-12-25 22:39:16 +00:00
if n < gb {
n = n / mb
return fmt.Sprintf("%sM", strconv.FormatInt(n, 10))
2016-12-25 19:06:57 +00:00
}
2016-12-25 22:39:16 +00:00
n = n / gb
return fmt.Sprintf("%sG", strconv.FormatInt(n, 10))
2016-12-25 19:06:57 +00:00
}
func round(num float64) int {
return int(num + math.Copysign(0.5, num))
}
func colorScale(n int) ui.Attribute {
if n > 70 {
return ui.ColorRed
}
if n > 30 {
return ui.ColorYellow
}
return ui.ColorGreen
}