Skip to content

Commit

Permalink
fix: make uptime precision configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
jon4hz committed Jan 21, 2024
1 parent 17c6c88 commit e84ca15
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
3 changes: 2 additions & 1 deletion config/config_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ type Uptime struct {
}

type SysInfo struct {
Disabled bool `mapstructure:"disabled"`
Disabled bool `mapstructure:"disabled"`
UptimePrecision int `mapstructure:"uptime_precision"`
}

type Zpool struct {
Expand Down
6 changes: 5 additions & 1 deletion section/sysinfo/sysinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func (Section) Enabled(c *context.Context) bool {
return !c.Config.SysInfo.Disabled
}

func (Section) Default(ctx *context.Context) {
ctx.Config.SysInfo.UptimePrecision = 3
}

func (Section) Gather(c *context.Context) error {
s, err := host.InfoWithContext(c)
if err != nil {
Expand Down Expand Up @@ -113,7 +117,7 @@ func (Section) Print(ctx *context.Context) string {
style("Distro")+": "+c.Platform,
style("Kernel")+": "+c.Kernel,
"",
style("Uptime")+": "+durafmt.Parse(c.Uptime).LimitFirstN(3).String(),
style("Uptime")+": "+durafmt.Parse(c.Uptime).LimitFirstN(ctx.Config.SysInfo.UptimePrecision).String(),
style("Load")+":"+fmt.Sprintf(" %s (1m), %s (5m), %s (15m)", greenF(c.Load.Load1), greenF(c.Load.Load5), greenF(c.Load.Load15)),
style("Processes")+":"+fmt.Sprintf(" %s (root), %s (user), %s (total)", green(c.RootProcs), green(c.UserProcs), green(c.RootProcs+c.UserProcs)),
"",
Expand Down
6 changes: 5 additions & 1 deletion section/uptime/uptime.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ func (Section) Enabled(c *context.Context) bool {
return !c.Config.Uptime.Disabled
}

func (Section) Default(ctx *context.Context) {
ctx.Config.Uptime.Precision = 3
}

func (Section) Gather(c *context.Context) error {
t, err := host.BootTimeWithContext(c)
if err != nil {
Expand All @@ -33,5 +37,5 @@ func (Section) Print(ctx *context.Context) string {
if c == nil {
return ""
}
return "uptime: " + durafmt.Parse(c.Uptime).LimitFirstN(3).String()
return "uptime: " + durafmt.Parse(c.Uptime).LimitFirstN(ctx.Config.Uptime.Precision).String()
}

0 comments on commit e84ca15

Please sign in to comment.