git.lucas.co / fontpreview
git clone https://git.lucas.co/fontpreview.git

commite9d3b5edd68e6cc446a59393a4fea15840e04b55
authorSiddharth Dushantha <[email protected]>
date2020-02-08 13:24
add all files

 Makefile       |  13 +++++
 README.md      |  81 ++++++++++++++++++++++++++
 extra/demo.gif | Bin 0 -> 6219698 bytes
 extra/logo.png | Bin 0 -> 2493 bytes
 fontpreview    | 178 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 272 insertions(+)

diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..c33f354
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,13 @@
+DEST ?= /usr/bin
+
+all:
+	@echo Run \'make install\' to install fontpreview on your device
+
+install:
+	@cp fontpreview $(DEST)/fontpreview
+	@chmod 755 $(DEST)/fontpreview
+	@echo fontpreview has been installed on your device
+
+uninstall:
+	@rm -rf $(DEST)/fontpreview
+	@echo fontpreview has been removed from your device
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..553b91d
--- /dev/null
+++ b/README.md
@@ -0,0 +1,81 @@
+<p align="center"><img src="extra/logo.png"><br><sub>✨ Very customizable and minimal font previewer written in bash ✨</sub></p>
+
+`fontpreview` is a commandline tool that lets you **quickly search** for fonts
+that are installed on your machine and preview them. The **fuzzy search** feature
+is provided by `fzf` and the preview is generated with `imagemagick` and then
+displayed using `sxiv`. This tool is **highley customizeable**, almost all of the
+variables in this tool can be changed using the commandline flags or you can 
+configure them using environment variables.
+
+![](extra/demo.gif)
+
+## Dependencies
+
+- `xdotool`
+- `fzf`
+- `imagemagick`
+- `sxiv`
+
+## Installation
+### Install using ```make```
+```bash
+# Clone the repo
+$ git clone https://github.com/sdushantha/fontpreview
+
+# Change your current directory to fontpreview
+$ cd fontpreview
+
+# Install it
+$ sudo make install
+```
+
+## Usage
+```
+$ fontpreview --help
+usage: fontpreview [-h] [--size "px"] [--position "+x+y"] [--search-prompt SEARCH_PROMPT]
+                   [--font-size "FONT_SIZE"] [--bg-color "BG_COLOR"] [--fg-color "FG_COLOR"]
+                   [--preview-text "PREVIEW_TEXT"] [--version]
+ 
+┌─┐┌─┐┌┐┌┌┬┐┌─┐┬─┐┌─┐┬  ┬┬┌─┐┬ ┬
+├┤ │ ││││ │ ├─┘├┬┘├┤ └┐┌┘│├┤ │││
+└  └─┘┘└┘ ┴ ┴  ┴└─└─┘ └┘ ┴└─┘└┴┘
+Very customizable and minimal font previewer written in bash
+ 
+optional arguments:
+   -h, --help            show this help message and exit
+   --size                size of the font preview window
+   --position            the position where the font preview window should be displayed
+   --search-prompt       input prompt of fuzzy searcher
+   --font-size           font size
+   --bg-color            background color of the font preview window
+   --fg-color            foreground color of the font preview window
+   --preview-text        preview text that should be displayed in the font preview window
+   --version             show the version of kunst you are using
+```
+
+## Configure
+You can configure `fontpreview` through environment variables.
+
+This can be in your `.bashrc`, `.zshrc`, etc
+
+```bash
+# Input prompt of fuzzy searcher
+export FONTPREVIEW_SEARCH_PROMPT="❯ "
+
+# Size of the font preview window
+export FONTPREVIEW_SIZE=532x365
+
+# The position where the font preview window should be displayed
+export FONTPREVIEW_POSITION="+0+0"
+
+# Font size
+export FONTPREVIEW_FONT_SIZE=38
+
+# Background color of the font preview window
+export FONTPREVIEW_BG_COLOR="#ffffff"
+
+# Foreground color of the font preview window
+export FONTPREVIEW_FG_COLOR="#000000"
+
+# Preview text that should be displayed in the font preview window
+export FONTPREVIEW_PREVIEW_TEXT="ABCDEFGHIJKLM\nNOPQRSTUVWXYZ\nabcdefghijklm\nnopqrstuvwxyz\n1234567890\n!@$\%(){}[]"
diff --git a/extra/demo.gif b/extra/demo.gif
new file mode 100644
index 0000000..614dd2f
Binary files /dev/null and b/extra/demo.gif differ
diff --git a/extra/logo.png b/extra/logo.png
new file mode 100644
index 0000000..1c33708
Binary files /dev/null and b/extra/logo.png differ
diff --git a/fontpreview b/fontpreview
new file mode 100755
index 0000000..7b29017
--- /dev/null
+++ b/fontpreview
@@ -0,0 +1,178 @@
+#!/usr/bin/env bash
+#
+# Siddharth Dushantha 2020
+#
+# Dependencies: sxiv, imagemagick, xdotool, fzf
+
+FONT_PREVIEW="/tmp/font.png"
+VERSION=1.0.0
+
+# Default values
+SEARCH_PROMPT="❯ "
+SIZE=532x365
+POSITION="+0+0"
+FONT_SIZE=38
+BG_COLOR="#ffffff"
+FG_COLOR="#000000"
+PREVIEW_TEXT="ABCDEFGHIJKLM\nNOPQRSTUVWXYZ\nabcdefghijklm\nnopqrstuvwxyz\n1234567890\n!@$\%(){}[]"
+
+show_help() {
+	echo "usage: fontpreview [-h] [--size \"px\"] [--position \"+x+y\"] [--search-prompt SEARCH_PROMPT]"
+    echo "                   [--font-size \"FONT_SIZE\"] [--bg-color \"BG_COLOR\"] [--fg-color \"FG_COLOR\"]"
+    echo "                   [--preview-text \"PREVIEW_TEXT\"] [--version]"
+	echo " "
+	echo "┌─┐┌─┐┌┐┌┌┬┐┌─┐┬─┐┌─┐┬  ┬┬┌─┐┬ ┬"
+    echo "├┤ │ ││││ │ ├─┘├┬┘├┤ └┐┌┘│├┤ │││"
+    echo "└  └─┘┘└┘ ┴ ┴  ┴└─└─┘ └┘ ┴└─┘└┴┘"
+	echo "Very customizable and minimal font previewer written in bash"
+	echo " "
+	echo "optional arguments:"
+	echo "   -h, --help            show this help message and exit"
+	echo "   --size                size of the font preview window"
+    echo "   --position            the position where the font preview window should be displayed"
+    echo "   --search-prompt       input prompt of fuzzy searcher"
+    echo "   --font-size           font size"
+    echo "   --bg-color            background color of the font preview window"
+    echo "   --fg-color            foreground color of the font preview window"
+    echo "   --preview-text        preview text that should be displayed in the font preview window"
+	echo "   --version             show the version of kunst you are using"
+}
+
+pre_exit() {
+	# Get the proccess ID of this script and kill it.
+    # We are dumping the output of kill to /dev/null
+    # because if the user quits sxiv before they
+    # exit this script, an error will be shown
+    # from kill and we dont want that
+	kill -9 "$(cat /tmp/fontpreview.pid 2>/dev/null)" &> /dev/null
+ 
+}
+
+generate_preview(){
+    # Credits: https://bit.ly/2UvLVhM
+    convert -size $SIZE xc:"$BG_COLOR" \
+        -gravity center \
+        -pointsize $FONT_SIZE \
+        -font "$1" \
+        -fill "$FG_COLOR" \
+        -annotate +0+0 "$PREVIEW_TEXT" \
+        -flatten "$FONT_PREVIEW"
+}
+
+main(){
+    # Checkig if needed dependencies are installed
+    type -p xdotool &>/dev/null || {
+        echo "error: Could not find 'xdotool', is it installed?"
+        return
+    }
+    type -p sxiv &>/dev/null || {
+        echo "error: Could not find 'sxiv', is it installed?"
+        return
+    }
+    type -p convert &>/dev/null || {
+        echo "error: Could not find 'imagemagick', is it installed?"
+        return
+    }
+
+    # Checking for enviornment variables which the user might have set.
+    # This config file for fontpreview is pretty much the bashrc, zshrc, etc
+    # Majority of the variables in fontpreview can changed using the enviornment variables
+    # and this makes fontpreview very customizable
+    [[ $FONTPREVIEW_SEARCH_PROMPT != "" ]] && SEARCH_PROMPT=$FONTPREVIEW_SEARCH_PROMPT
+    [[ $FONTPREVIEW_SIZE != "" ]] && SIZE=$FONTPREVIEW_SIZE
+    [[ $FONTPREVIEW_POSITION != "" ]] && POSITION=$FONTPREVIEW_POSITION
+    [[ $FONTPREVIEW_FONT_SIZE != "" ]] && FONT_SIZE=$FONTPREVIEW_FONT_SIZE
+    [[ $FONTPREVIEW_BG_COLOR != "" ]] && BG_COLOR=$FONTPREVIEW_BG_COLOR
+    [[ $FONTPREVIEW_FG_COLOR != "" ]] && FG_COLOR=$FONTPREVIEW_FG_COLOR
+    [[ $FONTPREVIEW_PREVIEW_TEXT != "" ]] && PREVIEW_TEXT=$FONTPREVIEW_PREVIEW_TEXT
+
+    # Save the window ID of the terminal window fontpreview is executed in.
+    # This is so that when we open up sxiv, we can change the focus back to
+    # the terminal window, so that the user can search for the fonts without
+    # having to manualy change the focus back to the terminal.
+    xdotool getactivewindow > /tmp/terminal_window_id
+
+	# Flag to run some commands only once in the loop
+	FIRST_RUN=true
+
+    while true; do
+        # List out all the fonts which imagemagick is able to find, extract
+        # the font names and then pass them to fzf
+        font=$(convert -list font |  awk -F: '/^\s *Font: /{print substr($NF,2)}' | fzf --prompt="$SEARCH_PROMPT")
+
+        # Exit if nothing is returned by fzf, which also means that the user
+        # has pressed [ESCAPE]
+        [[ -z $font ]] && return
+
+        generate_preview "$font"
+
+        if [ $FIRST_RUN == true ]; then
+            FIRST_RUN=false
+
+            # Display the font preview using sxiv
+            sxiv -g "$SIZE$POSITION" "$FONT_PREVIEW" -N "fontpreview" -b &
+
+            # Change focus from sxiv, back to the terminal window
+            # so that user can continue to search for fonts without
+            # having to manually change focus back to the terminal window
+            xdotool windowfocus "$(cat /tmp/terminal_window_id)"
+
+            # Save the process ID so that we can kill
+            # sxiv when the user exits the script
+            echo $! >/tmp/fontpreview.pid
+    	fi
+    done
+}
+
+# Disable CTRL-Z because if we allowed this key press,
+# then the script would exit but, sxiv would still be
+# running
+trap "" SIGTSTP
+
+trap pre_exit EXIT
+
+# Parse the arguments
+options=$(getopt -o h --long position:,size:,version,search-prompt:,font-size:,bg-color:,fg-color:,preview-text:,help -- "$@")
+eval set -- "$options"
+
+while true; do
+    case "$1" in 
+        --size)
+            shift;
+            FONTPREVIEW_SIZE=$2
+            ;;
+        --position)
+            shift;
+            FONTPREVIEW_POSITION=$2
+            ;;
+        -h|--help)
+            show_help
+            exit
+            ;;
+        --version)
+            echo $VERSION
+            exit
+            ;;
+        --search-prompt)
+            FONTPREVIEW_SEARCH_PROMPT=$2
+            ;;
+        --font-size)
+            FONTPREVIEW_FONT_SIZE=$2
+            ;;
+        --bg-color)
+            FONTPREVIEW_BG_COLOR=$2
+            ;;
+        --fg-color)
+            FONTPREVIEW_FG_COLOR=$2
+            ;;
+        --preview-text)
+            FONTPREVIEW_PREVIEW_TEXT=$2
+            ;;
+        --)
+            shift
+            break
+            ;;
+    esac
+    shift
+done
+main