Skip to content

Commit

Permalink
fix: [Cherry-pick] Remove unused Unsafe reflection code (#315) (#316)
Browse files Browse the repository at this point in the history
Cherry-pick from master
pr: #315
Related to #314

Signed-off-by: Congqi Xia <[email protected]>
  • Loading branch information
congqixia authored Sep 26, 2024
1 parent 305b7df commit a729223
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 19 deletions.
3 changes: 2 additions & 1 deletion bapps/go_prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ func NewPromptApp(config *configs.Config, opts ...AppOption) BApp {
pa.sugguestHistory = !pa.sugguestHistory
},
}),
HandleFD,
// setup InputParser with `TearDown` overrided
prompt.OptionParser(NewBInputParser()),
)
pa.prompt = p
return pa
Expand Down
19 changes: 1 addition & 18 deletions bapps/go_prompt_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package bapps
import (
"os"
"os/exec"
"reflect"
"unsafe"

"github.com/c-bata/go-prompt"
)
Expand All @@ -24,24 +22,9 @@ func (t *bInputParser) TearDown() error {
return nil
}

func NewInputParser(parser *prompt.PosixParser) *bInputParser {
func NewBInputParser() *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{} {
// nolint
return reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).Elem().Interface()
}

0 comments on commit a729223

Please sign in to comment.