From 202a46d72ed0d51ba6e09e92ba04d47c525e61d4 Mon Sep 17 00:00:00 2001 From: pushaowei Date: Wed, 11 Mar 2020 17:06:12 +0800 Subject: [PATCH 1/2] By adding a filter, ignore Ctrl-z --- functions.go | 15 +++++++++++++-- ishell.go | 15 ++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/functions.go b/functions.go index 886a5a1..f707f09 100644 --- a/functions.go +++ b/functions.go @@ -1,7 +1,8 @@ package ishell import ( - "os" + "github.com/abiosoft/readline" + "os" ) func exitFunc(c *Context) { @@ -35,7 +36,17 @@ func addDefaultFuncs(s *Shell) { Help: "clear the screen", Func: clearFunc, }) - s.Interrupt(interruptFunc) + s.Interrupt(interruptFunc) + s.FilterInput(filterInput) +} + +func filterInput(r rune) (rune, bool){ + switch r { + // block CtrlZ feature + case readline.CharCtrlZ: + return r, false + } + return r, true } func interruptFunc(c *Context, count int, line string) { diff --git a/ishell.go b/ishell.go index f440095..1aaf052 100644 --- a/ishell.go +++ b/ishell.go @@ -42,6 +42,7 @@ var ( type Shell struct { rootCmd *Cmd generic func(*Context) + filterInput func(rune) (rune, bool) interrupt func(*Context, int, string) interruptCount int eof func(*Context) @@ -158,7 +159,8 @@ func (s *Shell) prepareRun() { if !s.customCompleter { s.initCompleters() } - s.activeMutex.Lock() + s.initFilterFunc() + s.activeMutex.Lock() s.active = true s.activeMutex.Unlock() @@ -374,6 +376,12 @@ func (s *Shell) CustomCompleter(completer readline.AutoCompleter) { s.setCompleter(completer) } +func (s *Shell) initFilterFunc() { + config := s.reader.scanner.Config.Clone() + config.FuncFilterInputRune = s.filterInput + s.reader.scanner.SetConfig(config) +} + // AddCmd adds a new command handler. // This only adds top level commands. func (s *Shell) AddCmd(cmd *Cmd) { @@ -408,6 +416,11 @@ func (s *Shell) Interrupt(f func(c *Context, count int, input string)) { s.interrupt = f } +// FilterInput add a function filter input rune (Ctrl-z) +func (s *Shell) FilterInput(f func(r rune) (rune, bool)) { + s.filterInput = f +} + // EOF adds a function to handle End of File input (Ctrl-d). // This overrides the default behaviour which terminates the shell. func (s *Shell) EOF(f func(c *Context)) { From f99ef73da64badf65c0f9a04fc0a097f5fb14ad9 Mon Sep 17 00:00:00 2001 From: pushaowei Date: Wed, 11 Mar 2020 17:16:50 +0800 Subject: [PATCH 2/2] By adding a filter, ignore Ctrl-z --- functions.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/functions.go b/functions.go index f707f09..4cc066c 100644 --- a/functions.go +++ b/functions.go @@ -37,14 +37,14 @@ func addDefaultFuncs(s *Shell) { Func: clearFunc, }) s.Interrupt(interruptFunc) - s.FilterInput(filterInput) + s.FilterInput(filterInputFunc) } -func filterInput(r rune) (rune, bool){ +func filterInputFunc(r rune) (rune, bool){ switch r { - // block CtrlZ feature - case readline.CharCtrlZ: - return r, false + // block CtrlZ feature + case readline.CharCtrlZ: + return r, false } return r, true }