ctop/cwidgets/compact/util.go

56 lines
875 B
Go
Raw Normal View History

package compact
// Common helper functions
import (
"fmt"
2018-10-25 20:17:53 +00:00
ui "github.com/gizak/termui"
)
2017-03-06 00:15:32 +00:00
const colSpacing = 1
2017-03-12 09:58:56 +00:00
// per-column width. 0 == auto width
var colWidths = []int{
2019-06-22 18:42:48 +00:00
5, // status
2017-03-12 09:58:56 +00:00
0, // name
0, // cid
0, // cpu
0, // memory
0, // net
0, // io
4, // pids
}
// Calculate per-column width, given total width
func calcWidth(width int) int {
spacing := colSpacing * len(colWidths)
var staticCols int
for _, w := range colWidths {
width -= w
if w == 0 {
2018-10-25 20:17:53 +00:00
staticCols++
2017-03-12 09:58:56 +00:00
}
}
return (width - spacing) / staticCols
}
func centerParText(p *ui.Par) {
var text string
var padding string
// strip existing left-padding
for i, ch := range p.Text {
if string(ch) != " " {
text = p.Text[i:]
break
}
}
padlen := (p.InnerWidth() - len(text)) / 2
for i := 0; i < padlen; i++ {
padding += " "
}
p.Text = fmt.Sprintf("%s%s", padding, text)
}