forked from justjanne/powerline-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment-jobs.go
40 lines (35 loc) · 891 Bytes
/
segment-jobs.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
package main
import (
"fmt"
pwl "github.com/justjanne/powerline-go/powerline"
"os"
"os/exec"
"strconv"
"strings"
)
func segmentJobs(p *powerline) []pwl.Segment {
nJobs := -1
ppid := os.Getppid()
if *p.args.Shell == "bash" {
pppidOut, _ := exec.Command("ps", "-p", strconv.Itoa(ppid), "-oppid=").Output()
pppid, _ := strconv.ParseInt(strings.TrimSpace(string(pppidOut)), 10, 64)
ppid = int(pppid)
}
out, _ := exec.Command("ps", "-oppid=").Output()
processes := strings.Split(string(out), "\n")
for _, processPpidStr := range processes {
processPpid, _ := strconv.ParseInt(strings.TrimSpace(processPpidStr), 10, 64)
if int(processPpid) == ppid {
nJobs++
}
}
if nJobs > 0 {
return []pwl.Segment{{
Name: "jobs",
Content: fmt.Sprintf("%d", nJobs),
Foreground: p.theme.JobsFg,
Background: p.theme.JobsBg,
}}
}
return []pwl.Segment{}
}