Website/bin/just/choose.sh

56 lines
1.4 KiB
Bash
Raw Normal View History

2024-07-12 22:56:04 +00:00
#!/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
2024-07-30 03:36:45 +00:00
hash_highlight "$(just_wrapper --list $recipe)" "conf"
2024-07-12 22:56:04 +00:00
else
2024-07-30 03:36:45 +00:00
hash_highlight "$(just_wrapper --show $recipe)" "sh"
2024-07-12 22:56:04 +00:00
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'
}
2024-07-30 03:36:45 +00:00
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
}
2024-07-12 22:56:04 +00:00
case "$action" in
*choose) choose;;
*preview) preview;;
esac