Shell
godocimport "barista.run/modules/shell"
Show the output of a shell command on the bar: shell.New("sh", "-c", "whoami")
.
Show the last line of output from a long-running shell command: shell.Tail("dmesg")
.
Configuration
-
Output(func(string) bar.Output)
: Sets the output format. ForNew()
, this function is given the complete, trimmed output from the command. ForTail()
it is given a single line from the output. -
Every(time.Duration)
: Only forNew()
, sets the interval at which the command should be repeated. If not set, or set to a zero duration, disables automatic refresh. -
Refresh
: Refreshes the output. ForNew()
, also re-runs the command. ForTail()
re-uses the last line of output. Can be used if the output format has a time, or in click handlers, e.g.s.Output(func (in string) bar.Output { return outputs.Text(in).OnClick(ifLeft(s.Refresh)) })
Examples
Show a count of chrome processes, refreshed every second:
chromeCount := shell.New("pgrep", "-c", "chrome").
Every(time.Second).
Output(func(count string) bar.Output {
return outputs.Textf("%s chrome procs", count)
})
Show the last line of dmesg output, with human readable time deltas:
var dmesgFormat = regexp.MustCompile(`^\[([0-9\.]+)\] (.*)$`)
dmesg := shell.Tail("dmesg", "-w").Output(func(line string) bar.Output {
res := dmesgFormat.FindStringSubmatch(line) // res[1] = time, res[2] = message
timeOfMsg, _ := strconv.ParseFloat(res[1], 64)
outLine := res[2]
if len(outLine) > 20 {
outLine = outLine[0:19] + "…"
}
return outputs.Repeat(func(time.Time) bar.Output {
uptimeStr, _ := ioutil.ReadFile("/proc/uptime") // uptimeStr = "$uptime $cputime"
timeNow, _ := strconv.ParseFloat(strings.Split(string(uptimeStr), " ")[0], 64)
delta := time.Duration(uint64(timeNow-timeOfMsg)) * time.Second
return outputs.Textf("(%v ago) %s", delta, outLine)
}).Every(time.Second)
})