aboutsummaryrefslogtreecommitdiff
path: root/wallpaper/wpPointer.sh
diff options
context:
space:
mode:
authorInigoGutierrez <inigogf.95@gmail.com>2022-09-20 13:54:01 +0200
committerInigoGutierrez <inigogf.95@gmail.com>2022-09-20 13:54:01 +0200
commit48cec0cfbf1a9b27d4e8d11f7cf2cf66e48362d3 (patch)
tree00b3ca57b5b5bd70228cd769375aa150a4719ebf /wallpaper/wpPointer.sh
parent2cd81ede4a0a6b540cf370c4ef222277b8b867a6 (diff)
downloadscripts-48cec0cfbf1a9b27d4e8d11f7cf2cf66e48362d3.tar.gz
scripts-48cec0cfbf1a9b27d4e8d11f7cf2cf66e48362d3.zip
Implemented wallpaper history navigation and organized wallpaper scripts.
Diffstat (limited to 'wallpaper/wpPointer.sh')
-rwxr-xr-xwallpaper/wpPointer.sh184
1 files changed, 184 insertions, 0 deletions
diff --git a/wallpaper/wpPointer.sh b/wallpaper/wpPointer.sh
new file mode 100755
index 0000000..a816f18
--- /dev/null
+++ b/wallpaper/wpPointer.sh
@@ -0,0 +1,184 @@
+#!/bin/sh
+
+# wpPointer.sh: Set next or previous wallpaper in wallpaper history or set and add one to it
+#
+# Usage: wpPointer.sh [-n|p] [-o] FILE...
+#
+# -n sets next wallpaper if it exists
+# -p sets previous wallpaper if it exists
+# -o opens recorded wallpapers in sxiv
+
+usageMessage="Usage: ${0} [-n|p] [-o] FILE..."
+
+wpDatFile="$CONFIG_WPLIST"
+wpPointerFile="$CONFIG_WPPOINTER"
+recordedMax="30"
+
+nextOrPrev=""
+next="n"
+prev="p"
+
+open=""
+
+error() {
+ printf '%s error: %s\n' "$0" "$*" >&2
+ notify-send "${0} error" "$*"
+}
+
+errorAndUsage() {
+ printf '%s error: %s\n%s\n' "$0" "$*" "$usageMessage" >&2
+ notify-send "${0} error" "${*}\n\n${usageMessage}"
+}
+
+getRecordedAmount() {
+ wc -l "$wpDatFile" | cut -d' ' -f1
+}
+
+setWP() {
+ wp="$1"
+ feh --bg-max "$wp"
+ ln -sf "$wp" "${HOME}/wp"
+}
+
+setWpByPointer() {
+ pointer="$1"
+ wp="$(sed -n "${pointer}p" "$wpDatFile")"
+ echo "$pointer" >"$wpPointerFile"
+ if ! setWP "$wp"; then
+ error "Unable to set ${wp} as wallpaper."
+ fi
+}
+
+setNextOrPrev() {
+
+ nextOrPrev="$1"
+
+ if ! [ -f "$wpDatFile" ]; then
+ error "${wpDatFile} does not exist or is not a regular file. Maybe no wallpapers were registered yet."
+ exit 2
+ fi
+
+ if ! [ -s "$wpDatFile" ]; then
+ error "${wpDatFile} is empty."
+ exit 2
+ fi
+
+ datFileLength="$(getRecordedAmount)"
+
+ if [ -f "$wpPointerFile" ]; then
+ pointer="$(cat "$wpPointerFile")"
+ else
+ pointer="$datFileLength"
+ fi
+
+ if [ "$nextOrPrev" = "$next" ]; then
+
+ if [ "$pointer" -eq "$datFileLength" ]; then
+ error 'Last recorded wallpaper already selected.'
+ exit 3
+ fi
+
+ pointer=$((pointer+1))
+ setWpByPointer "$pointer"
+
+ fi
+
+ if [ "$nextOrPrev" = "$prev" ]; then
+
+ if [ "$pointer" -eq "1" ]; then
+ error 'First recorded wallpaper already selected.'
+ exit 3
+ fi
+
+ pointer=$((pointer-1))
+ setWpByPointer "$pointer"
+
+ fi
+
+}
+
+addWp() {
+
+ wp="$1"
+
+ if ! [ -f "$1" ]; then
+ error "File ${1} does not exist."
+ exit 4
+ fi
+
+ if ! setWP "$wp"; then
+ error "Unable to set ${wp} as wallpaper."
+ exit 4
+ fi
+
+ wpPath="$(readlink -f "$wp")"
+ # Double \ for explicit \ to reach variable!
+ wpPathSanitized="$(echo "$wpPath" | sed 's|/|\\/|g')"
+
+ # Delete existing references to the wallpaper to avoid repetition in history
+ sed -i "/^${wpPathSanitized}$/d" "$wpDatFile"
+
+ echo "$wpPath" >>"$wpDatFile"
+ [ "$(getRecordedAmount)" -gt "$recordedMax" ] && sed -i "1d" "$wpDatFile"
+ getRecordedAmount >"$wpPointerFile"
+
+}
+
+# Check pointer file makes sense
+
+if [ -f "$wpPointerFile" ]; then
+ pointer="$(cat "$wpPointerFile")"
+ pointerAsNumber="$(echo "$pointer" | sed 1q | grep -E '[0-9]+')"
+ if [ "$pointer" != "$pointerAsNumber" ]; then
+ rm "$wpPointerFile"
+ else
+ if [ "$pointer" -lt 1 ]; then
+ echo "1" >"$wpPointerFile"
+ fi
+ recordedAmount=$(getRecordedAmount)
+ if [ "$pointer" -gt "$recordedAmount" ]; then
+ echo "$recordedAmount" >"$wpPointerFile"
+ fi
+ fi
+fi
+
+# Process options
+
+while getopts ':npo' opt; do
+ case $opt in
+
+ 'n' )
+ if [ -n "$nextOrPrev" ]; then
+ errorAndUsage 'Tried to use -n and -p at the same time.'
+ exit 1
+ fi
+ setNextOrPrev "$next"
+ ;;
+
+ 'p' )
+ if [ -n "$nextOrPrev" ]; then
+ errorAndUsage 'Tried to use -n and -p at the same time.'
+ exit 1
+ fi
+ setNextOrPrev "$prev"
+ ;;
+
+ 'o' )
+ open="y"
+ ;;
+
+ '?' )
+ printf '%s\n' "$usageMessage"
+ exit 1
+
+ esac
+done
+shift $((OPTIND - 1))
+
+# Set and add arguments to wallpaper history
+
+for wp in "$@"; do
+ addWp "$wp"
+done
+
+[ -n "$open" ] && sxiv -t $(cat "$wpDatFile")