This is an automated email from the ASF dual-hosted git repository.

shwstppr pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudstack-cloudmonkey.git


The following commit(s) were added to refs/heads/main by this push:
     new 03342d0  autocomplete: allow completion based on name/detail (#165)
03342d0 is described below

commit 03342d025bb4dccc21c589de84faacd1aa8f2f17
Author: Abhishek Kumar <[email protected]>
AuthorDate: Mon Aug 25 17:51:10 2025 +0530

    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 <[email protected]>
---
 cli/completer.go                              |  6 ++++--
 vendor/github.com/chzyer/readline/complete.go | 23 +++++++++++++++++++----
 2 files changed, 23 insertions(+), 6 deletions(-)

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 @@ func (t *autoCompleter) Do(line []rune, pos int) (options 
[][]rune, offset int)
                                        })
                                }
                                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 @@ func (t *autoCompleter) Do(line []rune, pos int) (options 
[][]rune, offset int)
                        }
                        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 @@ func newOpCompleter(w io.Writer, op *Operation, width int) 
*opCompleter {
                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 @@ func (o *opCompleter) OnComplete() bool {
        // 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
                }

Reply via email to