1
0
mirror of https://git.decapod.one/brethil/dotfiles synced 2024-11-06 04:11:31 +01:00
dotfiles/functions/asciinema.zsh
bretello 0ed9e5373c
zsh: add asciinema-switch
helps switch different asciinema profiles
2022-03-26 16:43:46 +01:00

56 lines
1.3 KiB
Bash

function warning {
echo -e "$(color $YELLOW Warning:) $@" >&2
}
function error {
echo -e "$(color $RED Error:) $@" >&2
}
function info {
echo -e "$(color $GREEN Info:) $@"
}
function switch-asciinema-user
{
local conf_dir="$HOME/.config/asciinema"
local current_id=$(basename $(readlink -f ${conf_dir}/install-id ))
function get_ids {
find ${conf_dir} -name 'install-id-*' -exec basename {} \;
}
if [[ $1 == "-l" || $1 == "--list" ]]; then
echo "$(color $BOLD Available ids): (current = $(color $UNDERLINE ${current_id}))"
for _id in $(get_ids); do
echo " $(green ➡️) ${_id}"
done
return
fi
local ids=$(get_ids)
local query
local new_id
if [[ -n "$@" ]]; then
query="-q $@"
fi
new_id=$(echo $ids| fzf --height=$(($(wc -l <<< $ids)+2)) ${query} )
if [[ -z $new_id ]]; then
warning "Keeping curent id ($(color $UNDERLINE ${current_id}))"
return
fi
if [[ "$new_id" == "$current_id" ]]; then
warning "id $new_id is already set."
return
fi
local id_file="${conf_dir}/${new_id}"
if [[ ! -f ${id_file} ]]; then
error "${id_file} does not exist"
return
fi
ln -sf "${id_file}" "${conf_dir}/install-id" && \
info "Set \"$new_id\"." || \
error "Could not set id \"$new_id\". (run with -l for a list of available profiles)"
}