From c47cd9af1b7917f93d53620e77f75487522a73e3 Mon Sep 17 00:00:00 2001 From: Mateusz Piotrowski <0mp@FreeBSD.org> Date: Tue, 11 Feb 2020 10:38:24 +0100 Subject: [PATCH 1/8] Adjust makefile to ease packaging - PREFIX is a much more standard variable than DEST. - Add BINDIR to actually let people choose where to install fontpreview precisely if they need. - Support DESTDIR, which is useful for staged package building. While here, use tabs to indent values of variables. Spaces are sometimes significant in makefiles (they are parts of the values), which might lead to confusing bugs. --- Makefile | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index c33f354..3a4171f 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,14 @@ -DEST ?= /usr/bin +PREFIX?= /usr +BINDIR= ${PREFIX}/bin all: @echo Run \'make install\' to install fontpreview on your device install: - @cp fontpreview $(DEST)/fontpreview - @chmod 755 $(DEST)/fontpreview + @cp fontpreview $(DESTDIR)$(BINDIR)/fontpreview + @chmod 755 $(DESTDIR)$(BINDIR)/fontpreview @echo fontpreview has been installed on your device uninstall: - @rm -rf $(DEST)/fontpreview + @rm -rf $(DESTDIR)$(BINDIR)/fontpreview @echo fontpreview has been removed from your device From eb2372c03adaea4ddcb405ceaca2113e48f5490f Mon Sep 17 00:00:00 2001 From: Mateusz Piotrowski <0mp@FreeBSD.org> Date: Tue, 11 Feb 2020 10:47:35 +0100 Subject: [PATCH 2/8] Strip whitespace --- fontpreview | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/fontpreview b/fontpreview index fed00a9..d29e636 100755 --- a/fontpreview +++ b/fontpreview @@ -124,19 +124,19 @@ main(){ # sxiv when the user exits the script echo $! >"$PIDFILE" - # Check for crashes of sxvi + # Check for crashes of sxvi elif [ -f $PIDFILE ] ; then PID=$(cat $PIDFILE) if [ ! -e /proc/$PID ] ; then echo "Restart sxvi - You maybe using a obsolete version. " >&2 # 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 "$TERMWIN_IDFILE")" - + # Save the process ID so that we can kill # sxiv when the user exits the script echo $! >"$PIDFILE" @@ -158,7 +158,7 @@ options=$(getopt -o h --long position:,size:,version,search-prompt:,font-size:,b eval set -- "$options" while true; do - case "$1" in + case "$1" in --size) shift; FONTPREVIEW_SIZE=$2 From 34ecfe4bdc549479749753bc0b66b2b9d2642dbc Mon Sep 17 00:00:00 2001 From: Mateusz Piotrowski <0mp@FreeBSD.org> Date: Tue, 11 Feb 2020 11:00:03 +0100 Subject: [PATCH 3/8] Remove unnecessary semicolons --- fontpreview | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fontpreview b/fontpreview index d29e636..862675a 100755 --- a/fontpreview +++ b/fontpreview @@ -160,11 +160,11 @@ eval set -- "$options" while true; do case "$1" in --size) - shift; + shift FONTPREVIEW_SIZE=$2 ;; --position) - shift; + shift FONTPREVIEW_POSITION=$2 ;; -h|--help) From 347c8f3cc95c0ce0f62d813b12db40ab1a761be3 Mon Sep 17 00:00:00 2001 From: Mateusz Piotrowski <0mp@FreeBSD.org> Date: Tue, 11 Feb 2020 11:04:32 +0100 Subject: [PATCH 4/8] Create temporary files in a more portable way Also: - Do not put random strings into files inside the fontpreview directory. They are already unique there. - Exit with an error code if the program fails to create the temporary files. --- fontpreview | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/fontpreview b/fontpreview index 862675a..3d2ce80 100755 --- a/fontpreview +++ b/fontpreview @@ -4,12 +4,6 @@ # # Dependencies: sxiv, imagemagick, xdotool, fzf -# Use mktemp to create temporary files that won't -# collide with any other application's tmp files. -FONTPREVIEW_DIR="$(mktemp -d --tmpdir fontpreview_dir_XXXXXXXX)" -PIDFILE="$(mktemp --tmpdir="$FONTPREVIEW_DIR" fontpreview_XXXXXXXX.pid)" -FONT_PREVIEW="$(mktemp --tmpdir="$FONTPREVIEW_DIR" fontpreview_XXXXXXXX.png)" -TERMWIN_IDFILE="$(mktemp --tmpdir="$FONTPREVIEW_DIR" fontpreview_XXXXXXXX.termpid)" VERSION=1.0.3 # Default values @@ -153,6 +147,16 @@ trap "" SIGTSTP trap pre_exit EXIT +# Use mktemp to create a temporary directory that won't +# collide with temporary files of other application. +FONTPREVIEW_DIR="$(mktemp -d "${TMPDIR:-/tmp}/fontpreview_dir.XXXXXXXX")" || exit +PIDFILE="$FONTPREVIEW_DIR/fontpreview.pid" +touch "$PIDFILE" || exit +FONT_PREVIEW="$FONTPREVIEW_DIR/fontpreview.png" +touch "$FONT_PREVIEW" || exit +TERMWIN_IDFILE="$FONTPREVIEW_DIR/fontpreview.termpid" +touch "$TERMWIN_IDFILE" || 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" From 3a9c4538f0e4317908af09d866b92923709ff58f Mon Sep 17 00:00:00 2001 From: Mateusz Piotrowski <0mp@FreeBSD.org> Date: Tue, 11 Feb 2020 11:10:01 +0100 Subject: [PATCH 5/8] Fix sxiv invocation The file name has to be the last argument. It was causing the following errors: sxiv: -N: No such file or directory sxiv: fontpreview: No such file or directory sxiv: -b: No such file or directory --- fontpreview | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fontpreview b/fontpreview index 3d2ce80..f9595f0 100755 --- a/fontpreview +++ b/fontpreview @@ -124,7 +124,7 @@ main(){ if [ ! -e /proc/$PID ] ; then echo "Restart sxvi - You maybe using a obsolete version. " >&2 # Display the font preview using sxiv - sxiv -g "$SIZE$POSITION" "$FONT_PREVIEW" -N "fontpreview" -b & + sxiv -g "$SIZE$POSITION" -N "fontpreview" -b "$FONT_PREVIEW" & # Change focus from sxiv, back to the terminal window # so that user can continue to search for fonts without From cd051648ec44e3d98a3a542bf56a46910e111895 Mon Sep 17 00:00:00 2001 From: Mateusz Piotrowski <0mp@FreeBSD.org> Date: Tue, 11 Feb 2020 11:12:07 +0100 Subject: [PATCH 6/8] Use double square brackets instead of single ones As fontpreview is now using Bash instead of POSIX sh(1), there is no need to use single square brackets. --- fontpreview | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/fontpreview b/fontpreview index f9595f0..70eda23 100755 --- a/fontpreview +++ b/fontpreview @@ -102,7 +102,7 @@ main(){ generate_preview "$font" - if [ $FIRST_RUN == true ]; then + if [[ $FIRST_RUN == true ]]; then FIRST_RUN=false # Display the font preview using sxiv @@ -119,9 +119,9 @@ main(){ echo $! >"$PIDFILE" # Check for crashes of sxvi - elif [ -f $PIDFILE ] ; then + elif [[ -f $PIDFILE ]] ; then PID=$(cat $PIDFILE) - if [ ! -e /proc/$PID ] ; then + if [[ ! -e /proc/$PID ]] ; then echo "Restart sxvi - You maybe using a obsolete version. " >&2 # Display the font preview using sxiv sxiv -g "$SIZE$POSITION" -N "fontpreview" -b "$FONT_PREVIEW" & From 6d42ae4b729951fcfd42384070cec39a32d57fc1 Mon Sep 17 00:00:00 2001 From: Mateusz Piotrowski <0mp@FreeBSD.org> Date: Tue, 11 Feb 2020 11:13:39 +0100 Subject: [PATCH 7/8] Use pgrep instead of procfs to check if program is running Procfs might not available on every platform. pgrep is much more portable. --- fontpreview | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/fontpreview b/fontpreview index 70eda23..22aaf53 100755 --- a/fontpreview +++ b/fontpreview @@ -120,8 +120,7 @@ main(){ # Check for crashes of sxvi elif [[ -f $PIDFILE ]] ; then - PID=$(cat $PIDFILE) - if [[ ! -e /proc/$PID ]] ; then + if ! pgrep -F "$PIDFILE"; then echo "Restart sxvi - You maybe using a obsolete version. " >&2 # Display the font preview using sxiv sxiv -g "$SIZE$POSITION" -N "fontpreview" -b "$FONT_PREVIEW" & From 178a66fd1896e6f12a727207c723fc2ee9dfe4fe Mon Sep 17 00:00:00 2001 From: Mateusz Piotrowski <0mp@FreeBSD.org> Date: Tue, 11 Feb 2020 11:37:31 +0100 Subject: [PATCH 8/8] Silence pgrep --- fontpreview | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fontpreview b/fontpreview index 22aaf53..7c6522e 100755 --- a/fontpreview +++ b/fontpreview @@ -120,7 +120,7 @@ main(){ # Check for crashes of sxvi elif [[ -f $PIDFILE ]] ; then - if ! pgrep -F "$PIDFILE"; then + if ! pgrep -F "$PIDFILE" >/dev/null 2>&1; then echo "Restart sxvi - You maybe using a obsolete version. " >&2 # Display the font preview using sxiv sxiv -g "$SIZE$POSITION" -N "fontpreview" -b "$FONT_PREVIEW" &