From 6d3595f7f7fcffcf3d329565b3fde54ec7e49524 Mon Sep 17 00:00:00 2001 From: kqzz <48300938+Kqzz@users.noreply.github.com> Date: Sat, 19 Feb 2022 22:02:36 -0500 Subject: [PATCH] feat: add sorting by cmd --- cmd/gotop/main.go | 2 +- widgets/proc.go | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/cmd/gotop/main.go b/cmd/gotop/main.go index 272eecc..cd8293f 100644 --- a/cmd/gotop/main.go +++ b/cmd/gotop/main.go @@ -353,7 +353,7 @@ func eventLoop(c gotop.Config, grid *layout.MyGrid) { grid.Proc.ToggleShowingGroupedProcs() ui.Render(grid.Proc) } - case "m", "c", "p": + case "m", "c", "n", "p": if grid.Proc != nil { grid.Proc.ChangeProcSortMethod(w.ProcSortMethod(e.ID)) ui.Render(grid.Proc) diff --git a/widgets/proc.go b/widgets/proc.go index 408906c..d332a15 100644 --- a/widgets/proc.go +++ b/widgets/proc.go @@ -25,6 +25,7 @@ const ( ProcSortCPU ProcSortMethod = "c" ProcSortMem = "m" ProcSortPid = "p" + ProcSortCmd = "n" ) type Proc struct { @@ -188,6 +189,9 @@ func (proc *ProcWidget) sortProcs() { case ProcSortMem: sort.Sort(sort.Reverse(SortProcsByMem(*procs))) proc.Header[3] += _downArrow + case ProcSortCmd: + sort.Sort(sort.Reverse(SortProcsByCmd(*procs))) + proc.Header[1] += _downArrow } } @@ -336,3 +340,20 @@ func (procs SortProcsByMem) Swap(i, j int) { func (procs SortProcsByMem) Less(i, j int) bool { return procs[i].Mem < procs[j].Mem } + +type SortProcsByCmd []Proc + +// Len implements Sort interface +func (procs SortProcsByCmd) Len() int { + return len(procs) +} + +// Swap implements Sort interface +func (procs SortProcsByCmd) Swap(i, j int) { + procs[i], procs[j] = procs[j], procs[i] +} + +// Less implements Sort interface +func (procs SortProcsByCmd) Less(i, j int) bool { + return strings.ToLower(procs[j].CommandName) < strings.ToLower(procs[i].CommandName) +}