diff --git a/functions.go b/functions.go index 886a5a1..4cc066c 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(filterInputFunc) +} + +func filterInputFunc(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)) {