Compare commits
No commits in common. "87751f7c720b9af79bc5c5679b1f003ba5fae4ed" and "1522a2fef0ed356f12a5c9cc011cdad6b4cd4a05" have entirely different histories.
87751f7c72
...
1522a2fef0
|
@ -13,33 +13,35 @@ import (
|
|||
"go.wit.com/log"
|
||||
)
|
||||
|
||||
var argBash ArgsBash
|
||||
|
||||
/*
|
||||
This struct can be used with the go-arg package. These
|
||||
are the generic default command line arguments for the 'GUI' package
|
||||
*/
|
||||
var argBash ArgsBash
|
||||
|
||||
type ArgsBash struct {
|
||||
Bash bool `arg:"--bash" help:"generate bash completion"`
|
||||
}
|
||||
|
||||
// try this struct out (?)
|
||||
var myAuto *AutoArgs
|
||||
var myBash *BashAuto
|
||||
|
||||
type AutoArgs struct {
|
||||
id int // should be unique
|
||||
hidden bool // don't update the toolkits when it's hidden
|
||||
Auto func([]string)
|
||||
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!
|
||||
autoFunc func(*Auto)
|
||||
type BashAuto struct {
|
||||
id int // should be unique
|
||||
hidden bool // don't update the toolkits when it's hidden
|
||||
Auto func([]string)
|
||||
appName string // a good way to track the name of the binary ?
|
||||
}
|
||||
|
||||
// argname is the name of the executable
|
||||
func Bash(argname string, autocomplete func([]string)) *AutoArgs {
|
||||
myAuto = new(AutoArgs)
|
||||
myAuto.appName = argname
|
||||
/*
|
||||
func dumpjunk() {
|
||||
fmt.Fprintln(os.Stderr, "")
|
||||
fmt.Fprintln(os.Stderr, os.Args)
|
||||
os.Exit(0)
|
||||
}
|
||||
*/
|
||||
|
||||
// argname is the name of the executable
|
||||
func Bash(argname string, autocomplete func([]string)) *BashAuto {
|
||||
if len(os.Args) > 1 && os.Args[1] == "--bash" {
|
||||
doBash(argname)
|
||||
os.Exit(0)
|
||||
|
@ -52,12 +54,15 @@ func Bash(argname string, autocomplete func([]string)) *AutoArgs {
|
|||
|
||||
arg.Register(&argBash)
|
||||
|
||||
myBash = new(BashAuto)
|
||||
myBash.appName = argname
|
||||
|
||||
// parse go.Arg here?
|
||||
return myAuto
|
||||
return myBash
|
||||
}
|
||||
|
||||
// print out auto complete debugging info
|
||||
func (pb *Auto) PrintDebug() {
|
||||
func (pb *Auto) AutoDebug() {
|
||||
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)
|
||||
// fmt.Println("--all --gui --verbose --force")
|
||||
|
@ -79,7 +84,7 @@ func GetLast(cur string, argv []string) string {
|
|||
|
||||
// returns the name of the executable registered for shell autocomplete
|
||||
func AppName() string {
|
||||
return myAuto.appName
|
||||
return myBash.appName
|
||||
}
|
||||
|
||||
// makes a bash autocomplete file for your command
|
|
@ -28,7 +28,7 @@ func doBash2(argname string) {
|
|||
filename := filepath.Join(homeDir, ".local/share/bash-completion/completions", argname)
|
||||
if shell.Exists(filename) {
|
||||
log.Println(filename, "file already exists")
|
||||
// os.Exit(0)
|
||||
os.Exit(0)
|
||||
}
|
||||
basedir, _ := filepath.Split(filename)
|
||||
if !shell.IsDir(basedir) {
|
||||
|
@ -63,7 +63,7 @@ func (pb *Auto) Debugf(fmts string, parts ...any) {
|
|||
}
|
||||
|
||||
// makes a bash autocomplete file for your command
|
||||
func (pb *Auto) doHandlePB() error {
|
||||
func doHandlePB(pb *Auto) error {
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -81,8 +81,7 @@ func (pb *Auto) doHandlePB() error {
|
|||
for found := range all.IterAll() {
|
||||
dur := time.Since(found.Ctime.AsTime())
|
||||
pb.Duration = durationpb.New(dur)
|
||||
// found.PrintDebug()
|
||||
pb.Debugf("AUTO HISTORY: age=%s cmd='%s' partial='%s' argv='%v'", shell.FormatDuration(dur), found.Cmd, found.Partial, found.Argv)
|
||||
pb.Debugf("AUTO HISTORY: ctime='%v' age=%s argv='%v'", found.Ctime, shell.FormatDuration(dur), found.Argv)
|
||||
last = found
|
||||
}
|
||||
}
|
||||
|
@ -161,52 +160,6 @@ func (pb *Auto) Autocomplete(notsure any, sendthis string) {
|
|||
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 {
|
||||
newauto := new(Auto)
|
||||
newauto.Argname = argname
|
||||
|
@ -253,109 +206,32 @@ func parseArgv(argname string) *Auto {
|
|||
newauto.Cmd = s
|
||||
break
|
||||
}
|
||||
if newauto.Partial == "'"+newauto.Cmd+"'" {
|
||||
// not really a command, it's just a partially inputed string from the user
|
||||
newauto.Cmd = ""
|
||||
if newauto.Cmd == "" {
|
||||
newauto.Cmd = strings.Join(newauto.Argv, "BLAH")
|
||||
}
|
||||
// if newauto.Cmd == "" {
|
||||
// newauto.Cmd = strings.Join(newauto.Argv, "BLAH")
|
||||
// }
|
||||
return newauto
|
||||
}
|
||||
return newauto
|
||||
}
|
||||
|
||||
// also try to parse/send cur (?)
|
||||
func Bash2(argname string, appAutoFunc func(*Auto)) *Auto {
|
||||
pb := parseArgv(argname)
|
||||
if pb.SetupAuto {
|
||||
// --bash was passed. try to configure bash-completion
|
||||
func Bash2(argname string, autocomplete func(*Auto)) {
|
||||
newauto := parseArgv(argname)
|
||||
if newauto.SetupAuto {
|
||||
doBash2(argname)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if pb.Debug {
|
||||
if newauto.Debug {
|
||||
// dump debug info
|
||||
pb.PrintDebug()
|
||||
newauto.AutoDebug()
|
||||
}
|
||||
|
||||
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{pb.Arg3, pb.Arg0}
|
||||
// arg.InitFlags(flags)
|
||||
|
||||
appAutoFunc(pb) // run the autocomplete function the user made for their application
|
||||
if len(os.Args) > 1 && os.Args[1] == "--auto-complete" {
|
||||
doHandlePB(newauto)
|
||||
autocomplete(newauto)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
arg.Register(&argBash)
|
||||
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() // read in the history protobuf file
|
||||
|
||||
// turn on debugging if duration < 200 milliseconds
|
||||
dur := pb.Duration.AsDuration()
|
||||
if dur < time.Millisecond*200 {
|
||||
pb.Debug = true
|
||||
}
|
||||
|
||||
arg.Register(&argBash)
|
||||
flags := []string{}
|
||||
for _, s := range pb.Argv {
|
||||
if s == "--autodebug" {
|
||||
continue
|
||||
}
|
||||
flags = append(flags, s)
|
||||
}
|
||||
// pb.Debug = true
|
||||
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)
|
||||
}
|
||||
|
||||
if myAuto.pp == nil {
|
||||
pb.Debugf("DEBUG: myAuto.pp == nil after ParseFlags()")
|
||||
} else {
|
||||
pb.Debugf("DEBUG: myAuto.pp is ok after ParseFlags()")
|
||||
}
|
||||
|
||||
appAutoFunc(pb) // run the autocomplete function the user made for their application
|
||||
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
|
||||
}
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
arg.Register(&argBash)
|
||||
myAuto.pp = arg.MustParse(dest)
|
||||
return pb
|
||||
}
|
18
smartcd.test
18
smartcd.test
|
@ -1,18 +0,0 @@
|
|||
_cd_complete()
|
||||
{
|
||||
# sets local to this func vars
|
||||
local cur prev all
|
||||
cur=${COMP_WORDS[COMP_CWORD]}
|
||||
# prev=${COMP_WORDS[COMP_CWORD-1]}
|
||||
all=${COMP_WORDS[@]}
|
||||
|
||||
# this is where we generate the go-arg output
|
||||
GOARGS=$(smartcd --auto-complete \'$cur\' $all)
|
||||
|
||||
# this compares the command line input from the user
|
||||
# to whatever strings we output
|
||||
COMPREPLY=( $(compgen -W "$GOARGS" -- $cur) ) # THIS WORKS
|
||||
return 0
|
||||
}
|
||||
complete -F _cd_complete cd
|
||||
|
Loading…
Reference in New Issue