From 4d1b5407784e97ba461d336339f7d8bf1be34757 Mon Sep 17 00:00:00 2001 From: druckdev <63563978+druckdev@users.noreply.github.com> Date: Mon, 2 Nov 2020 17:46:26 +0100 Subject: [PATCH] zsh:ls-show-hidden: Bug fixes around -d flag Fix behavior to match ls's when the -d flag but no files or directories were passed. Fix bug that the function thinks -d is passed when a long option that contains a 'd' (as --group-directories-first) is specified and thus just emulates plain `ls` in that case. --- .config/zsh/autoload/ls-show-hidden | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/.config/zsh/autoload/ls-show-hidden b/.config/zsh/autoload/ls-show-hidden index 8332389..58cda65 100755 --- a/.config/zsh/autoload/ls-show-hidden +++ b/.config/zsh/autoload/ls-show-hidden @@ -29,16 +29,26 @@ done # Print working directory when only flags were given as arguments. if ! (( ${#dirs} + ${#files} )); then - dirs+="$PWD" + dirs+=. fi -# Do not separate files and directories if -d flag was specified. +# Just pass everything to ls when the -d flag is given since then the -A flag +# makes no difference. +# Remove all long options because `getopts` cannot handle those and sees for +# example -d in --group-directories-first. Remove the resulting empty strings +# afterwards since that confuses `getopts` apparently. +builtin local -a all_opts empty +all_opts=("$@") +empty=("") +set -- "${(@)${(@)all_opts//--*}:|empty}" while getopts d flag 2>/dev/null; do - if [[ "$flag" = "d" ]]; then - command ls "$@" -- "${files[@]}" "${dirs[@]}" - return - fi + [[ "$flag" = "d" ]] || continue + command ls "${all_opts[@]}" -- "${files[@]}" "${dirs[@]}" + return done +# Restore options. +set -- "${all_opts[@]}" +unset all_opts empty builtin local separator=""