Files
dotfiles/env/.local/scripts/hypr/utils-launcher

131 lines
2.8 KiB
Bash
Executable File

#!/usr/bin/env bash
THIS=$(basename ${BASH_SOURCE[0]})
usage() {
cat <<EOF
A fuzzy finder to launch utility applications / scripts from a single menu.
USAGE:
$ $THIS [OPTIONS]
OPTIONS:
-c | --config <file>: Set the location of the configuration file (default: XDG_CONFIG_HOME/utils-launcher/config.json)
-l | --launch: Launches in a new terminal window.
EOF
}
window_class="com.ghostty.$THIS"
window_padding_x="2"
config_file=""
launch_flag="0"
rows=()
invocation_id=${RANDOM}
LOG_FILE=${LOG_FILE:-/tmp/$THIS.log}
XDG_CONFIG_HOME=${XDG_CONFIG_HOME}
SCRIPTS=${SCRIPTS}
while [[ $# -gt 0 ]]; do
if [[ $1 == "-c" ]] || [[ $1 == "--config" ]]; then
shift
config_file="$1"
elif [[ $1 == "-l" ]] || [[ $1 == "--launch" ]]; then
launch_flag="1"
fi
shift
done
log() {
echo "[$invocation_id]:[$($SCRIPTS/isosec)]:[$THIS]: $1" >>$LOG_FILE
}
if [[ -z $XDG_CONFIG_HOME ]]; then
log "XDG_CONFIG_HOME not set"
log "using ~/.config"
XDG_CONFIG_HOME=$HOME/.config
fi
launch() {
ghostty --class=$window_class --window-padding-x=$window_padding_x \
--keybind="ctrl+c=quit" \
-e ${BASH_SOURCE[0]} --config $config_file
}
footer() {
cat <<'EOF'
__ ______________ _____
/ / / /_ __/ _/ / / ___/
/ / / / / / / // / \__ \
/ /_/ / / / _/ // /______/ /
\____/ /_/ /___/_____/____/
EOF
}
generate_rows() {
readarray -t names <<<"$(echo "$1" | jq -r '.[] | .name')"
readarray -t execs <<<"$(echo "$1" | jq -r '.[] | .exec')"
for i in "${!names[@]}"; do
rows+=("${execs[i]}|${names[i]}")
done
}
################################################################################
# MAIN
################################################################################
if [[ -z $config_file ]]; then
log "No config file set."
log "Using ~/.config/utils-launcher/config.json"
config_file="$XDG_CONFIG_HOME/utils-launcher/config.json"
fi
if [[ -z $SCRIPTS ]]; then
log "SCRIPTS not set"
log "using ~/.local/scripts"
SCRIPTS=$HOME/.local/scripts
fi
if [[ $launch_flag == "1" ]]; then
launch && exit 0
fi
if [[ ! -f $config_file ]]; then
log "[ERROR]: no config file set" && exit 1
fi
file_data=$(cat $config_file)
# Setup colors before calling fzf.
[[ -f $SCRIPTS/catppuccin-colors ]] && source $SCRIPTS/catppuccin-colors
generate_rows "$file_data"
sel=$(
printf "%s\n" "${rows[@]}" |
fzf --style=full --footer="$(footer)" --with-nth=2 --delimiter='|' \
--preview-label='[ Command ]' \
--preview='printf "\nName: {2}\nExec: {1}"'
)
log "Selection: $sel"
if [[ -n "$sel" ]]; then
# Parse the exec command for the selection.
exec_cmd=${sel%%|*}
log "Exec: '$exec_cmd'"
if [[ -z $exec_cmd ]]; then
log "[ERROR]: Command is empty." && exit 1
fi
eval exec uwsm app -- "$exec_cmd"
else
log "No selection."
fi