summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-09-24 22:18:14 -0500
committerJeff Carr <[email protected]>2025-09-24 22:18:14 -0500
commitf2543af73813d1030e110ad539e4be67c3893e62 (patch)
treecf7278c22bd4996c6555e77f82caad25cd6bb30d
parent1dbf1a561b092658416e1a3b0c59ba6b057bad07 (diff)
trying to move code into go-arg
-rw-r--r--bash.orig.go64
-rw-r--r--complete.go250
-rw-r--r--smartcd.test2
3 files changed, 177 insertions, 139 deletions
diff --git a/bash.orig.go b/bash.orig.go
index 77b7e56..d095db5 100644
--- a/bash.orig.go
+++ b/bash.orig.go
@@ -5,12 +5,10 @@ package prep
import (
"fmt"
"os"
- "path/filepath"
"strings"
"go.wit.com/dev/alexflint/arg"
"go.wit.com/lib/gui/shell"
- "go.wit.com/log"
)
/*
@@ -37,33 +35,28 @@ type AutoArgs struct {
match map[string]string // maps for strings
}
-// argname is the name of the executable
-func Bash(argname string, autocomplete func([]string)) *AutoArgs {
- myAuto = new(AutoArgs)
- myAuto.appName = argname
-
- if len(os.Args) > 1 && os.Args[1] == "--bash" {
- doBash(argname)
- os.Exit(0)
- }
-
- if len(os.Args) > 1 && os.Args[1] == "--auto-complete" {
- autocomplete(os.Args[2:])
- os.Exit(0)
- }
-
- arg.Register(&argBash)
-
- // parse go.Arg here?
- return myAuto
-}
-
// print out auto complete debugging info
func (pb *Auto) PrintDebug() {
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)
}
+func (pb *Auto) Debugf(fmts string, parts ...any) {
+ fmts = strings.TrimSpace(fmts)
+ fmts += "\n"
+ // NOTE: env doesn't work probably most (all?) the time because bash
+ // doesn't send all the ENV to autocomplete. so, trap on a "--autodebug" command line arg
+ if os.Getenv("AUTOCOMPLETE_VERBOSE") == "true" || pb.Debug {
+ if !pb.Newline {
+ fmt.Fprintf(os.Stderr, "\n")
+ pb.Newline = true
+ }
+ fmt.Fprintf(os.Stderr, fmts, parts...)
+ } else {
+ // fmt.Fprintf(os.Stderr, "NOT DOING ANYTHING\n")
+ }
+}
+
// returns the last command (is blank if the current arg is not blank)
func GetLast(cur string, argv []string) string {
if cur != "''" {
@@ -83,6 +76,7 @@ func AppName() string {
return myAuto.appName
}
+/*
// makes a bash autocomplete file for your command
func doBash(argname string) {
fmt.Println(makeBashCompletionText(argname))
@@ -112,3 +106,27 @@ func doBash(argname string) {
}
os.Exit(0)
}
+*/
+
+/*
+// argname is the name of the executable
+func Bash(argname string, autocomplete func([]string)) *AutoArgs {
+ myAuto = new(AutoArgs)
+ myAuto.appName = argname
+
+ if len(os.Args) > 1 && os.Args[1] == "--bash" {
+ doBash(argname)
+ os.Exit(0)
+ }
+
+ if len(os.Args) > 1 && os.Args[1] == "--auto-complete" {
+ autocomplete(os.Args[2:])
+ os.Exit(0)
+ }
+
+ arg.Register(&argBash)
+
+ // parse go.Arg here?
+ return myAuto
+}
+*/
diff --git a/complete.go b/complete.go
index 235e73d..45fdb76 100644
--- a/complete.go
+++ b/complete.go
@@ -17,52 +17,6 @@ import (
)
// makes a bash autocomplete file for your command
-func doBash2(argname string) {
- fmt.Println(makeBashCompletionText2(argname))
-
- homeDir, err := os.UserHomeDir()
- if err != nil {
- log.Printf("%v\n", err)
- os.Exit(0)
- }
- filename := filepath.Join(homeDir, ".local/share/bash-completion/completions", argname)
- if shell.Exists(filename) {
- log.Println(filename, "file already exists")
- // os.Exit(0)
- }
- basedir, _ := filepath.Split(filename)
- if !shell.IsDir(basedir) {
- os.MkdirAll(basedir, os.ModePerm)
- }
-
- if f, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err == nil {
- f.Write([]byte(makeBashCompletionText2(argname)))
- f.Close()
- log.Println("bash file created:", filename)
- log.Println("restart bash")
- } else {
- log.Info(filename, err)
- }
- os.Exit(0)
-}
-
-func (pb *Auto) Debugf(fmts string, parts ...any) {
- fmts = strings.TrimSpace(fmts)
- fmts += "\n"
- // NOTE: env doesn't work probably most (all?) the time because bash
- // doesn't send all the ENV to autocomplete. so, trap on a "--autodebug" command line arg
- if os.Getenv("AUTOCOMPLETE_VERBOSE") == "true" || pb.Debug {
- if !pb.Newline {
- fmt.Fprintf(os.Stderr, "\n")
- pb.Newline = true
- }
- fmt.Fprintf(os.Stderr, fmts, parts...)
- } else {
- // fmt.Fprintf(os.Stderr, "NOT DOING ANYTHING\n")
- }
-}
-
-// makes a bash autocomplete file for your command
func (pb *Auto) doHandlePB() error {
homeDir, err := os.UserHomeDir()
if err != nil {
@@ -124,43 +78,34 @@ func (pb *Auto) doHandlePB() error {
return err
}
-/*
-// prints help to STDERR // TODO: move everything below this to go-args
-func (args) doBashHelp() {
- if argv.BashAuto[1] != "''" {
- // if this is not blank, then the user has typed something
- return
- }
- if argv.BashAuto[0] != ARGNAME {
- // if this is not the name of the command, the user already started doing something
- return
- }
- if argv.BashAuto[0] == ARGNAME {
- me.pp.WriteHelp(os.Stderr)
- return
+func (pb *Auto) SubCommand(cmd string) {
+ partial := strings.Trim(pb.Partial, "'")
+ if pb.Debug {
+ myAuto.pp.WriteHelpForAutocomplete(os.Stderr, os.Stdout, partial, pb.Cmd)
+ // myAuto.pp.GetUsageForSubcommand(os.Stdout, os.Stderr, partial, cmd)
+ // myAuto.pp.GetUsageForSubcommand(os.Stdout, nil, partial, cmd)
+ } else {
+ f, _ := os.OpenFile("/tmp/outlook", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
+ myAuto.pp.WriteHelpForAutocomplete(f, os.Stdout, partial, pb.Cmd)
+ // myAuto.pp.GetUsageForSubcommand(os.Stdout, nil, partial, cmd)
}
- fmt.Fprintln(os.Stderr, "")
- fmt.Fprintln(os.Stderr, "hello world")
- fmt.Fprintln(os.Stderr, "")
+ os.Exit(0)
+ // SubCommand(cmd)
}
-*/
-func (pb *Auto) Autocomplete(notsure any, sendthis string) {
- 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)
+func (pb *Auto) SubCommand2(cmd string, addmatch []string) {
+ partial := strings.Trim(pb.Partial, "'")
+ if pb.Debug {
+ myAuto.pp.WriteHelpForAutocomplete(os.Stderr, os.Stdout, partial, pb.Cmd)
+ // myAuto.pp.GetUsageForSubcommand(os.Stdout, os.Stderr, partial, cmd)
+ // myAuto.pp.GetUsageForSubcommand(os.Stdout, nil, partial, cmd)
+ } else {
+ f, _ := os.OpenFile("/tmp/outlook", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
+ myAuto.pp.WriteHelpForAutocomplete(f, os.Stdout, partial, pb.Cmd)
+ // myAuto.pp.GetUsageForSubcommand(os.Stdout, nil, partial, cmd)
}
- fmt.Printf("%s", strings.Join(all, " "))
+ os.Exit(0)
+ // SubCommand(cmd)
}
func (pb *Auto) Autocomplete2(sendthis string) {
@@ -278,39 +223,6 @@ func parseArgv(argname string) *Auto {
}
// 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
- 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{pb.Arg3, pb.Arg0}
- // arg.InitFlags(flags)
-
- appAutoFunc(pb) // run the autocomplete function the user made for their application
- os.Exit(0)
- }
-
- arg.Register(&argBash)
- return pb
-}
-
-// also try to parse/send cur (?)
// func Bash3(appAutoFunc func(*Auto), dest any) *Auto {
func Bash3(dest any) *Auto {
myAuto = new(AutoArgs)
@@ -319,7 +231,7 @@ func Bash3(dest any) *Auto {
pb := parseArgv(myAuto.appName)
if pb.SetupAuto {
// --bash was passed. try to configure bash-completion
- doBash2(myAuto.appName)
+ doBash(myAuto.appName)
os.Exit(0)
}
@@ -350,7 +262,7 @@ func Bash3(dest any) *Auto {
flags = append(flags, s)
}
// pb.Debug = true
- pb.Debugf("DEBUG: MustParse(%v)", flags)
+ // pb.Debugf("DEBUG: MustParse(%v)", flags)
var err error
myAuto.pp, err = arg.ParseFlags(flags, dest)
if err != nil {
@@ -360,7 +272,7 @@ func Bash3(dest any) *Auto {
if myAuto.pp == nil {
pb.Debugf("DEBUG: myAuto.pp == nil after ParseFlags()")
} else {
- pb.Debugf("DEBUG: myAuto.pp is ok after ParseFlags()")
+ // pb.Debugf("DEBUG: myAuto.pp is ok after ParseFlags()")
}
if pb.IsAuto {
@@ -370,7 +282,7 @@ func Bash3(dest any) *Auto {
pb.Autocomplete2(val)
os.Exit(0)
} else {
- pb.Debugf("DEBUG: NO MATCH last='%s' found key '%s' = %s", pb.Last, key, val)
+ // pb.Debugf("DEBUG: NO MATCH last='%s' found key '%s' = %s", pb.Last, key, val)
}
}
myAuto.autoFunc(pb) // run the autocomplete function the user made for their application
@@ -386,3 +298,109 @@ func Bash3(dest any) *Auto {
myAuto.pp = arg.MustParse(dest)
return pb
}
+
+// makes a bash autocomplete file for your command
+func doBash(argname string) {
+ fmt.Println(makeBashCompletionText2(argname))
+
+ homeDir, err := os.UserHomeDir()
+ if err != nil {
+ log.Printf("%v\n", err)
+ os.Exit(0)
+ }
+ filename := filepath.Join(homeDir, ".local/share/bash-completion/completions", argname)
+ if shell.Exists(filename) {
+ log.Println(filename, "file already exists")
+ // os.Exit(0)
+ }
+ basedir, _ := filepath.Split(filename)
+ if !shell.IsDir(basedir) {
+ os.MkdirAll(basedir, os.ModePerm)
+ }
+
+ if f, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err == nil {
+ f.Write([]byte(makeBashCompletionText2(argname)))
+ f.Close()
+ log.Println("bash file created:", filename)
+ log.Println("restart bash")
+ } else {
+ log.Info(filename, err)
+ }
+ os.Exit(0)
+}
+
+/*
+// 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
+ 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{pb.Arg3, pb.Arg0}
+ // arg.InitFlags(flags)
+
+ appAutoFunc(pb) // run the autocomplete function the user made for their application
+ os.Exit(0)
+ }
+
+ arg.Register(&argBash)
+ return pb
+}
+*/
+
+/*
+// prints help to STDERR // TODO: move everything below this to go-args
+func (args) doBashHelp() {
+ if argv.BashAuto[1] != "''" {
+ // if this is not blank, then the user has typed something
+ return
+ }
+ if argv.BashAuto[0] != ARGNAME {
+ // if this is not the name of the command, the user already started doing something
+ return
+ }
+ if argv.BashAuto[0] == ARGNAME {
+ me.pp.WriteHelp(os.Stderr)
+ return
+ }
+ fmt.Fprintln(os.Stderr, "")
+ fmt.Fprintln(os.Stderr, "hello world")
+ fmt.Fprintln(os.Stderr, "")
+}
+*/
+
+/*
+func (pb *Auto) Autocomplete(notsure any, sendthis string) {
+ 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, " "))
+}
+*/
diff --git a/smartcd.test b/smartcd.test
index b67a5ea..d476814 100644
--- a/smartcd.test
+++ b/smartcd.test
@@ -1,3 +1,5 @@
+# could work like 'z' ?
+
_cd_complete()
{
# sets local to this func vars