Refactor filter editing input handling.

Only swallow events that are explicitly handled by the editor. Fall
through to the default event handling for all other events.

Also, cancel editing on <Escape> and fix README file formatting.
This commit is contained in:
Brian Mattern 2019-06-03 14:13:31 -07:00
parent 5941bec6c1
commit 2aea13bf0a
3 changed files with 53 additions and 46 deletions

View File

@ -73,7 +73,7 @@ snap connect gotop-cjbassi:system-observe
- Quit: `q` or `<C-c>`
- Process navigation:
- `k` and `<Up>`: up
- `j` and `<Down`: down
- `j` and `<Down>`: down
- `<C-u>`: half page up
- `<C-d>`: half page down
- `<C-b>`: full page up
@ -88,10 +88,10 @@ snap connect gotop-cjbassi:system-observe
- `m`: Mem
- `p`: PID
- Process filtering:
- /: start editing filter
- `/`: start editing filter
- (while editing):
- <Enter> accept filter
- <C-c>: clear filter
- `<Enter>` accept filter
- `<C-c>` and `<Escape>`: clear filter
- CPU and Mem graph scaling:
- `h`: scale in
- `l`: scale out

89
main.go
View File

@ -274,6 +274,33 @@ 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)
default:
return false
}
return true
}
func eventLoop() {
drawTicker := time.NewTicker(updateInterval).C
@ -297,8 +324,16 @@ func eventLoop() {
}
}
case e := <-uiEvents:
// Handle resize event always.
if e.ID == "<Resize>" {
if proc.EditingFilter() && handleEditFilterEvents(e) {
break
}
switch e.ID {
case "q", "<C-c>":
return
case "?":
helpVisible = !helpVisible
case "<Resize>":
payload := e.Payload.(ui.Resize)
termWidth, termHeight := payload.Width, payload.Height
if statusbar {
@ -309,55 +344,23 @@ func eventLoop() {
}
help.Resize(payload.Width, payload.Height)
ui.Clear()
if helpVisible {
ui.Render(help)
} else {
ui.Render(grid)
if statusbar {
ui.Render(bar)
}
}
}
if proc.EditingFilter() {
if utf8.RuneCountInString(e.ID) == 1 {
proc.SetFilter(proc.Filter() + e.ID)
ui.Render(proc)
}
if helpVisible {
switch e.ID {
case "<C-c>":
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)
}
} else if helpVisible {
switch e.ID {
case "q", "<C-c>":
return
case "?":
helpVisible = false
ui.Render(grid)
ui.Clear()
ui.Render(help)
case "<Escape>":
helpVisible = false
ui.Render(grid)
case "<Resize>":
ui.Render(help)
}
} else {
switch e.ID {
case "q", "<C-c>":
return
case "?":
helpVisible = true
ui.Clear()
ui.Render(help)
ui.Render(grid)
case "h":
graphHorizontalScale += graphHorizontalScaleDelta
cpu.HorizontalScale = graphHorizontalScale
@ -370,6 +373,11 @@ func eventLoop() {
mem.HorizontalScale = graphHorizontalScale
ui.Render(cpu, mem)
}
case "<Resize>":
ui.Render(grid)
if statusbar {
ui.Render(bar)
}
case "<MouseLeft>":
payload := e.Payload.(ui.Mouse)
proc.HandleClick(payload.X, payload.Y)
@ -414,7 +422,6 @@ func eventLoop() {
proc.ChangeProcSortMethod(w.ProcSortMethod(e.ID))
ui.Render(proc)
case "/":
proc.SetFilter("")
proc.SetEditingFilter(true)
ui.Render(proc)
}

View File

@ -33,7 +33,7 @@ Process filtering:
- /: start editing filter
- (while editing):
- <Enter>: accept filter
- <C-c> clear filter
- <C-c> and <Escape>: clear filter
CPU and Mem graph scaling:
- h: scale in