-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reset Raw mode before start command running (#158)
Signed-off-by: Congqi Xia <[email protected]>
- Loading branch information
Showing
2 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |