From 2212a36605d2dad6892aab10bc1ca37eaed27e91 Mon Sep 17 00:00:00 2001 From: LuciferInLove Date: Fri, 5 Jun 2020 00:35:58 +0300 Subject: [PATCH 1/5] Adding key for exit --- select.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/select.go b/select.go index 15dfc41..f1cb884 100644 --- a/select.go +++ b/select.go @@ -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. @@ -273,6 +276,12 @@ func (s *Select) innerRun(cursorPos, scroll int, top rune) (int, string, error) } else { searchMode = true } + case key == s.Keys.Exit.Code: + clearScreen(sb) + rl.Write([]byte(showCursor)) + rl.Clean() + rl.Close() + os.Exit(0) case key == KeyBackspace: if !canSearch || !searchMode { break @@ -578,6 +587,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"}, } } From 296b6400035a4ee96b1fe02a1ebdced9e5c3d7b1 Mon Sep 17 00:00:00 2001 From: LuciferInLove Date: Fri, 5 Jun 2020 00:40:10 +0300 Subject: [PATCH 2/5] Adding key for exit --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 760a44d..5364550 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/manifoldco/promptui +module github.com/LuciferInLove/promptui go 1.12 From d8893f35f691a6234b4a1befd9da371b7394e53e Mon Sep 17 00:00:00 2001 From: LuciferInLove Date: Fri, 5 Jun 2020 00:58:15 +0300 Subject: [PATCH 3/5] Adding key for exit --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 5364550..760a44d 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/LuciferInLove/promptui +module github.com/manifoldco/promptui go 1.12 From c0ea6b6ac2bfaae7d42fa09f618e4cf20d7037cb Mon Sep 17 00:00:00 2001 From: LuciferInLove Date: Wed, 8 Jul 2020 04:26:29 +0300 Subject: [PATCH 4/5] Adding optional value that can be returned on exit --- select.go | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/select.go b/select.go index f1cb884..0c04e5f 100644 --- a/select.go +++ b/select.go @@ -18,6 +18,9 @@ import ( // SelectWithAdd's logic. const SelectedAdd = -1 +// ExitCode is default exit code. It will be returned on application exit. +const ExitCode = 0 + // Select represents a list of items used to enable selections, they can be used as search engines, menus // or as a list of items in a cli based prompt. type Select struct { @@ -78,6 +81,10 @@ type Select struct { // A function that determines how to render the cursor Pointer Pointer + // A value that can de returned as result of Run() on ExitKey pressing. + // If not defined, application will be closed on ExitKey pressing. + ExitValue string + Stdin io.ReadCloser Stdout io.WriteCloser } @@ -223,6 +230,8 @@ func (s *Select) RunCursorAt(cursorPos, scroll int) (int, string, error) { } func (s *Select) innerRun(cursorPos, scroll int, top rune) (int, string, error) { + isExit := bool(false) + c := &readline.Config{ Stdin: s.Stdin, Stdout: s.Stdout, @@ -277,11 +286,7 @@ func (s *Select) innerRun(cursorPos, scroll int, top rune) (int, string, error) searchMode = true } case key == s.Keys.Exit.Code: - clearScreen(sb) - rl.Write([]byte(showCursor)) - rl.Clean() - rl.Close() - os.Exit(0) + isExit = true case key == KeyBackspace: if !canSearch || !searchMode { break @@ -304,6 +309,10 @@ func (s *Select) innerRun(cursorPos, scroll int, top rune) (int, string, error) } } + if isExit { + closeReadlineInstance(rl) + } + if searchMode { header := SearchPrompt + cur.Format() sb.WriteString(header) @@ -365,6 +374,11 @@ func (s *Select) innerRun(cursorPos, scroll int, top rune) (int, string, error) for { _, err = rl.Readline() + if isExit { + err = nil + break + } + if err != nil { switch { case err == readline.ErrInterrupt, err.Error() == "Interrupt": @@ -394,6 +408,14 @@ func (s *Select) innerRun(cursorPos, scroll int, top rune) (int, string, error) return 0, "", err } + if isExit { + clearScreen(sb) + if s.ExitValue != "" { + return s.list.Index(), fmt.Sprintf("%v", s.ExitValue), err + } + os.Exit(ExitCode) + } + items, idx := s.list.Items() item := items[idx] @@ -522,6 +544,9 @@ type SelectWithAdd struct { // a function that defines how to render the cursor Pointer Pointer + // A value that can de returned on exit as result of Run() + ExitValue string + // HideHelp sets whether to hide help information. HideHelp bool } @@ -550,6 +575,7 @@ func (sa *SelectWithAdd) Run() (int, string, error) { Size: 5, list: list, Pointer: sa.Pointer, + ExitValue: sa.ExitValue, } s.setKeys() @@ -645,3 +671,9 @@ func clearScreen(sb *screenbuf.ScreenBuf) { sb.Clear() sb.Flush() } + +func closeReadlineInstance(rl *readline.Instance) { + rl.Write([]byte(showCursor)) + rl.Clean() + rl.Close() +} From 7b2707b82ee720d377d28997337701dbb746d79b Mon Sep 17 00:00:00 2001 From: LuciferInLove Date: Thu, 30 Jul 2020 15:50:09 +0300 Subject: [PATCH 5/5] Fix exit in search mode --- select.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/select.go b/select.go index 0c04e5f..55bf3ac 100644 --- a/select.go +++ b/select.go @@ -285,7 +285,7 @@ func (s *Select) innerRun(cursorPos, scroll int, top rune) (int, string, error) } else { searchMode = true } - case key == s.Keys.Exit.Code: + case key == s.Keys.Exit.Code && !searchMode: isExit = true case key == KeyBackspace: if !canSearch || !searchMode {