Do not auto-complete directories in shell completions (only entries)

This commit is contained in:
2024-03-18 14:39:55 -04:00
parent 26a5898e15
commit e4de8f930b
3 changed files with 21 additions and 19 deletions
+12 -11
View File
@@ -1,24 +1,25 @@
# mutn(1) completion
_path_gen() {
local mutn_path="$HOME/.local/share/libmutton"
local glob_status=$(shopt -p globstar)
[ -z "${glob_status##*u*}" ] && shopt -s globstar # if recursive globbing is disabled, enable it
local full_paths=("$mutn_path"/**/*)
$glob_status # set recursive globbing to user default
for scan_path in "${full_paths[@]}"; do
trimmed_paths+=("${scan_path#$mutn_path}")
local mutnPath="$HOME/.local/share/libmutton"
local globStatus=$(shopt -p globstar)
[ -z "${globStatus##*u*}" ] && shopt -s globstar # if recursive globbing is disabled, enable it
local fullPaths=("$mutnPath"/**/*)
$globStatus # set recursive globbing to user default
for scanPath in "${fullPaths[@]}"; do
# exclude directories
[ -f "$scanPath" ] && trimmedPaths+=("${scanPath#$mutnPath}")
done
if [ "${trimmed_paths[0]}" == '/**/*' ]; then trimmed_paths[0]=help; fi
if [ "${trimmedPaths[0]}" == '' ]; then trimmedPaths[0]=help; fi
} &&
_mutn_completions() {
_mutnCompletions() {
local cur=${COMP_WORDS[COMP_CWORD]}
local prev=${COMP_WORDS[COMP_CWORD-1]}
case $prev in
mutn )
while read -r; do ITEM=${REPLY// /\\ }; COMPREPLY+=( "$ITEM" ); done < <( compgen -W "$(printf "'%s' " "${trimmed_paths[@]}")" -- "$cur" )
while read -r; do ITEM=${REPLY// /\\ }; COMPREPLY+=( "$ITEM" ); done < <( compgen -W "$(printf "'%s' " "${trimmedPaths[@]}")" -- "$cur" )
;;
/* )
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "copy edit gen add shear" -- "$cur" )
@@ -38,4 +39,4 @@ _mutn_completions() {
esac
} &&
_path_gen && complete -F _mutn_completions mutn
_path_gen && complete -F _mutnCompletions mutn
+1 -1
View File
@@ -2,7 +2,7 @@ function MUTNEntryCompleter {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
#$entryPath = (Resolve-Path '~/.local/share/libmutton').Path # UNIX testing
$entryPath = (Resolve-Path '~/AppData/Local/libmutton/entries').Path
$entryNames = If (Test-Path $entryPath) {(Get-ChildItem -Path $entryPath -Recurse).FullName.Substring($entryPath.Length) -replace '\\', '/' -replace ' ', '` '}
$entryNames = If (Test-Path $entryPath) {(Get-ChildItem -Path $entryPath -Recurse -File).FullName.Substring($entryPath.Length) -replace '\\', '/' -replace ' ', '` '}
$entryNames | Where-Object { $_ -like "$wordToComplete*" }
}
+8 -7
View File
@@ -1,16 +1,17 @@
#compdef mutn
mutn_path="$HOME/.local/share/libmutton"
full_paths=("$mutn_path"/**/*)
trimmed_paths=()
for scan_path in "${full_paths[@]}"; do
trimmed_paths+=("${scan_path#$mutn_path}")
mutnPath="$HOME/.local/share/libmutton"
fullPaths=("$mutnPath"/**/*)
trimmedPaths=()
for scanPath in "${fullPaths[@]}"; do
# exclude directories
[ -f "$scanPath" ] && trimmedPaths+=("${scanPath#$mutnPath}")
done
[[ -z $trimmed_paths ]] && trimmed_paths=(help)
[[ -z $trimmedPaths ]] && trimmedPaths=(help)
case ${words[-2]} in
mutn )
compadd $trimmed_paths
compadd $trimmedPaths
;;
/* )
compadd {copy,edit,gen,add,shear}