From f892600c35265c3b2d0c79322107239c1ec9479b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Setni=C4=8Dka?= Date: Mon, 20 Feb 2017 02:51:01 +0100 Subject: [PATCH] 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) --- example/readline-demo/readline-demo.go | 12 +++++++++++- operation.go | 9 +++++++++ readline.go | 4 ++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/example/readline-demo/readline-demo.go b/example/readline-demo/readline-demo.go index cb6fb1a..2b1daf7 100644 --- a/example/readline-demo/readline-demo.go +++ b/example/readline-demo/readline-demo.go @@ -61,6 +61,15 @@ var completer = readline.NewPrefixCompleter( 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() { l, err := readline.NewEx(&readline.Config{ Prompt: "\033[31m»\033[0m ", @@ -69,7 +78,8 @@ func main() { InterruptPrompt: "^C", EOFPrompt: "exit", - HistorySearchFold: true, + HistorySearchFold: true, + FuncFilterInputRune: filterInput, }) if err != nil { panic(err) diff --git a/operation.go b/operation.go index 8377cce..14ade31 100644 --- a/operation.go +++ b/operation.go @@ -96,6 +96,15 @@ func (o *Operation) ioloop() { keepInSearchMode := false keepInCompleteMode := false 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 o.buf.Len() == 0 { o.buf.Clean() diff --git a/readline.go b/readline.go index 1e232fb..3f02a83 100644 --- a/readline.go +++ b/readline.go @@ -63,6 +63,10 @@ type Config struct { // it use in IM usually. 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 FuncIsTerminal func() bool FuncMakeRaw func() error