94 lines
3.0 KiB
Bash
Executable File
94 lines
3.0 KiB
Bash
Executable File
#!/bin/sh
|
|
# Font preview with ueberzug and fzf
|
|
# https://github.com/OliverLew/fontpreview-ueberzug
|
|
# Dependencies: imagemagick, ueberzug, fzf, fontconfig
|
|
# This is inspired by https://github.com/sdushantha/fontpreview
|
|
|
|
# Checking for environment variables if available.
|
|
# These are compatible with original fontpreview.
|
|
SIZE=${FONTPREVIEW_SIZE:-800x800}
|
|
FONT_SIZE=${FONTPREVIEW_FONT_SIZE:-50}
|
|
BG_COLOR=${FONTPREVIEW_BG_COLOR:-#080808}
|
|
FG_COLOR=${FONTPREVIEW_FG_COLOR:-#b3b1ad}
|
|
PREVIEW_TEXT=${FONTPREVIEW_PREVIEW_TEXT:-"Jackdaws Love\nMy Big Sphinx\nOf Quartz\n\n1234567890\n¡!@#$\%|^&*\n',.;:_-=+\"\n(){}[]<>¿?ñÑ"}
|
|
|
|
# Ueberzug related variables
|
|
FIFO="/tmp/fontpreview-ueberzug-fifo"
|
|
IMAGE="/tmp/fontpreview-ueberzug-img.png"
|
|
ID="fontpreview-ueberzug"
|
|
WIDTH=$(tput cols)
|
|
HEIGHT=$(tput lines)
|
|
|
|
usage() {
|
|
echo "Usage: fontpreview [-h] [-s FONT_SIZE] [-b BG] [-f FG] [-t TEXT]"
|
|
}
|
|
|
|
start_ueberzug() {
|
|
mkfifo "$FIFO" || exit 1
|
|
touch "$IMAGE" || exit 1
|
|
ueberzug layer --parser simple --silent <"$FIFO" &
|
|
# https://github.com/seebye/ueberzug/issues/54#issuecomment-502869935
|
|
exec 3>"$FIFO"
|
|
}
|
|
|
|
stop_ueberzug() {
|
|
exec 3>&-
|
|
rm "$FIFO" "$IMAGE" >/dev/null 2>&1
|
|
}
|
|
|
|
preview() {
|
|
# In fzf the cols and lines are those of the preview pane
|
|
[ -f "$1" ] || fontfile=$(fc-match -f "%{file}" "$1")
|
|
convert -size "$SIZE" xc:"$BG_COLOR" -fill "$FG_COLOR" \
|
|
-pointsize "$FONT_SIZE" -font "$fontfile" -gravity center \
|
|
-annotate +0+0 "$PREVIEW_TEXT" "$IMAGE" &&
|
|
{
|
|
printf "action\tadd\tidentifier\t%s\t" "$ID"
|
|
printf "x\t%d\ty\t%d\tpath\t%s\t" 2 1 "$IMAGE"
|
|
printf "width\t%d\theight\t%d\n" "$WIDTH" "$HEIGHT"
|
|
} >"$FIFO" ||
|
|
printf "action\tremove\tidentifier\t%s\n" "$ID" >"$FIFO"
|
|
}
|
|
|
|
while getopts "hs:b:f:t:" arg; do
|
|
case "$arg" in
|
|
s) FONT_SIZE=$OPTARG ;;
|
|
b) BG_COLOR=$OPTARG ;;
|
|
f) FG_COLOR=$OPTARG ;;
|
|
t) PREVIEW_TEXT=$OPTARG ;;
|
|
*)
|
|
usage
|
|
exit
|
|
;;
|
|
esac
|
|
done
|
|
shift $((OPTIND - 1))
|
|
|
|
if [ "$#" = 0 ]; then
|
|
trap stop_ueberzug EXIT QUIT INT TERM
|
|
# Check if dependencies are installed
|
|
for dependency in ueberzug convert fzf fc-list; do
|
|
command -v "$dependency" >/dev/null 2>&1 ||
|
|
{
|
|
echo "Please install ${dependency}." >&2
|
|
exit 1
|
|
}
|
|
done
|
|
# Prepare
|
|
start_ueberzug
|
|
# Export cli args as environment variables for preview command
|
|
export FONTPREVIEW_FONT_SIZE="$FONT_SIZE"
|
|
export FONTPREVIEW_BG_COLOR="$BG_COLOR"
|
|
export FONTPREVIEW_FG_COLOR="$FG_COLOR"
|
|
export FONTPREVIEW_PREVIEW_TEXT="$PREVIEW_TEXT"
|
|
# The preview command runs this script again with an argument
|
|
fc-list -f "%{family}:%{style}\n" |
|
|
while IFS=: read -r family style; do
|
|
echo "${family%,*}${style:+:${style%,*}}"
|
|
done | sort | uniq |
|
|
fzf --reverse --cycle --preview "sh $0 {}" \
|
|
--preview-window "left:$((WIDTH / 2)):noborder:wrap"
|
|
elif [ "$#" = 1 ]; then
|
|
[ -p "$FIFO" ] && preview "$1"
|
|
fi
|