1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
package prep
// This is where the actual autocomplete happens
// lots of the fun magic is in here
import (
"errors"
"os"
"time"
"go.wit.com/dev/alexflint/arg"
"go.wit.com/lib/config"
durationpb "google.golang.org/protobuf/types/known/durationpb"
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
)
func Autocomplete(dest any) *Auto {
myAuto = new(AutoArgs) // todo: redo this
findAppInfo(dest) // parses back to main() for argv info
pb := parseArgv(myAuto.appName) // parses os.Args into a protobuf
// set the start time of the binary
now := time.Now()
pb.Ctime = timestamppb.New(now)
if pb.SetupAuto {
// --bash was passed. try to configure bash-completion
MakeAutocompleteFiles(myAuto.appName)
os.Exit(0)
}
if pb.Debug {
// dump debug info
pb.PrintDebug()
}
// all, err := pb.getHistoryPB() // read in the history protobuf file
// func (newpb *Auto) getHistoryPB() (*Autos, error) {
all := NewAutos()
err := config.LoadCache(all, "argv", pb.Argname) // loads ~/.cache/argv/forge.pb
if err != nil {
// there is no history
}
if all.Len() == 0 {
// first time
all.Clone(pb)
err = all.Save()
}
// set the duration since the last auto complete
// find the last entry. this is dumb way to do it
var last *Auto
for found := range all.IterAll() {
last = found
}
dur := time.Since(last.Ctime.AsTime())
pb.Duration = durationpb.New(dur)
// roll the autocomplete file
if all.Len() > 15 {
pb.Debugf("DEBUG: trim() history 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)
// }
}
// save the history
all.Clone(pb)
errors.Join(err, all.Save())
pb.Debugf("WRITE DEBUG: write PB='%s' len(pb)=%d config.Save().err=%v", all.Filename, all.Len(), err)
// return all, err
//}
if pb.Debug {
all.PrintHistory()
}
// turn on debugging if duration < 200 milliseconds
dur = pb.Duration.AsDuration()
if dur < time.Millisecond*200 {
pb.Debug = true
pb.Fast = 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)
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()")
}
// not autocompleting. just return to the application
if !pb.IsAuto {
arg.Register(&argBash)
myAuto.pp = arg.MustParse(dest)
return pb
}
// this is a work in progress
if pb.Last == "--gui" {
pb.Debugf("DEBUG: last=%s found --gui", pb.Last)
pb.Autocomplete2("andlabs gogui")
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)
return nil
}
|