xmtop/widgets/disk.go

39 lines
699 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"
2018-02-23 16:42:39 +08:00
disk "github.com/shirou/gopsutil/disk"
2018-02-19 15:25:02 +08:00
)
type Disk struct {
*ui.Gauge
fs string // which filesystem to get the disk usage of
interval time.Duration
}
func NewDisk() *Disk {
2018-02-23 16:42:39 +08:00
// get root filesystem usage
2018-02-19 15:25:02 +08:00
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() {
2018-02-23 16:42:39 +08:00
disk, _ := disk.Usage(d.fs)
2018-02-19 15:25:02 +08:00
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
}