Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions select.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ type SelectKeys struct {

// Search is the key used to trigger the search mode for the list. Default to the "/" key.
Search Key

// Exit is the key used to correctly shutdown the process. Default to the "q" key.
Exit Key
}

// Key defines a keyboard code and a display representation for the help menu.
Expand Down Expand Up @@ -253,6 +256,8 @@ func (s *Select) innerRun(cursorPos, scroll int, top rune) (int, string, error)
s.list.SetCursor(cursorPos)
s.list.SetStart(scroll)

isExit := false

c.SetListener(func(line []rune, pos int, key rune) ([]rune, int, bool) {
switch {
case key == KeyEnter:
Expand All @@ -273,6 +278,10 @@ func (s *Select) innerRun(cursorPos, scroll int, top rune) (int, string, error)
} else {
searchMode = true
}
case key == s.Keys.Exit.Code && !searchMode:
isExit = true
closeReadlineInstance(rl)
return nil, 0, true
case key == KeyBackspace || key == KeyCtrlH:
if !canSearch || !searchMode {
break
Expand Down Expand Up @@ -355,6 +364,9 @@ func (s *Select) innerRun(cursorPos, scroll int, top rune) (int, string, error)

for {
_, err = rl.Readline()
if isExit {
err = readline.ErrInterrupt
}

if err != nil {
switch {
Expand Down Expand Up @@ -578,6 +590,7 @@ func (s *Select) setKeys() {
PageUp: Key{Code: KeyBackward, Display: KeyBackwardDisplay},
PageDown: Key{Code: KeyForward, Display: KeyForwardDisplay},
Search: Key{Code: '/', Display: "/"},
Exit: Key{Code: 'q', Display: "q"},
}
}

Expand Down Expand Up @@ -636,3 +649,9 @@ func clearScreen(sb *screenbuf.ScreenBuf) {
sb.Clear()
sb.Flush()
}

func closeReadlineInstance(rl *readline.Instance) {
rl.Write([]byte(showCursor))
rl.Clean()
rl.Close()
}