xmtop/src/widgets/disk.go

43 lines
742 B
Go
Raw Normal View History

2018-02-19 15:25:02 +08:00
package widgets
import (
"fmt"
"time"
2018-05-11 12:23:20 +08:00
"github.com/cjbassi/gotop/src/utils"
2018-03-30 06:48:43 +08:00
ui "github.com/cjbassi/termui"
2018-03-04 09:05:52 +08:00
psDisk "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-03-28 05:27:23 +08:00
self := &Disk{
2018-03-04 09:05:52 +08:00
Gauge: ui.NewGauge(),
fs: "/",
interval: time.Second * 5,
}
2018-03-28 05:27:23 +08:00
self.Label = "Disk Usage"
2018-02-19 15:25:02 +08:00
2018-04-13 11:00:34 +08:00
self.update()
2018-03-28 05:27:23 +08:00
ticker := time.NewTicker(self.interval)
2018-02-19 15:25:02 +08:00
go func() {
for range ticker.C {
2018-03-28 05:27:23 +08:00
self.update()
2018-02-19 15:25:02 +08:00
}
}()
2018-03-28 05:27:23 +08:00
return self
2018-02-19 15:25:02 +08:00
}
2018-03-28 05:27:23 +08:00
func (self *Disk) update() {
usage, _ := psDisk.Usage(self.fs)
self.Percent = int(usage.UsedPercent)
self.Description = fmt.Sprintf(" (%dGB free)", int(utils.BytesToGB(usage.Free)))
2018-02-19 15:25:02 +08:00
}