diff --git a/bin/dot b/bin/dot index 8e2fa18a8..a641cf100 100755 --- a/bin/dot +++ b/bin/dot @@ -8,20 +8,20 @@ source "$DOTLY_PATH/scripts/core/_main.sh" ##? dot ##? dot ##? dot [...] +##? dot -h -list_command_paths() { - find "$DOTFILES_PATH/scripts" -maxdepth 2 -perm +111 -type f | - grep -v core | - sort +exit_if_empty() { + [ "${1}" == "" ] && die "No executable scripts" || true } fzf_prompt() { local paths="$1" + exit_if_empty "$paths" match="$(echo "$paths" | xargs -I % sh -c 'echo "$(basename $(dirname %)) $(basename %)"' | - fzf --height 100% --preview 'dot $(echo {} | cut -d" " -f 1) $(echo {} | cut -d" " -f 2) -h | bat --style=numbers')" - printf "$match " + fzf --height 100% --preview 'dot -h $(echo {} | cut -d" " -f 1) $(echo {} | cut -d" " -f 2)')" + printf "$match" read args if coll::is_empty "$args"; then dot $match @@ -30,10 +30,35 @@ fzf_prompt() { fi } +is_executable() { + [[ -x "${1}/scripts/${2}/${3}" ]] +} + +ask_for_help() { + [ "${1}" == "true" ] +} + +generate_help () { + bat --color always --style=numbers "${1}/scripts/${2}/${3}" +} + +help="false" +while getopts ":h" opt; do + shift 1 + case $opt in + h) help="true" + ;; + [?]) + echo "Invalid option -$opt" >&2 + exit 1 + ;; + esac +done + if args::has_no_args "$@"; then - fzf_prompt "$(list_command_paths)" + fzf_prompt "$(paths::list_scripts_sorted)" elif args::total_is 1 "$@"; then - fzf_prompt "$(list_command_paths | grep "/$1/")" + fzf_prompt "$(paths::list_scripts_sorted | grep "/$1/")" else context="$1" command="$2" @@ -41,6 +66,14 @@ else shift 2 export DOT_TRACE=${TRACE:-false} - # @todo HERE we have to merge custom and dotly scripts - "${DOTLY_PATH}/scripts/${context}/${command}" "$@" + script_path="" + is_executable "$DOTLY_PATH" "$context" "$command" && script_path="$DOTLY_PATH" + is_executable "$DOTFILES_PATH" "$context" "$command" && script_path="$DOTFILES_PATH" + exit_if_empty "$script_path" + + if ask_for_help $help; then + generate_help "${script_path}" "${context}" "${command}" + else + "${script_path}/scripts/${context}/${command}" "$@" + fi fi diff --git a/scripts/core/_main.sh b/scripts/core/_main.sh index 1436f4c15..78fa459d2 100755 --- a/scripts/core/_main.sh +++ b/scripts/core/_main.sh @@ -7,6 +7,7 @@ if ! ${DOT_MAIN_SOURCED:-false}; then source "$DOTLY_PATH/scripts/core/platform.sh" source "$DOTLY_PATH/scripts/core/output.sh" source "$DOTLY_PATH/scripts/core/str.sh" + source "$DOTLY_PATH/scripts/core/paths.sh" readonly DOT_MAIN_SOURCED=true fi diff --git a/scripts/core/args.sh b/scripts/core/args.sh index 238c2391f..41a3832a6 100755 --- a/scripts/core/args.sh +++ b/scripts/core/args.sh @@ -1,4 +1,4 @@ -#!/bin/user/env bash +#!/usr/bin/env bash args::total_is() { total_expected="${1}" diff --git a/scripts/core/collections.sh b/scripts/core/collections.sh index 42f382146..dff8abea9 100755 --- a/scripts/core/collections.sh +++ b/scripts/core/collections.sh @@ -1,4 +1,4 @@ -#!/bin/user/env bash +#!/usr/bin/env bash coll::is_empty() { local var=${1} diff --git a/scripts/core/paths.sh b/scripts/core/paths.sh new file mode 100644 index 000000000..ab3be45e5 --- /dev/null +++ b/scripts/core/paths.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash + +paths::list_dotly_scripts() { + find "$DOTLY_PATH/scripts" -maxdepth 2 -perm +111 -type f | + grep -v core +} + +paths::list_dotfiles_scripts() { + find "$DOTFILES_PATH/scripts" -maxdepth 2 -perm +111 -type f | + grep -v core +} + +paths::list_all_scripts() { + paths::list_dotly_scripts + paths::list_dotfiles_scripts +} + +paths::list_scripts_sorted() { + paths::list_all_scripts | sort +} diff --git a/scripts/core/platform.sh b/scripts/core/platform.sh index 882c7731a..f15a70d10 100644 --- a/scripts/core/platform.sh +++ b/scripts/core/platform.sh @@ -1,3 +1,5 @@ +#!/usr/bin/env bash + platform::command_exists() { type "$1" >/dev/null 2>&1 }