diff options
author | Sean E. Russell <seanerussell@gmail.com> | 2020-02-13 10:15:52 -0600 |
---|---|---|
committer | Sean E. Russell <seanerussell@gmail.com> | 2020-02-13 10:15:52 -0600 |
commit | 7e5c0c969c223973335c6fae5432411afc3fb060 (patch) | |
tree | 7f785ba4f692b81806209cb5f0c959e86efdba70 /widgets/proc_other.go | |
parent | 4bfe0251a8893ed08654d59b8a8b8182958e907f (diff) |
Fixes the directory structure.
Diffstat (limited to 'widgets/proc_other.go')
-rw-r--r-- | widgets/proc_other.go | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/widgets/proc_other.go b/widgets/proc_other.go new file mode 100644 index 0000000..c0d13e7 --- /dev/null +++ b/widgets/proc_other.go @@ -0,0 +1,57 @@ +// +build darwin openbsd + +package widgets + +import ( + "fmt" + "log" + "os/exec" + "strconv" + "strings" + + "github.com/cjbassi/gotop/utils" +) + +const ( + // Define column widths for ps output used in Procs() + five = "12345" + ten = five + five + fifty = ten + ten + ten + ten + ten +) + +func getProcs() ([]Proc, error) { + 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 + linesOfProcStrings := strings.Split(strings.TrimSpace(string(output)), "\n")[1:] + + procs := []Proc{} + for _, line := range linesOfProcStrings { + 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, line) + } + cpu, err := strconv.ParseFloat(utils.ConvertLocalizedString(strings.TrimSpace(line[63:68])), 64) + if err != nil { + log.Printf("failed to convert third field to float: %v. split: %v", err, line) + } + mem, err := strconv.ParseFloat(utils.ConvertLocalizedString(strings.TrimSpace(line[69:74])), 64) + if err != nil { + log.Printf("failed to convert fourth field to float: %v. split: %v", err, line) + } + proc := Proc{ + Pid: pid, + CommandName: strings.TrimSpace(line[11:61]), + Cpu: cpu, + Mem: mem, + FullCommand: line[74:], + } + procs = append(procs, proc) + } + + return procs, nil +} |