commit f2a27f25f98e9411441aa82fbe8c17a9abbc0b65
parent 290435872585a3d29ca9c8b1570ad27c62c05973
Author: dwrz <dwrz@dwrz.net>
Date: Mon, 2 Jan 2023 12:04:54 +0000
Refactor map-bg script
Diffstat:
1 file changed, 29 insertions(+), 10 deletions(-)
diff --git a/scripts/map-bg b/scripts/map-bg
@@ -1,14 +1,20 @@
#!/usr/bin/env bash
dimensions="$(xrandr --current | grep '*' | uniq | awk '{print $1}')"
-height="$(echo $dimensions | cut -d 'x' -f2)"
-width="$(echo $dimensions | cut -d 'x' -f1)"
+height="$(echo "${dimensions}" | cut -d 'x' -f2)"
+width="$(echo "${dimensions}" | cut -d 'x' -f1)"
-coordinates="$1"
-zoom="$2"
+tiles="$1"
+coordinates="$2"
+zoom="$3"
-# If no coordinates were provided, take them from the password store.
+# Default to satellite imagery.
if [[ -z "$1" ]]; then
+ tiles="arcgis-worldimagery"
+fi
+
+# If no coordinates were provided, take them from the password store.
+if [[ -z "$2" ]]; then
dir="${PASSWORD_STORE_DIR}/coordinates"
locations="$(find "${dir}" -mindepth 1 -exec basename -- {} .gpg \;)"
location="$(echo "${locations}" | shuf -n 1)"
@@ -16,17 +22,30 @@ if [[ -z "$1" ]]; then
fi
# If no zoom was provided, use a number between 8 and 18, inclusive.
-if [[ -z "$2" ]]; then
+if [[ -z "$3" ]]; then
zoom="$(shuf -i 8-18 -n 1)"
fi
# Create the map and store it in the maps directory.
# If we fail, use an existing map.
+map_dir="${HOME}/.cache/maps/${width}x${height}"
+mkdir -p "${map_dir}"
+
+# Use a cached map, if one exists.
+if [[ -f "${map_dir}/${tiles}-${coordinates}-${zoom}.png" ]]; then
+ feh --bg-fill "${map_dir}/${tiles}-${coordinates}-${zoom}.png"
+ exit 0
+fi
+
+# Otherwise, attempt to create a static map.
+# If we fail, use a random map from the cache.
create-static-map \
--width "${width}" --height "${height}" \
- -o "${HOME}/.cache/map-${coordinates}-${zoom}.png" \
- -c "${coordinates}" -z "${zoom}" && \
- feh --bg-fill "${HOME}/.cache/map-${coordinates}-${zoom}.png"
+ --center "${coordinates}" --zoom "${zoom}" \
+ --type "${tiles}" \
+ --output "${map_dir}/${tiles}-${coordinates}-${zoom}.png" \
+ && \
+ feh --bg-fill "${map_dir}/${tiles}-${coordinates}-${zoom}.png"
if [[ "$?" -ne 0 ]]; then
- feh --bg-fill --randomize "/home/dwrz/archive/images/maps/"*
+ feh --bg-fill --randomize "${map_dir}/"*
fi