summaryrefslogtreecommitdiffstats
path: root/widgets/disk.go
blob: ee8581c9d49bb2c312b3bb90b8187b9c05569616 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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)
	d.Description = fmt.Sprintf(" (%dGB free)", utils.BytesToGB(disk.Free))
}