something fun

This commit is contained in:
Jeff Carr 2025-09-17 20:55:21 -05:00
parent 06bb79b504
commit 89093ac8f7
2 changed files with 83 additions and 8 deletions

View File

@ -5,8 +5,9 @@ syntax = "proto3";
package httppb;
import "google/protobuf/timestamp.proto"; // Import the well-known type for Timestamp
import "google/protobuf/duration.proto"; // for duration
message Auto {
message Auto { // `autogenpb:marshal` `autogenpb:sort` `autogenpb:nomutex`
google.protobuf.Timestamp ctime = 1; // when the user tried this autocomplete
string argname = 2; // what the shell thinks the name of the executable is
string partial = 3; // set to the partial string trying to be matched
@ -17,10 +18,12 @@ message Auto {
bool isAuto = 8; // is true if '--auto-complete' is set
bool setupAuto = 9; // is true if '--bash' is set // setup bash autocomplete here
bool debug = 10; // print debugging info if true
bool newline = 11; // was a newline was sent to STDERR?
google.protobuf.Duration duration = 12; // time since the last autocomplete
}
message Autos { // `autogenpb:marshal` `autogenpb:mutex`
message Autos { // `autogenpb:marshal` `autogenpb:sort` `autogenpb:nomutex`
string uuid = 1; // `autogenpb:uuid:94210ebf-a534-4b33-aadd-2f5e1f56ae38`
string version = 2; // `autogenpb:version:v0.0.1`
repeated Auto auto = 3; // THIS MUST BE HttpRequest and then HttpRequests
repeated Auto autos = 3; // THIS MUST BE HttpRequest and then HttpRequests
}

82
bash.go
View File

@ -7,10 +7,13 @@ import (
"os"
"path/filepath"
"strings"
"time"
"go.wit.com/dev/alexflint/arg"
"go.wit.com/lib/gui/shell"
"go.wit.com/log"
durationpb "google.golang.org/protobuf/types/known/durationpb"
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
)
var argBash ArgsBash
@ -32,11 +35,13 @@ type BashAuto struct {
appName string // a good way to track the name of the binary ?
}
/*
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 {
@ -61,8 +66,7 @@ func Bash(argname string, autocomplete func([]string)) *BashAuto {
// print out auto complete debugging info
func AutoDebug(pb *Auto) {
fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintf(os.Stderr, "AUTO DEBUG: arg0='%s' arg1='%s' partial='%s' argv=%v\n", pb.Arg0, pb.Arg1, pb.Partial, pb.Argv)
pb.Debugf("AUTO DEBUG: arg0='%s' arg1='%s' partial='%s' argv=%v\n", pb.Arg0, pb.Arg1, pb.Partial, pb.Argv)
// fmt.Println("--all --gui --verbose --force")
}
@ -93,9 +97,6 @@ func parseArgv(argname string) *Auto {
}
// set debug flag
if os.Getenv("AUTOCOMPLETE_VERBOSE") == "true" {
newauto.Debug = true
}
for _, s := range os.Args {
if s == "--debug" {
newauto.Debug = true
@ -128,6 +129,7 @@ func Bash2(argname string, autocomplete func(*Auto)) {
}
if len(os.Args) > 1 && os.Args[1] == "--auto-complete" {
doHandlePB(newauto)
autocomplete(newauto)
os.Exit(0)
}
@ -200,6 +202,76 @@ func doBash2(argname string) {
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 "--debug" 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 doHandlePB(pb *Auto) error {
homeDir, err := os.UserHomeDir()
if err != nil {
return err
}
basedir := filepath.Join(homeDir, ".cache/autocomplete")
os.MkdirAll(basedir, os.ModePerm)
fullname := filepath.Join(basedir, pb.Argname+".pb")
all := NewAutos()
var last *Auto
data, err := os.ReadFile(fullname)
if err == nil {
err = all.Unmarshal(data)
if err == nil {
for found := range all.IterAll() {
dur := time.Since(found.Ctime.AsTime())
pb.Duration = durationpb.New(dur)
pb.Debugf("AUTO DEBUG: ctime='%v' age=%s argv='%v'", found.Ctime, shell.FormatDuration(dur), found.Argv)
last = found
}
}
}
if all.Len() > 15 {
pb.Debugf("DEBUG TOO LONG: len is over 100 len=%d vs new=%d", all.Len(), all.Len()-90)
all.Autos = all.Autos[all.Len()-10:]
// newall.Autos = all.Autos[0:10]
// for _, found := range all.Autos[0:10] {
// newall.Append(found)
// }
}
now := time.Now()
pb.Ctime = timestamppb.New(now)
duration := time.Since(last.Ctime.AsTime())
all.Append(pb)
data, err = all.Marshal()
if err != nil {
return err
}
f, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
defer f.Close()
if err != nil {
return err
}
_, err = f.Write(data)
pb.Debugf("WRITE DEBUG: write PB='%s' len(pb)=%d len(data)=%d dur=%v err=%v", fullname, all.Len(), len(data), duration, err)
return err
}
/*
// prints help to STDERR // TODO: move everything below this to go-args
func (args) doBashHelp() {