summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCaleb Bassi <calebjbassi@gmail.com>2019-02-10 21:38:20 -0800
committerGitHub <noreply@github.com>2019-02-10 21:38:20 -0800
commit52590931d20551edbe9d4521db2e790fda9f4433 (patch)
tree72888766f9056b23e37ab4aa172998dfb3e21114
parent4b6488e29559449e892d36dc25756cc0f620c1b5 (diff)
parentd6008391c3d4000a9f4ed046f2a5271add8a9f95 (diff)
Merge pull request #109 from whuang8/bug/show-chrome-processes
Fix inability to display command names with whitespace in macOS
-rw-r--r--src/widgets/proc_other.go29
1 files changed, 19 insertions, 10 deletions
diff --git a/src/widgets/proc_other.go b/src/widgets/proc_other.go
index f386a66..da0123c 100644
--- a/src/widgets/proc_other.go
+++ b/src/widgets/proc_other.go
@@ -10,6 +10,13 @@ import (
"strings"
)
+const (
+ // Define column widths for ps output used in Processes()
+ five = "12345"
+ ten = five + five
+ fifty = ten + ten + ten + ten + ten
+)
+
func (self *Proc) update() {
processes, err := Processes()
if err != nil {
@@ -29,33 +36,35 @@ func (self *Proc) update() {
}
func Processes() ([]Process, error) {
- output, err := exec.Command("ps", "-wwcaxo", "pid,comm,pcpu,pmem,args").Output()
+ keywords := fmt.Sprintf("pid=%s,comm=%s,pcpu=%s,pmem=%s,args", ten, fifty, five, five)
+ output, err := exec.Command("ps", "-caxo", keywords).Output()
if err != nil {
return nil, fmt.Errorf("failed to execute 'ps' command: %v", err)
}
+
// converts to []string and removes the header
strOutput := strings.Split(strings.TrimSpace(string(output)), "\n")[1:]
+
processes := []Process{}
for _, line := range strOutput {
- split := strings.Fields(line)
- pid, err := strconv.Atoi(split[0])
+ pid, err := strconv.Atoi(strings.TrimSpace(line[0:10]))
if err != nil {
- log.Printf("failed to convert first field to int: %v. split: %v", err, split)
+ log.Printf("failed to convert first field to int: %v. split: %v", err, line)
}
- cpu, err := strconv.ParseFloat(split[2], 64)
+ cpu, err := strconv.ParseFloat(strings.TrimSpace(line[63:68]), 64)
if err != nil {
- log.Printf("failed to convert third field to float: %v. split: %v", err, split)
+ log.Printf("failed to convert third field to float: %v. split: %v", err, line)
}
- mem, err := strconv.ParseFloat(split[3], 64)
+ mem, err := strconv.ParseFloat(strings.TrimSpace(line[69:74]), 64)
if err != nil {
- log.Printf("failed to convert fourth field to float: %v. split: %v", err, split)
+ log.Printf("failed to convert fourth field to float: %v. split: %v", err, line)
}
process := Process{
PID: pid,
- Command: split[1],
+ Command: strings.TrimSpace(line[11:61]),
CPU: cpu,
Mem: mem,
- Args: strings.Join(split[4:], " "),
+ Args: line[74:],
}
processes = append(processes, process)
}