feat: add sorting by cmd

This commit is contained in:
kqzz 2022-02-19 22:02:36 -05:00
parent b08c67b752
commit 6d3595f7f7
2 changed files with 22 additions and 1 deletions

View File

@ -353,7 +353,7 @@ func eventLoop(c gotop.Config, grid *layout.MyGrid) {
grid.Proc.ToggleShowingGroupedProcs() grid.Proc.ToggleShowingGroupedProcs()
ui.Render(grid.Proc) ui.Render(grid.Proc)
} }
case "m", "c", "p": case "m", "c", "n", "p":
if grid.Proc != nil { if grid.Proc != nil {
grid.Proc.ChangeProcSortMethod(w.ProcSortMethod(e.ID)) grid.Proc.ChangeProcSortMethod(w.ProcSortMethod(e.ID))
ui.Render(grid.Proc) ui.Render(grid.Proc)

View File

@ -25,6 +25,7 @@ const (
ProcSortCPU ProcSortMethod = "c" ProcSortCPU ProcSortMethod = "c"
ProcSortMem = "m" ProcSortMem = "m"
ProcSortPid = "p" ProcSortPid = "p"
ProcSortCmd = "n"
) )
type Proc struct { type Proc struct {
@ -188,6 +189,9 @@ func (proc *ProcWidget) sortProcs() {
case ProcSortMem: case ProcSortMem:
sort.Sort(sort.Reverse(SortProcsByMem(*procs))) sort.Sort(sort.Reverse(SortProcsByMem(*procs)))
proc.Header[3] += _downArrow 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 { func (procs SortProcsByMem) Less(i, j int) bool {
return procs[i].Mem < procs[j].Mem 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)
}