Skip to content

Commit

Permalink
hammerspoon: add location module to replace CoreLocationCLI
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedelgabri committed Feb 8, 2025
1 parent f75dfb0 commit 2c8dedb
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 18 deletions.
31 changes: 21 additions & 10 deletions config/.hammerspoon/location.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,10 @@
local M = {}
local log = require 'log'

function M.write()
local loc = hs.location.get()
function M.write(data)
local path = hs.fs.temporaryDirectory() .. '.location.json'

local ok, _ = pcall(
hs.json.write,
{ lat = loc.latitude, lon = loc.longitude },
path,
true,
true
)
local ok, _ = pcall(hs.json.write, data, path, true, true)

if ok then
log.i('Location written to ' .. path)
Expand All @@ -24,7 +17,25 @@ end
function M.run()
if hs.location.servicesEnabled() then
hs.location.start()
M.write()
local loc = hs.location.get()

if not loc then
log.e 'No location found.'
return nil
end

hs.location.geocoder.lookupLocation(loc, function(state, result)
if state then
-- Needed otherwise JSON serialization fails
-- ERROR: LuaSkin: Object cannot be serialised as JSON
-- ERROR: LuaSkin: Failed to write object to JSON file
result[1].location['__luaSkinType'] = nil

M.write(result[1])
else
M.write { location = loc, locality = '' }
end
end)
hs.location.stop()
else
log.e '\n-- Location services disabled!\n'
Expand Down
12 changes: 7 additions & 5 deletions config/tmux/scripts/get-prayer
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
#!/usr/bin/env bash

# Intentionally not setting -e because we want the script to continue if CoreLocationCLI fails.
# Intentionally not setting -e because we want the script to continue if getting the location data fails
set -u -o pipefail

if [[ -n "${DEBUG-}" ]]; then
set -x
fi

FLAGS="$(CoreLocationCLI --format "-latitude %latitude -longitude %longitude")"
LOCATION_DATA="$TMPDIR/.location.json"

# Can't use -P (regex pattern) because macOS has the BSD grep while -P works on GNU grep.
if grep -q "\-latitude .* -longitude .*" <<<"$FLAGS"; then
printf "%s" "󰩷 $(next-prayer mawaqit $FLAGS)"
# Validate the JSON syntax
if jq -e . "$LOCATION_DATA" >/dev/null 2>&1; then
FLAGS=$(jq '"-latitude \(.location.latitude) -longitude \(.location.longitude)"' "$LOCATION_DATA")

printf "%s" "󰩷 $(next-prayer mawaqit "$FLAGS")"
else
printf "%s" "$(next-prayer aladhan)"
fi
17 changes: 15 additions & 2 deletions config/zsh.d/zsh/bin/weather
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env bash

# Modified version of the generated one from `curl wttr.in/:bash.function`
#
# Simple function to get weather data
Expand All @@ -9,8 +8,22 @@
# $ weather <city>
# Check curl wttr.in/:help for more info

# Intentionally not setting -e because we want the script to continue if getting the location data fails
set -u -o pipefail

if [[ -n "${DEBUG-}" ]]; then
set -x
fi

LOCATION_DATA="$TMPDIR/.location.json"
FALLBACK=""

if jq -e . "$LOCATION_DATA" >/dev/null 2>&1; then
FALLBACK=$(jq -r '.locality' "$LOCATION_DATA")
fi

weather() {
local location="${1:-$(CoreLocationCLI --json | jq -r '.locality' 2>/dev/null)}"
local location="${1:-$FALLBACK}"
curl -fGsS --compressed "wttr.in/${location}"?"m&lang=en&${2:-0qm}"
}

Expand Down
1 change: 0 additions & 1 deletion nix/modules/shared/gui.nix
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ in {
"raycast"
"anki"
"appcleaner"
"corelocationcli"
"firefox"
"imageoptim"
"kap"
Expand Down

0 comments on commit 2c8dedb

Please sign in to comment.