|
#!/usr/bin/env fish |
|
# print hours remaining to sunrise or sunset |
|
# v0.4 gist.github.com/godDLL/c247f130d2d921d023f78765d6bfd477 |
|
# |
|
set -q LOC ; or set -l LOC '' #'Stavanger,Norway' # if empty uses IP geolocation |
|
set -l FMT '%D,%s' # dawn,sunset |
|
#set -l FMT '%S,%d' # sunrize,dusk |
|
|
|
|
|
switch "$argv[1]" |
|
case 'x' |
|
if test -z "$fish_suntime" |
|
set -l -- CMD (status filename) |
|
$CMD update |
|
end |
|
printf '%s %s%s%s %s\n' $fish_suntime |
|
set -e argv[1] |
|
if test -n "$argv" |
|
and suntime $argv |
|
end |
|
|
|
case '-h' '--help' |
|
echo -e -- Usage:\ (status basename) "[COMMANDS] [OPTIONS] |
|
Time until sunrise or sunset, rounded to ½ hour. |
|
|
|
-c \tWith clock |
|
-n \tNo color, no emoji, plain text |
|
-t \tText with no rounding |
|
-a \tDefault |
|
|
|
Commands are silent when used as the only arument: |
|
e \tReturn error if sun is down |
|
u update\tUpdate using wttr.in and set -U in Fish |
|
x \tPrint dawn and sunset time for export |
|
ux \tUpdate and show the updated time |
|
|
|
Examples: |
|
suntime x -a \t# Show cached day and night time, and time until sunup/down |
|
suntime e && open_night_terminal \t# Night shift :) |
|
suntime u -a \t# Update and print time until sunup/down live |
|
" >&2 |
|
|
|
case 'u' 'update' 'ux' |
|
set -l -- SUN (curl wttr.in/$LOC\?format="$FMT" 2>/dev/null | string split -- ,) |
|
if test -n "$SUN" |
|
if string match -q --invert -- '*:*:*' "$SUN" |
|
echo -- $SUN >&2 |
|
exit 2 |
|
end |
|
set -l up (string split -- : $SUN[1]) |
|
set -l down (string split -- : $SUN[2]) |
|
set -U fish_suntime $SUN \n (math -s0 "3600* $up[1] + 60* $up[2] + $up[3]") (math -s0 "3600* $down[1] + 60* $down[2] + $down[3]") |
|
else |
|
echo -- 'Error updating dawn and sunset times' >&2 |
|
exit 1 |
|
end |
|
test 'ux' = "$argv[1]" |
|
and suntime x |
|
set -e argv[1] |
|
if test -n "$argv" |
|
and suntime $argv |
|
end |
|
|
|
case '*' #'-a' |
|
if test -z "$fish_suntime" |
|
set -l -- CMD (status filename) |
|
$CMD update |
|
end |
|
|
|
set -l D (date +%H\n%M\n%S) |
|
set -l dS (math -s0 "3600* $D[1] + 60* $D[2] + $D[3]") |
|
set -l dH |
|
set -l dM |
|
set -l TO 'sunrise' |
|
set -l EMO '🍄 ' #◯⭘ 🌙☽ |
|
|
|
if test "$dS" -lt "$fish_suntime[5]" -a "$dS" -ge "$fish_suntime[4]" |
|
set TO 'sunset' |
|
test 'e' = "$argv" |
|
and exit 0 |
|
set EMO '🌼 ' #☀⚽︎ ❂ ☀️ |
|
set dM "$fish_suntime[5] - $dS" |
|
|
|
else if test "$dS" -ge "$fish_suntime[5]" |
|
test 'e' = "$argv" |
|
and exit 1 |
|
set dM "$fish_suntime[4] + 86400 - $dS" |
|
else |
|
test 'e' = "$argv" |
|
and exit 1 |
|
set dM "$fish_suntime[4] - $dS" |
|
end |
|
set dH (math -s0 "( $dM ) /3600") |
|
set dM (math -s0 "( $dM - $dH *3600) /60") |
|
|
|
set -l tM 'minutes' |
|
test 1 = "$dM" |
|
and set tM 'minute' |
|
|
|
if contains -- '-t' $argv |
|
|
|
if contains -- '-c' $argv |
|
printf '%s:%s.%s, ' $D |
|
end |
|
if test 0 = "$dH" |
|
printf '%s %s to %s\n' $dM $tM $TO |
|
else |
|
set -l tH 'hours' |
|
test 1 = "$dH" |
|
and set tH 'hour' |
|
|
|
printf '%s %s %s %s to %s\n' $dH $tH $dM $tM $TO |
|
end |
|
|
|
exit |
|
end |
|
|
|
if test 0 = "$dH" |
|
if test 45 -le "$dM" |
|
set dH 'about an hour' |
|
else if test 30 -le "$dM" |
|
set dH 'about ½ hour' |
|
else |
|
if test 1 -ge "$dM" |
|
set dH 'a minute' |
|
else |
|
set dH "$dM $tM" |
|
end |
|
end |
|
if contains -- '-n' $argv |
|
if contains -- '-c' $argv |
|
printf '%s:%s.%s, ' $D |
|
end |
|
set dH (string replace -- '½ ' 'half-' $dH) |
|
printf '%s to %s\n' $dH $TO |
|
else |
|
if contains -- '-c' $argv |
|
printf '\e[1m%s:%s\e[2m.%s\e[22m' $D |
|
end |
|
printf ' \e[2m%s\e[22m %s\e[2m to %s\e[22m\n' $EMO $dH $TO |
|
end |
|
|
|
else # >= 1hr |
|
set -l tH 'hours' |
|
if test 1 = "$dH" |
|
if test 45 -le "$dM" |
|
set dH 2 |
|
else if test 15 -le "$dM" |
|
set dH "$dH½" |
|
else |
|
set tH 'hour' |
|
end |
|
else if test 45 -le "$dM" |
|
set dH (math -s0 "1 + $dH") |
|
else if test 15 -le "$dM" |
|
set dH "$dH½" |
|
end |
|
|
|
if contains -- '-n' $argv |
|
if contains -- '-c' $argv |
|
printf '%s:%s.%s, ' $D |
|
end |
|
set dH (string replace -- '½' ' and a half' $dH) |
|
printf '%s %s to %s\n' $dH $tH $TO |
|
else |
|
if contains -- '-c' $argv |
|
printf '\e[1m%s:%s\e[2m.%s\e[22m' $D |
|
end |
|
printf ' \e[2m%s\e[22m %s %s\e[2m to %s\e[22m\n' $EMO $dH $tH $TO |
|
end |
|
end |
|
|
|
end #switch |
|
|
v0.3 with even less bugs and more options