diff options
author | Sean E. Russell <ser@ser1.net> | 2020-02-14 09:35:58 -0600 |
---|---|---|
committer | Sean E. Russell <ser@ser1.net> | 2020-02-14 09:35:58 -0600 |
commit | f850a47d91d413de40de219aa597ade41a05f757 (patch) | |
tree | fa15d4e8ddc62888788d6814e4ab3c010931d892 /widgets | |
parent | 64d4a81212a43a4b805b92963fd4da230b3ca313 (diff) | |
parent | 246ebfbff2c87f66218c42ea9490214006f2efc8 (diff) |
Merge remote-tracking branch 'rephorm/filter'
Diffstat (limited to 'widgets')
-rw-r--r-- | widgets/help.go | 18 | ||||
-rw-r--r-- | widgets/proc.go | 46 |
2 files changed, 56 insertions, 8 deletions
diff --git a/widgets/help.go b/widgets/help.go index 953de45..9f78dd0 100644 --- a/widgets/help.go +++ b/widgets/help.go @@ -10,7 +10,7 @@ import ( const KEYBINDS = ` Quit: q or <C-c> -Process navigation +Process navigation: - k and <Up>: up - j and <Down>: down - <C-u>: half page up @@ -26,11 +26,17 @@ Process actions: - d3: kill selected process or group of processes with SIGQUIT (3) - d9: kill selected process or group of processes with SIGKILL (9) -Process sorting +Process sorting: - c: CPU - m: Mem - p: PID +Process filtering: + - /: start editing filter + - (while editing): + - <Enter>: accept filter + - <C-c> and <Escape>: clear filter + CPU and Mem graph scaling: - h: scale in - l: scale out @@ -47,12 +53,8 @@ func NewHelpMenu() *HelpMenu { } func (self *HelpMenu) Resize(termWidth, termHeight int) { - var textWidth = 0 - for _, line := range strings.Split(KEYBINDS, "\n") { - textWidth = maxInt(len(line), textWidth) - } - textWidth += 2 - textHeight := 28 + textWidth := 53 + textHeight := strings.Count(KEYBINDS, "\n") + 1 x := (termWidth - textWidth) / 2 y := (termHeight - textHeight) / 2 diff --git a/widgets/proc.go b/widgets/proc.go index 58ad6dd..9fed067 100644 --- a/widgets/proc.go +++ b/widgets/proc.go @@ -6,10 +6,12 @@ import ( "os/exec" "sort" "strconv" + "strings" "time" psCPU "github.com/shirou/gopsutil/cpu" + tui "github.com/gizak/termui/v3" ui "github.com/xxxserxxx/gotop/termui" "github.com/xxxserxxx/gotop/utils" ) @@ -37,9 +39,11 @@ type Proc struct { type ProcWidget struct { *ui.Table + entry *ui.Entry cpuCount int updateInterval time.Duration sortMethod ProcSortMethod + filter string groupedProcs []Proc ungroupedProcs []Proc showGroupedProcs bool @@ -56,6 +60,16 @@ func NewProcWidget() *ProcWidget { cpuCount: cpuCount, sortMethod: ProcSortCpu, showGroupedProcs: true, + filter: "", + } + self.entry = &ui.Entry{ + Style: self.TitleStyle, + Label: " Filter: ", + Value: "", + UpdateCallback: func(val string) { + self.filter = val + self.update() + }, } self.Title = " Processes " self.ShowCursor = true @@ -86,6 +100,37 @@ func NewProcWidget() *ProcWidget { return self } +func (self *ProcWidget) SetEditingFilter(editing bool) { + self.entry.SetEditing(editing) +} + +func (self *ProcWidget) HandleEvent(e tui.Event) bool { + return self.entry.HandleEvent(e) +} + +func (self *ProcWidget) SetRect(x1, y1, x2, y2 int) { + self.Table.SetRect(x1, y1, x2, y2) + self.entry.SetRect(x1+2, y2-1, x2-2, y2) +} + +func (self *ProcWidget) Draw(buf *tui.Buffer) { + self.Table.Draw(buf) + self.entry.Draw(buf) +} + +func (self *ProcWidget) filterProcs(procs []Proc) []Proc { + if self.filter == "" { + return procs + } + var filtered []Proc + for _, proc := range procs { + if strings.Contains(proc.FullCommand, self.filter) || strings.Contains(fmt.Sprintf("%d", proc.Pid), self.filter) { + filtered = append(filtered, proc) + } + } + return filtered +} + func (self *ProcWidget) update() { procs, err := getProcs() if err != nil { @@ -98,6 +143,7 @@ func (self *ProcWidget) update() { procs[i].Cpu /= float64(self.cpuCount) } + procs = self.filterProcs(procs) self.ungroupedProcs = procs self.groupedProcs = groupProcs(procs) |