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

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new d02fadc03 system/readline: fill in a single match on an empty prefix
d02fadc03 is described below

commit d02fadc03157f575a35cc8a3ada68ce9c0de4f9e
Author: Junbo Zheng <[email protected]>
AuthorDate: Fri Nov 20 10:58:21 2026 +0800

    system/readline: fill in a single match on an empty prefix
    
    Path completion only filled in the sole matching entry when at least
    one character of the word was typed, so "cd <TAB>" in a directory
    holding a single subdirectory printed the candidate as a list instead
    of completing the line. Fill in the match right away, and make a
    repeated TAB right after such a fill a no-op when the word is empty
    and the previous word already holds the match.
    
    Testing:
    
    - Built with cmake -B build -DBOARD_CONFIG=sim:nsh and
      cmake --build build -j, then ran ./build/nuttx.
    - In an NSH directory holding one file (only.txt) and one
      subdirectory (sub/):
    
        nsh> ls <TAB>        fills in "ls only.txt "
        nsh> cd <TAB>        fills in "cd sub/"
        nsh> ls <TAB><TAB>   fills once, the second TAB adds nothing
        nsh> ls onl<TAB>     prefix fills still work as before
    
    - With several matching candidates, "ls a<TAB>" still lists them.
    
    Signed-off-by: Junbo Zheng <[email protected]>
---
 system/readline/readline_common.c | 27 ++++++++++++++++++++++-----
 1 file changed, 22 insertions(+), 5 deletions(-)

diff --git a/system/readline/readline_common.c 
b/system/readline/readline_common.c
index 7ab3ed06d..2b2c9df14 100644
--- a/system/readline/readline_common.c
+++ b/system/readline/readline_common.c
@@ -442,14 +442,31 @@ static bool tab_path_completion(FAR struct rl_common_s 
*vtbl,
       return false;
     }
 
-  /* One match with something already typed: append what the user has not
-   * typed yet, plus '/' when it is a directory so a following TAB
-   * descends into it.  With an empty prefix the match is only listed
-   * below, so repeated TABs never append the same name over and over.
+  /* One match: fill in the rest of the name, plus '/' for a directory
+   * so a following TAB descends into it.  On an empty prefix the fill
+   * is skipped when the previous word already holds the match, so
+   * repeated TABs do not pile up copies of a completed file name.
    */
 
-  if (nr_matches == 1 && prefix_len > 0)
+  if (nr_matches == 1)
     {
+      if (prefix_len == 0 && word_start > 1 &&
+          buf[word_start - 1] == ' ')
+        {
+          int pstart = word_start - 1;
+
+          while (pstart > 0 && buf[pstart - 1] != ' ')
+            {
+              pstart--;
+            }
+
+          if (word_start - 1 - pstart == (int)strlen(single) &&
+              strncmp(buf + pstart, single, word_start - 1 - pstart) == 0)
+            {
+              return false;
+            }
+        }
+
       int add = strlen(single) - prefix_len;
 
       if (add < 0)

Reply via email to