forked from davidscholberg/goblocks
/
uptime.go
51 lines (48 loc) · 1.08 KB
/
uptime.go
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package modules
import (
"fmt"
"github.com/davidscholberg/go-durationfmt"
"github.com/davidscholberg/go-i3barjson"
"os"
"time"
)
// Uptime represents the configuration for the time display block.
type Uptime struct {
BlockConfigBase `yaml:",inline"`
DurationFormat string `yaml:"duration_format"`
}
// UpdateBlock updates the time display block.
func (c Uptime) UpdateBlock(b *i3barjson.Block) {
b.Color = c.Color
fullTextFmt := fmt.Sprintf("%s%%s", c.Label)
var load float64
r, err := os.Open("/proc/uptime")
if err != nil {
b.Urgent = true
b.FullText = fmt.Sprintf(fullTextFmt, err.Error())
return
}
defer r.Close()
_, err = fmt.Fscanf(r, "%f ", &load)
if err != nil {
b.Urgent = true
b.FullText = fmt.Sprintf(fullTextFmt, err.Error())
return
}
dur := time.Duration(load) * time.Second
durFmt := c.DurationFormat
if durFmt == "" {
durFmt = "%hh%mm%ss"
}
durStr, err := durationfmt.Format(dur, durFmt)
if err != nil {
b.Urgent = true
b.FullText = fmt.Sprintf(fullTextFmt, err.Error())
return
}
b.FullText = fmt.Sprintf(
"%s%s",
c.Label,
durStr,
)
}