From 45a252f2d9b58efac4f148e9c52821d3fcbbcdd1 Mon Sep 17 00:00:00 2001 From: Caleb Bassi Date: Thu, 13 Dec 2018 13:55:58 -0800 Subject: [PATCH] Revert changes to 'ps' on Darwin --- src/widgets/proc_darwin.go | 55 +++++++++++++++++++++ src/widgets/{proc_unix.go => proc_linux.go} | 2 - 2 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 src/widgets/proc_darwin.go rename src/widgets/{proc_unix.go => proc_linux.go} (97%) diff --git a/src/widgets/proc_darwin.go b/src/widgets/proc_darwin.go new file mode 100644 index 0000000..7fce6b0 --- /dev/null +++ b/src/widgets/proc_darwin.go @@ -0,0 +1,55 @@ +package widgets + +import ( + "log" + "os/exec" + "strconv" + "strings" +) + +func (self *Proc) update() { + processes := Processes() + // have to iterate like this in order to actually change the value + for i := range processes { + processes[i].CPU /= self.cpuCount + } + + self.ungroupedProcs = processes + self.groupedProcs = Group(processes) + + self.Sort() +} + +func Processes() []Process { + output, err := exec.Command("ps", "-axo", "pid,comm,pcpu,pmem,args").Output() + if err != nil { + log.Printf("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]) + if err != nil { + log.Printf("failed to convert first field to int: %v. split: %v", err, split) + } + cpu, err := strconv.ParseFloat(split[2], 64) + if err != nil { + log.Printf("failed to convert third field to float: %v. split: %v", err, split) + } + mem, err := strconv.ParseFloat(split[3], 64) + if err != nil { + log.Printf("failed to convert fourth field to float: %v. split: %v", err, split) + } + process := Process{ + PID: pid, + Command: split[1], + CPU: cpu, + Mem: mem, + Args: strings.Join(split[4:], " "), + } + processes = append(processes, process) + } + return processes +} diff --git a/src/widgets/proc_unix.go b/src/widgets/proc_linux.go similarity index 97% rename from src/widgets/proc_unix.go rename to src/widgets/proc_linux.go index 6f8a958..dd3cae5 100644 --- a/src/widgets/proc_unix.go +++ b/src/widgets/proc_linux.go @@ -1,5 +1,3 @@ -// +build freebsd linux darwin - package widgets import (