xmtop/widgets/disk.go

38 lines
665 B
Go
Raw Normal View History

2018-02-19 15:25:02 +08:00
package widgets
import (
"fmt"
"time"
ui "github.com/cjbassi/gotop/termui"
"github.com/cjbassi/gotop/utils"
ps "github.com/shirou/gopsutil/disk"
)
type Disk struct {
*ui.Gauge
fs string // which filesystem to get the disk usage of
interval time.Duration
}
func NewDisk() *Disk {
d := &Disk{ui.NewGauge(), "/", time.Second * 5}
d.Label = "Disk Usage"
go d.update()
ticker := time.NewTicker(d.interval)
go func() {
for range ticker.C {
d.update()
}
}()
return d
}
func (d *Disk) update() {
disk, _ := ps.Usage(d.fs)
d.Percent = int(disk.UsedPercent)
2018-02-23 15:58:48 +08:00
d.Description = fmt.Sprintf(" (%dGB free)", int(utils.BytesToGB(disk.Free)))
2018-02-19 15:25:02 +08:00
}