|
#!/usr/bin/env fish |
|
# time and track last 7 days of sleep |
|
# v0.3 gist.github.com/33c8b47095629923d490234776c1dbbd |
|
# |
|
|
|
set -l WU |
|
if contains -- "$argv[1]" '-b' '--bed' |
|
if test -z "$argv[2]" ; or contains -- "$argv[2]" '-v' '--verbose' |
|
set AT 22 00 |
|
else |
|
set AT (string split -- ':' $argv[2]) |
|
set -e argv[2] |
|
end |
|
set -e argv[1] |
|
set AT (math -s0 "(60- $AT[2])+ 60* (23- $AT[1]) -15") |
|
end |
|
if contains -- "$argv[1]" '-w' '--wake' |
|
if test -z "$argv[2]" ; or contains -- "$argv[2]" '-v' '--verbose' |
|
set WU 05 45 |
|
else |
|
set WU (string split -- ':' $argv[2]) |
|
set -e argv[2] |
|
end |
|
set -e argv[1] |
|
set WU (math -s0 "$WU[2]+ 60* $WU[1] -15") |
|
end |
|
if test -z "$argv" ; or contains -- "$argv" '-v' '--verbose' |
|
if test -z "$fish_sleep_hrs" |
|
echo -- "Found nothing. Please run with --time, or add something." >&2 |
|
exit 1 |
|
end |
|
set -l W \e\[22m\e\[1m \e\[22m\e\[2m \e\[22m |
|
set -l X 3 2 1 |
|
set -l C (math -s0 "min(3, max(1, ($fish_sleep_hrs[1] -1) /3))") |
|
if test -n "$AT" |
|
set AT (math -s3 "7.5+ (7.5 -$fish_sleep_hrs[1])/2 -$AT /60") |
|
if test 0 -ge "$AT" |
|
set -l H (math "floor(24+ $AT)") |
|
test 24 -eq "$H" |
|
and set H '00' |
|
set -l M (math "60+ round($AT *60) %60") |
|
test 60 -eq "$M" |
|
and set M '00' |
|
printf '%02d:%02d ' $H $M |
|
else |
|
printf '%02d:%02d ' (math "floor($AT)") (math "round($AT *60) %60") |
|
end |
|
end |
|
if test -n "$WU" |
|
set WU (math -s3 "7.5+ (7.5 -$fish_sleep_hrs[1])/2 -$WU /60") |
|
if test 0 -ge "$WU" |
|
printf '%02d:%02d ' (math "floor(0 -$WU)") (math "0- round($WU *60) %60") |
|
else |
|
set -l M (math "60- round($WU *60) %60") |
|
test 60 -eq "$M" |
|
and set M '00' |
|
printf '%02d:%02d ' (math "floor(24 -$WU)") $M |
|
end |
|
end |
|
|
|
set -l AVG $fish_sleep_hrs[1] |
|
if not contains -- "$argv" '-v' '--verbose' |
|
set -- AVG (math -s1 "0- (7.5 -$AVG)") |
|
test 0 -lt "$AVG" |
|
and set -- AVG +$AVG |
|
set -- AVG \e\[2m7.5$W[$C]$AVG |
|
end |
|
printf '\e[7;3%sm%s %s \e[22m\e[27m' $X[$C] $W[$C] $AVG |
|
if test -z "$fish_sleep_hrs[3]" |
|
if not contains -- "$argv" '-v' '--verbose' |
|
echo -- |
|
exit |
|
end |
|
else if not contains -- "$argv" '-v' '--verbose' |
|
echo -- |
|
exit |
|
end |
|
set -l R |
|
for I in $fish_sleep_hrs[2..-1] |
|
set -a R ' '$W[(math -s0 "min(3, max(1, ($I -1) /3))")]$I\e\[22m |
|
end #for |
|
string join -- ' ' $R |
|
else |
|
if contains -- "$argv" '-h' '--help' |
|
set -l -- CMD (status basename) |
|
echo -- "Usage: $CMD [HOURS_SLEPT] |
|
Indicate running average of hours slept, or update it. |
|
|
|
$CMD 6.5 # add a day's sleep |
|
$CMD +2 # correct this day's number |
|
$CMD -t # start sleep timer |
|
# Ctrl-C to abort |
|
set -e fish_sleep_hrs # to reset all |
|
$CMD --wake 05:45 |
|
# suggest a time to turn in for 7.5 average |
|
# plus the 15 minutes to fall aseleep |
|
$CMD --bed 22:30 --verbose |
|
# time for alarm, same as above |
|
" >&2 |
|
exit |
|
end |
|
|
|
set -l T |
|
if contains -- "$argv" '-t' '--time' '--timer' |
|
set -e argv[1] |
|
set -l B (date +%s) |
|
read -P 'Started '(date +%H:%M.)' ' -c '<Enter> to wake up and log' -l X |
|
set -l A (date +%s) |
|
set -- T (math -s1 "($A - $B) /3600") |
|
end |
|
test -z "$T" |
|
and set -- T $argv |
|
|
|
set -l -- CMD (status filename) |
|
if test -z "$fish_sleep_hrs" |
|
set -U -- fish_sleep_hrs (math -s1 "min(max(0, $T), 10)") |
|
set -a fish_sleep_hrs $fish_sleep_hrs |
|
$CMD |
|
exit |
|
end |
|
|
|
if string match -qr -- '^[+-]\d+' "$T" |
|
set -- fish_sleep_hrs[1] (math -s1 "($fish_sleep_hrs[1] - $fish_sleep_hrs[2] /2) + ($fish_sleep_hrs[2] $T)/2") |
|
set -- fish_sleep_hrs[2] (math -s1 "$fish_sleep_hrs[2] $T") |
|
else |
|
set -p -- fish_sleep_hrs (math -s1 "($fish_sleep_hrs[1] + max(0, $T)) /2") |
|
set -- fish_sleep_hrs[2] (math -s1 "min(max(0, $T), 24)") |
|
set -e fish_sleep_hrs[9] |
|
end |
|
$CMD |
|
end |
|
|
v0.3 Add time suggestion for
--bedand--wakegiven tracked sleep average.