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"),
)
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 ",
@ -70,6 +79,7 @@ func main() {
EOFPrompt: "exit",
HistorySearchFold: true,
FuncFilterInputRune: filterInput,
})
if err != nil {
panic(err)

View File

@ -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()

View File

@ -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