Skip to content

Commit

Permalink
Add a basic command plate to choose commands created by the current e…
Browse files Browse the repository at this point in the history
…nv file
  • Loading branch information
sherpalabsio committed Feb 4, 2025
1 parent 771d29e commit 2453358
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
14 changes: 14 additions & 0 deletions lib/cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Basic Commands:
symlink [PATH] - Symlink the current env file | Aliases: link, slink
dump - Dump the current env file to a .envrc.example file
reload - Reload the current env | Alias: r
command_plate - Offer a list of commands from the current env file and run the selected one
Troubleshooting:
status - Show debug status info | Aliases: s, stat
Expand Down Expand Up @@ -49,6 +50,7 @@ Log levels:
symlink | link | slink) _sherpa_cli_symlink "$2" ;;
dump) _sherpa_cli_dump_current_env ;;
r | reload) _sherpa_cli_reload ;;
command_plate) _sherpa_cli_command_plate ;;
-v | --version | version) echo "$version_info" ;;
-h | --help | help | "") echo "$usage_text" ;;
*) echo "Sherpa doesn't understand what you mean" ;;
Expand Down Expand Up @@ -175,6 +177,18 @@ _sherpa_cli_reload() {
_sherpa_unload_env_of_current_dir && _sherpa_load_env_for_current_dir
}

_sherpa_cli_command_plate() {
[ -f "$SHERPA_ENV_FILENAME" ] || return 1

# Warn the user if fzf is not installed
if ! command -v fzf > /dev/null; then
_sherpa_log_error "fzf is not installed. Please install it to use this feature."
return 1
fi

_sherpa_command_plate
}

_sherpa_dump_current_env() {
# Check if the feature is enabled
[ "$SHERPA_DUMP_ENV_ON_EDIT" != true ] && return
Expand Down
54 changes: 54 additions & 0 deletions lib/command_plate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
_sherpa_command_plate() {
local commands selected_command

# shellcheck disable=SC2207
commands=(
$(__sherpa_command_plate__load_local_aliases)
$(__sherpa_command_plate__load_local_functions)
)

# Filter and sort the commands
# shellcheck disable=SC2207
commands=(
$(
printf "%s\n" "${commands[@]}" |
awk 'length >= 2' | # Remove short commands
sort
)
)

selected_command=$(
printf "%s\n" "${commands[@]}" |
fzf --layout=reverse \
--border \
--info=inline \
--margin=8,20 \
--padding=1 \
--cycle
)

if [ -n "$selected_command" ]; then
echo "Running: $selected_command"
eval "$selected_command"
fi
}

__sherpa_command_plate__load_local_aliases() {
unalias -a

# shellcheck disable=SC1090
source "$SHERPA_ENV_FILENAME" &> /dev/null

compgen -a
}

__sherpa_command_plate__load_local_functions() {
local -r filter_pattern="^[[:space:]]*([[:alnum:]_]+[[:space:]]*\(\)|function[[:space:]]+[[:alnum:]_]+)"

# Cleanup:
# "function_1()" -> "function_1"
# "function function_2()" -> "function_2()" -> "function_2"
grep -oE "$filter_pattern" "$SHERPA_ENV_FILENAME" |
sed "s/function //" |
sed "s/()//"
}
3 changes: 2 additions & 1 deletion lib/init.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
if [ -n "$ZSH_VERSION" ]; then
SHERPA_LIB_DIR=$(
cd -- "$(dirname "$0")" >/dev/null 2>&1
cd -- "$(dirname "$0")" > /dev/null 2>&1
pwd -P
)
else
Expand Down Expand Up @@ -33,6 +33,7 @@ source "$SHERPA_LIB_DIR/env_stash/functions.sh"
source "$SHERPA_LIB_DIR/env_file_parser.sh"
source "$SHERPA_LIB_DIR/setup_cd_hook.sh"
source "$SHERPA_LIB_DIR/load_unload.sh"
source "$SHERPA_LIB_DIR/command_plate.sh"

source "$SHERPA_LIB_DIR/cli.sh"

Expand Down

0 comments on commit 2453358

Please sign in to comment.