Skip to content

Instantly share code, notes, and snippets.

@danny8376
Created September 29, 2024 18:12
Show Gist options
  • Save danny8376/552f623d79cc8ac0ddb4867b1d1c6481 to your computer and use it in GitHub Desktop.
Save danny8376/552f623d79cc8ac0ddb4867b1d1c6481 to your computer and use it in GitHub Desktop.
Try additional file extensions in msys2 shell (bash/zsh currently)
path_try_additional_ext_find_names() {
local fn="$1" result=()
shift
result+=("(" "-name" "$fn.$1")
shift
while (( $# )); do
result+=("-o" "-name" "$fn.$1")
shift
done
result+=(")")
echo "${result[@]}"
}
path_try_additional_ext() {
local exts=(
"bat"
)
#local cmd="$(echo $PATH | tr -d '\n' | sed 's#\([a-zA-Z]\):\\#/\1/#g ; s#\\#/#g ; s/:/\x0/g' | find -files0-from - -maxdepth 1 $(path_try_additional_ext_find_names $1 "${exts[@]}") 2>/dev/null | head -n 1)"
local cmd="$(ash -c 'echo $PATH' | tr -d '\n' | sed 's#\([a-zA-Z]\):\\#/\1/#g ; s#\\#/#g ; s/:/\x0/g' | find -files0-from - -maxdepth 1 $(path_try_additional_ext_find_names $1 "${exts[@]}") 2>/dev/null | head -n 1)"
[[ -z "$cmd" ]] && return 127
shift
"$cmd" "$@"
}
if [[ -n "$BASH_VERSION" ]]; then
if declare -f command_not_found_handle >/dev/null; then
eval "path_try_additional_ext_bak() $(declare -f command_not_found_handle | tail -n +2)"
command_not_found_handle() {
path_try_additional_ext "$@"
local ret=$?
if [[ $ret -eq 127 ]]; then
path_try_additional_ext_bak "$@"
return $?
fi
return $ret
}
else
command_not_found_handle() {
path_try_additional_ext "$@"
local ret=$?
[[ $ret -eq 127 ]] && echo "$(basename $0): command not found: $1"
return $ret
}
fi
elif [[ -n "$ZSH_VERSION" ]]; then
if declare -f command_not_found_handler >/dev/null; then
eval "path_try_additional_ext_bak() { $(declare -f command_not_found_handler | tail -n +2)"
command_not_found_handler() {
path_try_additional_ext "$@"
local ret=$?
if [[ $ret -eq 127 ]]; then
path_try_additional_ext_bak "$@"
return $?
fi
return $ret
}
else
command_not_found_handler() {
path_try_additional_ext "$@"
local ret=$?
[[ $ret -eq 127 ]] && echo "$(basename $0): command not found: $1"
return $ret
}
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment