diff options
Diffstat (limited to 'bash3.go')
| -rw-r--r-- | bash3.go | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/bash3.go b/bash3.go new file mode 100644 index 0000000..c9a52ae --- /dev/null +++ b/bash3.go @@ -0,0 +1,125 @@ +package prep + +// initializes logging and command line options + +import ( + "fmt" + "os" + "path/filepath" + "time" + + "go.wit.com/dev/alexflint/arg" + "go.wit.com/lib/config" + "go.wit.com/log" +) + +func Bash3(dest any) *Auto { + return Bash(dest) +} + +func Bash(dest any) *Auto { + myAuto = new(AutoArgs) + findAppInfo(dest) // parses back to main() for argv info + + pb := parseArgv(myAuto.appName) + if pb.SetupAuto { + // --bash was passed. try to configure bash-completion + doBash(myAuto.appName) + os.Exit(0) + } + + myAuto.match = make(map[string]string) + myAuto.match["--gui"] = "andlabs gocui" + + if pb.Debug { + // dump debug info + pb.PrintDebug() + } + + 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 + } + + // prepart["--gui"] = "andlabs gocui" + + 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()") + } + + if pb.IsAuto { + for key, val := range myAuto.match { + if pb.Last == key { + pb.Debugf("DEBUG: last=%s found key %s = %s", pb.Last, key, val) + pb.Autocomplete2(val) + os.Exit(0) + } else { + // pb.Debugf("DEBUG: NO MATCH last='%s' found key '%s' = %s", pb.Last, key, val) + } + } + if myAuto.autoFunc == nil { + pb.SubCommand(pb.Argv...) + } else { + myAuto.autoFunc(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 +} + +// 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 config.Exists(filename) { + log.Fprintln(os.Stderr, "# file already exists", filename) + // os.Exit(0) + } + basedir, _ := filepath.Split(filename) + if !config.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() + } else { + log.Fprintln(os.Stderr, "# open file error", filename, err) + } + os.Exit(0) +} |
