Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

By adding a filter, ignore Ctrl-z #133

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
15 changes: 13 additions & 2 deletions functions.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package ishell

import (
"os"
"github.com/abiosoft/readline"
"os"
)

func exitFunc(c *Context) {
Expand Down Expand Up @@ -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) {
Expand Down
15 changes: 14 additions & 1 deletion ishell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)) {
Expand Down