Move filter event handling into proc widget.

This commit is contained in:
Brian Mattern 2019-06-03 16:02:22 -07:00
parent 32f88622c7
commit f3b451c887
2 changed files with 34 additions and 46 deletions

35
main.go
View File

@ -12,7 +12,6 @@ import (
"strconv"
"syscall"
"time"
"unicode/utf8"
docopt "github.com/docopt/docopt.go"
ui "github.com/gizak/termui/v3"
@ -274,36 +273,6 @@ func initWidgets() {
}
}
// handleEditFilterEvents handles events while editing the proc filter.
// Returns true if the event was handled.
func handleEditFilterEvents(e ui.Event) bool {
if utf8.RuneCountInString(e.ID) == 1 {
proc.SetFilter(proc.Filter() + e.ID)
ui.Render(proc)
return true
}
switch e.ID {
case "<C-c>", "<Escape>":
proc.SetFilter("")
proc.SetEditingFilter(false)
ui.Render(proc)
case "<Enter>":
proc.SetEditingFilter(false)
ui.Render(proc)
case "<Backspace>":
if filter := proc.Filter(); filter != "" {
proc.SetFilter(filter[:len(filter)-1])
}
ui.Render(proc)
case "<Space>":
proc.SetFilter(proc.Filter() + " ")
ui.Render(proc)
default:
return false
}
return true
}
func eventLoop() {
drawTicker := time.NewTicker(updateInterval).C
@ -327,8 +296,8 @@ func eventLoop() {
}
}
case e := <-uiEvents:
if proc.EditingFilter() && handleEditFilterEvents(e) {
if proc.HandleEvent(e) {
ui.Render(proc)
break
}
switch e.ID {

View File

@ -96,19 +96,6 @@ func NewProcWidget() *ProcWidget {
return self
}
func (self *ProcWidget) Filter() string {
return self.filter
}
func (self *ProcWidget) SetFilter(filter string) {
self.filter = filter
self.update()
}
func (self *ProcWidget) EditingFilter() bool {
return self.editingFilter
}
func (self *ProcWidget) SetEditingFilter(editing bool) {
self.editingFilter = editing
if !editing {
@ -116,6 +103,38 @@ func (self *ProcWidget) SetEditingFilter(editing bool) {
}
}
// handleEditFilterEvents handles events while editing the proc filter.
// Returns true if the event was handled.
func (self *ProcWidget) HandleEvent(e tui.Event) bool {
if !self.editingFilter {
return false
}
if utf8.RuneCountInString(e.ID) == 1 {
self.filter += e.ID
self.update()
return true
}
switch e.ID {
case "<C-c>", "<Escape>":
self.filter = ""
self.update()
self.SetEditingFilter(false)
case "<Enter>":
self.SetEditingFilter(false)
case "<Backspace>":
if self.filter != "" {
self.filter = self.filter[:len(self.filter)-1]
self.update()
}
case "<Space>":
self.filter += " "
self.update()
default:
return false
}
return true
}
func (self *ProcWidget) Draw(buf *tui.Buffer) {
self.Table.Draw(buf)
if self.filter != "" || self.editingFilter {