Allow setting the center of the map

This commit is contained in:
Manuel Thalmann 2024-04-26 08:28:25 +02:00
parent 453c004e89
commit 33e2a7bf53
2 changed files with 92 additions and 11 deletions

View file

@ -1,8 +1,14 @@
{ pkgs, lib, config, ... }: { pkgs, lib, config, ... }:
{ {
options = { options = {
scripts.output = lib.mkOption { scripts = {
type = lib.types.package; output = lib.mkOption {
type = lib.types.package;
};
geocode = lib.mkOption {
type = lib.types.package;
};
}; };
map = { map = {
@ -10,6 +16,11 @@
type = lib.types.nullOr lib.types.int; type = lib.types.nullOr lib.types.int;
default = 2; default = 2;
}; };
center = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = "Switzerland";
};
}; };
requestParams = lib.mkOption { requestParams = lib.mkOption {
@ -18,17 +29,30 @@
}; };
config = { config = {
scripts.output = pkgs.writeShellApplication { scripts = {
name = "map"; output = pkgs.writeShellApplication {
name = "map";
runtimeInputs = with pkgs; [ runtimeInputs = with pkgs; [
curl curl
imagemagick imagemagick
]; ];
text = '' text = ''
convert <(${./map} ${lib.concatStringsSep " " config.requestParams}) sixel:- convert <(${./map} ${lib.concatStringsSep " " config.requestParams}) sixel:-
''; '';
};
geocode = pkgs.writeShellApplication {
name = "geocode";
runtimeInputs = with pkgs; [
curl
jq
];
text = ''exec ${./geocode} "$@"'';
};
}; };
requestParams = [ requestParams = [
@ -36,6 +60,10 @@
"scale=2" "scale=2"
(lib.mkIf (config.map.zoom != null) (lib.mkIf (config.map.zoom != null)
"zoom=${toString config.map.zoom}") "zoom=${toString config.map.zoom}")
(lib.mkIf (config.map.center != null)
"center=\"${config.scripts.geocode}/bin/geocode ${
lib.escapeShellArg config.map.center
}\"")
]; ];
}; };
} }

53
geocode Executable file
View file

@ -0,0 +1,53 @@
#!/usr/bin/env bash
set -euo pipefail
cachedir=~/.cache/google-api/geocode
mkdir -p "$cachedir"
hash=$(echo "$1" | sha256sum - | cut -d' ' -f1)
cachefile="$cachedir/$hash"
if [[ ! -f "$cachefile" ]]; then
keyFile=${XDG_DATA_HOME:-~/.local/share}/google-api/key
if [[ ! -f "$keyFile" ]]; then
mkdir -p "$(basename "$keyFile")"
echo "No Google API key found in $keyFile" >&2
echo "For getting one, see https://developers.google.com/maps/documentation/geocoding/overview#before-you-begin" >&2
exit 1
fi
key=$(cat "$keyFile")
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' exit
output=$tmp/output
curlArgs=(
https://maps.googleapis.com/maps/api/geocode/json
--silent --show-error --get --output "$output" --write-out '%{http_code}'
--data-urlencode address="$1"
)
#echo curl ''${curlArgs[@]@Q} >&2
curlArgs+=(--data-urlencode key="$key")
if status=$(curl "${curlArgs[@]}"); then
if [[ "$status" == 200 ]]; then
jq -r '.results[0].geometry.location as $loc | "\($loc | .lat),\($loc | .lng)"' "$output" > "$cachefile"
else
echo "API returned non-200 HTTP status code $status, output is" >&2
cat "$output" >&2
exit 1
fi
else
code=$?
echo "curl exited with code $code" >&2
exit 1
fi
fi
cat "$cachefile"