another stab at autocomplete
This commit is contained in:
parent
763b1a60fc
commit
e0c508f197
132
bash.new.go
132
bash.new.go
|
@ -63,7 +63,7 @@ func (pb *Auto) Debugf(fmts string, parts ...any) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// makes a bash autocomplete file for your command
|
// makes a bash autocomplete file for your command
|
||||||
func doHandlePB(pb *Auto) error {
|
func (pb *Auto) doHandlePB() error {
|
||||||
homeDir, err := os.UserHomeDir()
|
homeDir, err := os.UserHomeDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -81,7 +81,8 @@ func doHandlePB(pb *Auto) error {
|
||||||
for found := range all.IterAll() {
|
for found := range all.IterAll() {
|
||||||
dur := time.Since(found.Ctime.AsTime())
|
dur := time.Since(found.Ctime.AsTime())
|
||||||
pb.Duration = durationpb.New(dur)
|
pb.Duration = durationpb.New(dur)
|
||||||
pb.Debugf("AUTO HISTORY: ctime='%v' age=%s argv='%v'", found.Ctime, shell.FormatDuration(dur), found.Argv)
|
// found.PrintDebug()
|
||||||
|
pb.Debugf("AUTO HISTORY: age=%s cmd='%s' partial='%s' argv='%v'", shell.FormatDuration(dur), found.Cmd, found.Partial, found.Argv)
|
||||||
last = found
|
last = found
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -160,6 +161,52 @@ func (pb *Auto) Autocomplete(notsure any, sendthis string) {
|
||||||
fmt.Printf("%s", strings.Join(all, " "))
|
fmt.Printf("%s", strings.Join(all, " "))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (pb *Auto) Autocomplete2(sendthis string) {
|
||||||
|
dur := pb.Duration.AsDuration()
|
||||||
|
if dur < time.Millisecond*200 {
|
||||||
|
pb.Debug = true
|
||||||
|
/*
|
||||||
|
pb.Debugf("TODO: show extended help here '%s' '%s' %v dur=%v\n", pb.Arg0, pb.Arg1, pb.Argv, shell.FormatDuration(dur))
|
||||||
|
pb.PrintDebug()
|
||||||
|
fmt.Println(" ")
|
||||||
|
*/
|
||||||
|
if myAuto.pp == nil {
|
||||||
|
pb.Debugf("myAuto.pp == nil")
|
||||||
|
} else {
|
||||||
|
pb.Debugf("myAuto.pp != nil")
|
||||||
|
if pb.Cmd == "" {
|
||||||
|
myAuto.pp.WriteHelp(os.Stderr)
|
||||||
|
} else {
|
||||||
|
myAuto.pp.WriteHelpForSubcommand(os.Stderr, pb.Cmd)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
parts := strings.Split(sendthis, " ")
|
||||||
|
var all []string
|
||||||
|
for _, part := range parts {
|
||||||
|
var found bool
|
||||||
|
for _, s := range os.Args {
|
||||||
|
if s == part {
|
||||||
|
found = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if found {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
all = append(all, part)
|
||||||
|
}
|
||||||
|
fmt.Printf("%s", strings.Join(all, " "))
|
||||||
|
/*
|
||||||
|
if dur > time.Millisecond*200 {
|
||||||
|
if dur < time.Millisecond*800 {
|
||||||
|
// fmt.Println("a b")
|
||||||
|
fmt.Println(pb.Partial + " hello world")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
func parseArgv(argname string) *Auto {
|
func parseArgv(argname string) *Auto {
|
||||||
newauto := new(Auto)
|
newauto := new(Auto)
|
||||||
newauto.Argname = argname
|
newauto.Argname = argname
|
||||||
|
@ -214,30 +261,91 @@ func parseArgv(argname string) *Auto {
|
||||||
return newauto
|
return newauto
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MustParse processes command line arguments and exits upon failure
|
||||||
|
func MustParse(dest ...interface{}) *arg.Parser {
|
||||||
|
// arg.
|
||||||
|
// register = append(register, dest...)
|
||||||
|
return arg.MustParse(dest)
|
||||||
|
}
|
||||||
|
|
||||||
// also try to parse/send cur (?)
|
// also try to parse/send cur (?)
|
||||||
func Bash2(argname string, autocomplete func(*Auto)) *Auto {
|
func Bash2(argname string, appAutoFunc func(*Auto)) *Auto {
|
||||||
newauto := parseArgv(argname)
|
pb := parseArgv(argname)
|
||||||
if newauto.SetupAuto {
|
if pb.SetupAuto {
|
||||||
// --bash was passed. try to configure bash-completion
|
// --bash was passed. try to configure bash-completion
|
||||||
doBash2(argname)
|
doBash2(argname)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
if newauto.Debug {
|
if pb.Debug {
|
||||||
// dump debug info
|
// dump debug info
|
||||||
newauto.AutoDebug()
|
pb.PrintDebug()
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(os.Args) > 1 && os.Args[1] == "--auto-complete" {
|
if pb.IsAuto {
|
||||||
doHandlePB(newauto)
|
pb.doHandlePB()
|
||||||
autocomplete(newauto)
|
if pb.Debug {
|
||||||
if newauto.Debug {
|
// TODO:
|
||||||
// check here to see if there was any completion text sent
|
// check here to see if there was any completion text sent
|
||||||
// if not, send "reset bash newline\n" to cause bash to redraw PS1 for the user
|
// if not, send "reset bash newline\n" to cause bash to redraw PS1 for the user
|
||||||
}
|
}
|
||||||
|
arg.Register(&argBash)
|
||||||
|
// flags := []string{pb.Arg3, pb.Arg0}
|
||||||
|
// arg.InitFlags(flags)
|
||||||
|
|
||||||
|
appAutoFunc(pb) // run the autocomplete function the user made for their application
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
arg.Register(&argBash)
|
arg.Register(&argBash)
|
||||||
return newauto
|
return pb
|
||||||
|
}
|
||||||
|
|
||||||
|
// also try to parse/send cur (?)
|
||||||
|
func Bash3(argname string, appAutoFunc func(*Auto), dest any) *Auto {
|
||||||
|
myAuto = new(AutoArgs)
|
||||||
|
myAuto.appName = argname
|
||||||
|
myAuto.autoFunc = appAutoFunc
|
||||||
|
|
||||||
|
pb := parseArgv(argname)
|
||||||
|
if pb.SetupAuto {
|
||||||
|
// --bash was passed. try to configure bash-completion
|
||||||
|
doBash2(argname)
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
if pb.Debug {
|
||||||
|
// dump debug info
|
||||||
|
pb.PrintDebug()
|
||||||
|
}
|
||||||
|
|
||||||
|
if pb.IsAuto {
|
||||||
|
pb.doHandlePB()
|
||||||
|
if pb.Debug {
|
||||||
|
// TODO:
|
||||||
|
// check here to see if there was any completion text sent
|
||||||
|
// if not, send "reset bash newline\n" to cause bash to redraw PS1 for the user
|
||||||
|
}
|
||||||
|
arg.Register(&argBash)
|
||||||
|
flags := []string{}
|
||||||
|
for _, s := range pb.Argv {
|
||||||
|
if s == "--autodebug" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
flags = append(flags, s)
|
||||||
|
}
|
||||||
|
pb.Debugf("DEBUG: MustParse(%v)", flags)
|
||||||
|
var err error
|
||||||
|
myAuto.pp, err = arg.ParseFlags(flags, dest)
|
||||||
|
if err != nil {
|
||||||
|
pb.Debugf("DEBUG: Parse error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
appAutoFunc(pb) // run the autocomplete function the user made for their application
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
arg.Register(&argBash)
|
||||||
|
myAuto.pp = arg.MustParse(dest)
|
||||||
|
return pb
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,6 +32,7 @@ type AutoArgs struct {
|
||||||
Auto func([]string)
|
Auto func([]string)
|
||||||
appName string // a good way to track the name of the binary ?
|
appName string // a good way to track the name of the binary ?
|
||||||
pp *arg.Parser // for parsing the command line args. Yay to alexf lint!
|
pp *arg.Parser // for parsing the command line args. Yay to alexf lint!
|
||||||
|
autoFunc func(*Auto)
|
||||||
}
|
}
|
||||||
|
|
||||||
// argname is the name of the executable
|
// argname is the name of the executable
|
||||||
|
@ -56,7 +57,7 @@ func Bash(argname string, autocomplete func([]string)) *AutoArgs {
|
||||||
}
|
}
|
||||||
|
|
||||||
// print out auto complete debugging info
|
// print out auto complete debugging info
|
||||||
func (pb *Auto) AutoDebug() {
|
func (pb *Auto) PrintDebug() {
|
||||||
dur := pb.Duration.AsDuration()
|
dur := pb.Duration.AsDuration()
|
||||||
pb.Debugf("AUTOCOMPLETE: arg0='%s' arg1='%s' partial='%s' cmd='%s' age=%s argv=%v\n", pb.Arg0, pb.Arg1, pb.Partial, pb.Cmd, shell.FormatDuration(dur), pb.Argv)
|
pb.Debugf("AUTOCOMPLETE: arg0='%s' arg1='%s' partial='%s' cmd='%s' age=%s argv=%v\n", pb.Arg0, pb.Arg1, pb.Partial, pb.Cmd, shell.FormatDuration(dur), pb.Argv)
|
||||||
// fmt.Println("--all --gui --verbose --force")
|
// fmt.Println("--all --gui --verbose --force")
|
||||||
|
|
Loading…
Reference in New Issue