Website/bin/just/choose.sh
Snoweuph bf11292a85
All checks were successful
Quality Check / QS Backend (push) Successful in 17s
Quality Check / QS Frontend (push) Successful in 34s
Switch to Symfony UX Twig Components, Icons and Stimulus
2024-07-21 20:39:34 +02:00

55 lines
1.4 KiB
Bash
Executable file

#!/bin/bash
justfile=$1
action=$2
content=$3
function preview() {
source "$(dirname ${BASH_SOURCE[0]})/colors.sh"
recipe=$(choose_truncate $content)
if [[ -z "$recipe" ]] || echo $recipe | grep -q "^\["; then
printf "${RED_FG}Select this to Exit${CLEAR}"
elif [[ "$content" == *" ..." ]]; then
hash_highlight "$(just_wrapper --list $recipe)" "conf"
else
hash_highlight "$(just_wrapper --show $recipe)" "sh"
fi
}
function choose() {
script_path="$(pwd)/${BASH_SOURCE[0]}"
choice=$( choose_truncate $(choose_list | fzf --tac --cycle --preview "$script_path $justfile preview {}" ))
if [[ -z "$choice" ]] || echo $choice | grep -q "^\["; then
exit
else
just_wrapper $choice
fi
}
function just_wrapper() {
just --justfile $justfile $@
}
function choose_truncate() {
echo $1 | cut -d ' ' -f1
}
function choose_list() {
just_wrapper -l --no-aliases --list-heading="" | awk '{$1=$1};1'
}
function hash_highlight() {
cache_dir="$(dirname ${BASH_SOURCE[0]})/.cache"
hash=$(echo $1 | md5sum | cut -d ' ' -f1)
file="$cache_dir/$hash"
if [[ -e $file ]]; then
cat $file
else
mkdir -p $cache_dir
data=$(printf "$1" | highlight --out-format xterm256 --syntax "$2")
printf "$data"
printf "$data" > $file
fi
}
case "$action" in
*choose) choose;;
*preview) preview;;
esac