autocomplete: allow completion based on name/detail (#165)
Fixes #48
https://github.com/user-attachments/assets/8296cbef-29ba-4c82-a072-46affbef7ce1
Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
diff --git a/cli/completer.go b/cli/completer.go
index 986b7e9..082ec50 100644
--- a/cli/completer.go
+++ b/cli/completer.go
@@ -465,7 +465,7 @@
})
}
for _, item := range argOptions {
- if strings.HasPrefix(item.Value, argInput) {
+ if strings.HasPrefix(item.Value, argInput) || (len(item.Detail) > 0 && strings.HasPrefix(item.Detail, argInput)) {
filteredOptions = append(filteredOptions, item)
}
}
@@ -476,12 +476,14 @@
}
for _, item := range filteredOptions {
option := item.Value + " "
- if len(filteredOptions) > 1 && len(item.Detail) > 0 {
+ if len(filteredOptions) > 0 && len(item.Detail) > 0 {
option += fmt.Sprintf("(%v)", item.Detail)
}
if strings.HasPrefix(option, argInput) {
options = append(options, []rune(option[len(argInput):]))
offset = len(argInput)
+ } else if len(item.Detail) > 0 && strings.HasPrefix(item.Detail, argInput) {
+ options = append(options, []rune(option))
}
}
return
diff --git a/vendor/github.com/chzyer/readline/complete.go b/vendor/github.com/chzyer/readline/complete.go
index 1c15aed..a5a2301 100644
--- a/vendor/github.com/chzyer/readline/complete.go
+++ b/vendor/github.com/chzyer/readline/complete.go
@@ -45,19 +45,34 @@
width: width,
}
}
+
+func (o *opCompleter) truncateBufferAfterLastEqual(completion []rune) {
+ bufRunes := o.op.buf.Runes()
+ for i := len(bufRunes) - 1; i >= 0; i-- {
+ if bufRunes[i] == '=' {
+ prefix := bufRunes[i+1:] // part after '=' in buffer
+ if len(prefix) > 0 && len(completion) > 0 && string(completion[:len(prefix)]) == string(prefix) {
+ o.op.buf.Set(bufRunes[:i+1]) // Keep content till '='
+ }
+ break
+ }
+ }
+}
+
func (o *opCompleter) writeRunes(candidate []rune) {
- detailIndex := len(candidate)
+ selected := candidate
spaceFound := false
for idx, r := range candidate {
if r == ' ' {
spaceFound = true
}
if spaceFound && r == '(' {
- detailIndex = idx
+ o.truncateBufferAfterLastEqual(candidate[idx+1:])
+ selected = candidate[:idx]
break
}
}
- o.op.buf.WriteRunes(candidate[:detailIndex])
+ o.op.buf.WriteRunes(selected)
}
func (o *opCompleter) doSelect() {
@@ -107,7 +122,7 @@
// only Aggregate candidates in non-complete mode
if !o.IsInCompleteMode() {
if len(newLines) == 1 {
- buf.WriteRunes(newLines[0])
+ o.writeRunes(newLines[0])
o.ExitCompleteMode(false)
return true
}