Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 43 additions & 10 deletions bin/dot
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ source "$DOTLY_PATH/scripts/core/_main.sh"
##? dot
##? dot <context>
##? dot <context> <cmd> [<args>...]
##? dot -h <context> <cmd>

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
Expand All @@ -30,17 +30,50 @@ 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"

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
1 change: 1 addition & 0 deletions scripts/core/_main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion scripts/core/args.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/user/env bash
#!/usr/bin/env bash

args::total_is() {
total_expected="${1}"
Expand Down
2 changes: 1 addition & 1 deletion scripts/core/collections.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/user/env bash
#!/usr/bin/env bash

coll::is_empty() {
local var=${1}
Expand Down
20 changes: 20 additions & 0 deletions scripts/core/paths.sh
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 2 additions & 0 deletions scripts/core/platform.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

platform::command_exists() {
type "$1" >/dev/null 2>&1
}
Expand Down