feat: ctrl-c & esc abort filtering

This commit is contained in:
Christian Muehlhaeuser 2022-07-31 03:10:36 +02:00 committed by Maas Lalani
parent fd788a4dd9
commit 6538d726d1
3 changed files with 13 additions and 2 deletions

View File

@ -44,6 +44,9 @@ func (o Options) Run() error {
tm, err := p.StartReturningModel()
m := tm.(model)
if m.aborted {
return fmt.Errorf("filter aborted")
}
if len(m.matches) > m.selected && m.selected >= 0 {
fmt.Println(m.matches[m.selected].Str)
}

View File

@ -28,6 +28,7 @@ type model struct {
selected int
indicator string
height int
aborted bool
quitting bool
matchStyle lipgloss.Style
textStyle lipgloss.Style
@ -90,7 +91,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.height = msg.Height
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "esc", "enter":
case "ctrl+c", "esc":
m.aborted = true
fallthrough
case "enter":
m.quitting = true
return m, tea.Quit
case "ctrl+n", "ctrl+j", "down":

View File

@ -2,6 +2,7 @@ package main
import (
"fmt"
"os"
"runtime/debug"
"github.com/alecthomas/kong"
@ -54,5 +55,8 @@ func main() {
"defaultUnderline": "false",
},
)
_ = ctx.Run()
if err := ctx.Run(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}