Ability to filter out input runes (#104)

Could be used to mask/disable some actions (like Ctrl-Z, or searching by
Ctrl-R and Ctrl-S)
This commit is contained in:
Jiří Setnička 2017-02-20 02:51:01 +01:00 committed by chzyer
parent eef24db1d5
commit f892600c35
3 changed files with 24 additions and 1 deletions

View File

@ -61,6 +61,15 @@ var completer = readline.NewPrefixCompleter(
readline.PcItem("sleep"), readline.PcItem("sleep"),
) )
func filterInput(r rune) (rune, bool) {
switch r {
// block CtrlZ feature
case readline.CharCtrlZ:
return r, false
}
return r, true
}
func main() { func main() {
l, err := readline.NewEx(&readline.Config{ l, err := readline.NewEx(&readline.Config{
Prompt: "\033[31m»\033[0m ", Prompt: "\033[31m»\033[0m ",
@ -69,7 +78,8 @@ func main() {
InterruptPrompt: "^C", InterruptPrompt: "^C",
EOFPrompt: "exit", EOFPrompt: "exit",
HistorySearchFold: true, HistorySearchFold: true,
FuncFilterInputRune: filterInput,
}) })
if err != nil { if err != nil {
panic(err) panic(err)

View File

@ -96,6 +96,15 @@ func (o *Operation) ioloop() {
keepInSearchMode := false keepInSearchMode := false
keepInCompleteMode := false keepInCompleteMode := false
r := o.t.ReadRune() r := o.t.ReadRune()
if o.cfg.FuncFilterInputRune != nil {
var process bool
r, process = o.cfg.FuncFilterInputRune(r)
if !process {
o.buf.Refresh(nil) // to refresh the line
continue // ignore this rune
}
}
if r == 0 { // io.EOF if r == 0 { // io.EOF
if o.buf.Len() == 0 { if o.buf.Len() == 0 {
o.buf.Clean() o.buf.Clean()

View File

@ -63,6 +63,10 @@ type Config struct {
// it use in IM usually. // it use in IM usually.
UniqueEditLine bool UniqueEditLine bool
// filter input runes (may be used to disable CtrlZ or for translating some keys to different actions)
// -> output = new (translated) rune and true/false if continue with processing this one
FuncFilterInputRune func(rune) (rune, bool)
// force use interactive even stdout is not a tty // force use interactive even stdout is not a tty
FuncIsTerminal func() bool FuncIsTerminal func() bool
FuncMakeRaw func() error FuncMakeRaw func() error