jmcdo29 / nest-commander

A module for using NestJS to build up CLI applications
https://nest-commander.jaymcdoniel.dev/
MIT License
435 stars 53 forks source link

Autocomplete Functionality with Filesystem Support #1119

Closed silte closed 7 months ago

silte commented 7 months ago

Is there an existing issue that is already proposing this?

Is your feature request related to a problem? Please describe it

The current implementation of Nest Commander does not support filesystem autocomplete in the zsh shell. This means that when users are typing commands, they do not receive suggestions for file paths, which can slow down their workflow. Additionally, the autocomplete function currently requires starting the commander app, which can be time-consuming.

Describe the solution you'd like

The proposed patch enhances the autocomplete functionality by adding support for filesystem autocomplete in the zsh shell. This means that when users start typing a file path, they will receive autocomplete suggestions, speeding up their workflow. The patch also allows for faster filesystem autocomplete without the need to start the commander app.

Teachability, documentation, adoption, migration strategy

Existing users can be informed about the update through release notes. As this is an enhancement of existing functionality, migration should be straightforward, with users simply needing to update to the latest version of Nest Commander.

What is the motivation / use case for changing the behavior?

The motivation for this change is to improve the user experience by making command line operations more efficient. By supporting filesystem autocomplete, users can navigate file paths more quickly and accurately. This is particularly useful for users who frequently work with complex file structures. The ability to use autocomplete without starting the commander app also saves time, making the tool more efficient.

silte commented 7 months ago

I can open a new pull request with the following changes if this is wanted for this project.

diff --git a/node_modules/nest-commander/src/constants.js b/node_modules/nest-commander/src/constants.js
index 9a8e630..7326bad 100644
--- a/node_modules/nest-commander/src/constants.js
+++ b/node_modules/nest-commander/src/constants.js
@@ -44,6 +44,16 @@ _{{app_name}}_nest_commander_completions()
     cur_word="\${COMP_WORDS[COMP_CWORD]}"
     args=("\${COMP_WORDS[@]}")

+    # Check if {{app_name}}_nest_commander_filesystem_completions is set to "true" or "1"
+    # If it is, use filename completion if last word starts with common filesystem operator chars
+    #  \`.\` , \`/\` or \`~\` e.g. \`./\`, \`../\`, \`/\`, \`~/\` 
+    if [[ "\${{{app_name}}_nest_commander_filesystem_completions}" == "true" ]] || [[ "\${{{app_name}}_nest_commander_filesystem_completions}" == "1" ]]; then
+        if [[ "\${cur_word}" =~ ^\.  ]] || [[ "\${cur_word}" =~ ^/ ]] || [[ "\${cur_word}" =~ ^~ ]]; then
+            COMPREPLY=($(compgen -f -- "\${cur_word}"))
+            return 0
+        fi
+    fi
+
     # ask nest commander to generate completions.
     type_list=$({{app_path}} completion "\${args[@]}")

@@ -71,8 +81,28 @@ _{{app_name}}_nest_commander_completions()
 {
   local reply
   local si=$IFS
-  IFS=$'\n' reply=($(COMP_CWORD="$((CURRENT-1))" COMP_LINE="$BUFFER" COMP_POINT="$CURSOR" {{app_path}} completion "\${words[@]}"))
+
+  # Check if {{app_name}}_nest_commander_filesystem_completions is set to "true" or "1"
+  # If it is, use zsh filename completion if last word starts with common filesystem operator chars
+  #  \`.\` , \`/\` or \`~\` e.g. \`./\`, \`../\`, \`/\`, \`~/\` 
+  if [[ "\${{{app_name}}_nest_commander_filesystem_completions}" == "true" ]] || [[ "\${{{app_name}}_nest_commander_filesystem_completions}" == "1" ]]; then
+    if [[ "\${words[-1]}" =~ ^\.  ]] || [[ "\${words[-1]}" =~ ^/ ]] || [[ "\${words[-1]}" =~ ^~ ]]; then
+      # _path_files is a zsh function that will list all files in the current directory
+      _path_files
+      return
+    fi
+  fi
+
+  IFS=$'\n'
+  reply=($(COMP_CWORD="$((CURRENT-1))" COMP_LINE="$BUFFER" COMP_POINT="$CURSOR" {{app_path}} completion "\${words[@]}"))
   IFS=$si
+
+  # if no match was found, fall back to filename completion
+  if [[ -z "\${reply[*]}" ]]; then
+    _path_files
+    return
+  fi
+
   _describe 'values' reply
 }
 compdef _{{app_name}}_nest_commander_completions {{app_name}}
jmcdo29 commented 7 months ago

Looks helpful. Feel free to open a PR with it so I'll be able to see it in the files :smile:

Avivbens commented 7 months ago

Just want to make sure the core functionality of completion for commands & options remained the same 🙏

I'm the original contributor of the completion feature

@silte

Avivbens commented 6 months ago

@jmcdo29 🙏 ⬆️

silte commented 5 months ago

@Avivbens

I apologize for the delayed response; my day-to-day job has been quite hectic lately.

Regarding the autocomplete functionality, there have been no changes to the core completion mechanics for commands and options. The update merely extends the same filesystem autocomplete support that Bash had to Zsh. This is activated when NestJS returns an empty result, and it adds an opt-in feature controlled by an environment variable ({{app_name}}_nest_commander_filesystem_completions). This enhancement significantly boosts the user experience by providing instant filesystem autocomplete suggestions, when the user’s last argument begins with common filesystem operators such as ., /, or ~ (e.g. ./, ../, /home/user, ~/), which eliminates the roughly 1-second delay typically encountered when initiating the NestJS application.

I hope this clarifies any concerns.

Avivbens commented 5 months ago

@Avivbens

I apologize for the delayed response; my day-to-day job has been quite hectic lately.

Regarding the autocomplete functionality, there have been no changes to the core completion mechanics for commands and options. The update merely extends the same filesystem autocomplete support that Bash had to Zsh. This is activated when NestJS returns an empty result, and it adds an opt-in feature controlled by an environment variable ({{app_name}}_nest_commander_filesystem_completions). This enhancement significantly boosts the user experience by providing instant filesystem autocomplete suggestions, when the user’s last argument begins with common filesystem operators such as ., /, or ~ (e.g. ./, ../, /home/user, ~/), which eliminates the roughly 1-second delay typically encountered when initiating the NestJS application.

I hope this clarifies any concerns.

Hi 👋

No worries, we're all busy these days.

Thank you for the clarification! Appreciate your hard work over this change, making this tool even better ✨