Skip to content

Instantly share code, notes, and snippets.

@godDLL
Last active December 10, 2021 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save godDLL/33c8b47095629923d490234776c1dbbd to your computer and use it in GitHub Desktop.
Save godDLL/33c8b47095629923d490234776c1dbbd to your computer and use it in GitHub Desktop.
Healthy sleep normalization tracker for the console.

Stat Sleep Fish

Sleep tracker with a timer, in the terminal. Demo
Screen Shot 2021-12-11 at 00 04 09

Installation

Download, chmod +x stat-sleep.fish and put it in your PATH

fish is required to run this script, but doesn't have to be your SHELL to do it.

Usage

Can suggest a time to go to bed or a time to set alarm, given the tracked
sleeping hours average. See --bed HH:MM and --wake HH:MM

Healthy sleep average is keyed to 7.5 hours, time for bed defaults to 22:00
and time for alarm defaults to 05:45

Usage:  stat-sleep.fish [HOURS_SLEPT]
Indicate running average of hours slept, or update it.

     stat-sleep.fish 6.5    # add a day's sleep
     stat-sleep.fish +2     # correct this day's number
     stat-sleep.fish -t     # start sleep timer
                            # Ctrl-C to abort
     set -e fish_sleep_hrs  # to reset all
     stat-sleep.fish --wake 05:45
     # suggest a time to turn in for 7.5 average
     # plus the 15 minutes to fall aseleep
     stat-sleep.fish --bed 22:00 --verbose
     # time for alarm, same as above
#!/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
@godDLL
Copy link
Author

godDLL commented Dec 5, 2021

License MIT, free to do whatever. Yours to fuck up.
asciinema.org/a/453741
Screen Shot 2021-12-05 at 04 17 51

@godDLL
Copy link
Author

godDLL commented Dec 6, 2021

v0.2 Add ability to correct today's NUMBER with +MORE and -LESS

@godDLL
Copy link
Author

godDLL commented Dec 10, 2021

v0.3 Add time suggestion for --bed and --wake given tracked sleep average.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment