Skip to content

Commit

Permalink
Reset Raw mode before start command running (#158)
Browse files Browse the repository at this point in the history
Signed-off-by: Congqi Xia <congqi.xia@zilliz.com>
  • Loading branch information
congqixia committed Jun 25, 2023
1 parent 547e1de commit 212e0d3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions bapps/go_prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func NewPromptApp(config *configs.Config, opts ...AppOption) BApp {
pa.sugguestHistory = !pa.sugguestHistory
},
}),
HandleFD,
)
pa.prompt = p
return pa
Expand Down Expand Up @@ -128,6 +129,7 @@ func (a *PromptApp) promptExecute(in string) {
}()
}
} else {
os.Stdout = stdout
close(pagerSig)
}
a.currentState, _ = a.currentState.Process(in)
Expand Down
46 changes: 46 additions & 0 deletions bapps/go_prompt_parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package bapps

import (
"os"
"os/exec"
"reflect"
"unsafe"

"github.com/c-bata/go-prompt"
)

// bInputParser wraps prompt.PosixParser to change TearDown behavior.
type bInputParser struct {
*prompt.PosixParser
}

// TearDown should be called after stopping input
func (t *bInputParser) TearDown() error {
t.PosixParser.TearDown()
rawModeOff := exec.Command("/bin/stty", "-raw", "echo")
rawModeOff.Stdin = os.Stdin
_ = rawModeOff.Run()
rawModeOff.Wait()
return nil
}

func NewInputParser(parser *prompt.PosixParser) *bInputParser {
p := &bInputParser{
PosixParser: prompt.NewStandardInputParser(),
}
return p
}

func HandleFD(p *prompt.Prompt) error {
in, ok := GetUnexportedField(reflect.ValueOf(p).Elem().FieldByName("in")).(*prompt.PosixParser)
if !ok {
// failed to reflect
return nil
}

return prompt.OptionParser(NewInputParser(in))(p)
}

func GetUnexportedField(field reflect.Value) interface{} {
return reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).Elem().Interface()

Check failure on line 45 in bapps/go_prompt_parser.go

View workflow job for this annotation

GitHub Actions / lint

G103: Use of unsafe calls should be audited (gosec)
}

0 comments on commit 212e0d3

Please sign in to comment.