|
#!/usr/bin/env fish |
|
# v0.4 gist.github.com/godDLL/b4a43505b0dac4f12dcd73046efae5ee |
|
# |
|
|
|
# works with the files !w writes |
|
if test '-' = "$argv" |
|
set -l ansi 's/'\e'\[[0-9;]*[m]//g' |
|
while read -l N |
|
echo -e -- "$N" |sed $ansi |
|
end |
|
exit |
|
end |
|
|
|
|
|
if set -q TITLE |
|
set -- TITLE (eval $TITLE) |
|
else |
|
set -- TITLE (date +%d\ %m-%Y) |
|
end |
|
set TITLE_len (string length -- " $TITLE") |
|
|
|
set -l PLOG ## up go to end back up left white on black rst |
|
set -a -- PLOG (echo -e -- '\e[A\e['$COLUMNS'G\e['$TITLE_len'D\e[37;40m '"$TITLE" '\e[m') |
|
echo -- $PLOG[-1] |
|
|
|
set -l -- ST |
|
if test -n "$STATUS" |
|
if test -n "$argv" |
|
set -- ST "$argv" |
|
else |
|
set -- ST (eval $STATUS) |
|
if test -z "$ST" |
|
set -- ST (date +%H:%M.) |
|
end |
|
end |
|
else |
|
if test -n "$argv" |
|
set -- ST "$argv" |
|
if test -z "$ST" |
|
set -- ST (date +%H:%M.) |
|
end |
|
else |
|
set -- ST (date +%H:%M.) |
|
end |
|
set -- STATUS 'date +%H:%M.' |
|
end |
|
set -l cl '\e[K' |
|
set -l upcl '\e[A'$cl |
|
set -a -- PLOG (echo -e -- $cl'\e[2m' (date +%A) "$ST"'\e[m') |
|
echo -- $PLOG[-1] |
|
#clear |
|
set -e ST |
|
|
|
|
|
function pick --description 'Show a screenful of options, pick one' |
|
|
|
if test -n "$argv[1]" |
|
and contains -- "$argv[1]" -h --help |
|
|
|
echo 'Usage: menu [OPTIONS] ITEM1 ITEM2 ... |
|
Returns selected item number. |
|
|
|
-i NUM pre-select item |
|
-d STR menu item delimiter, default to \n |
|
-x return code is item index, no echo |
|
' >&2 |
|
return |
|
else |
|
set -l clm \e\[m |
|
set -l sel \e\[7m |
|
argparse 'i/item=' 'd/delim=' 'x/echo' -- $argv |
|
|
|
test -n "$_flag_d" |
|
and set -l NL $_flag_d |
|
or set -l NL \n |
|
|
|
test -n "$_flag_i" |
|
and set -l ID $_flag_i |
|
or set -l ID 1 |
|
|
|
set -l -- ITEM ' '{$argv}' ' |
|
set -l -- LEN (count -- $ITEM) |
|
set -- ITEM[$ID] "$sel$ITEM[$ID]$clm" |
|
|
|
echo -en \e\[A >&2 |
|
string join -- $NL ' '{$ITEM}' ' >&2 |
|
echo -en \e\[H >&2 |
|
|
|
while read -s -n1 -P '' -l K |
|
switch "$K" |
|
case ' ' '' # select |
|
test -z "$_flag_x" |
|
and echo -en -- $argv[$ID] |
|
or return $ID |
|
return |
|
case ',' k h \[ \' # back |
|
set ITEM[$ID] ' '$argv[$ID]' ' |
|
test 1 -lt $ID |
|
and set ID (math -s0 $ID'- 1') |
|
case '.' j l \] \\ # forth |
|
set ITEM[$ID] ' '$argv[$ID]' ' |
|
test 1 -lt (math -s0 "$LEN - $ID") |
|
and set ID (math -s0 $ID'+ 1') |
|
end #switch |
|
|
|
set ITEM[$ID] "$sel$ITEM[$ID]$clm" |
|
echo -en \e\[A >&2 |
|
string join -- $NL ' '{$ITEM}' ' >&2 |
|
echo -en \e\[H >&2 |
|
end |
|
|
|
end |
|
|
|
end #fun |
|
test -n "$MENU"; or set -l MENU 'pick -d \t Use "[ and ]" or "j and l"' |
|
|
|
|
|
set -l ansi 's/'\e'\[[0-9;]*[m|K|G|A|B|C|D]//g' |
|
set -l ripcl '\e[K\e[B\e[K\e[2A\e[K' |
|
set -l ANS ' 0' |
|
# use N > set R, T, P, echo & PLOG > do R, show T, suggest P |
|
set -l P |
|
set -l R |
|
set -l TIME_fmt +\e\[1m'%H:%M'\e\[2m'.%S'\e\[m\ |
|
set -l -- T (date $TIME_fmt) |
|
set -l -- EPH (date +%s) |
|
|
|
while read -c (echo -e -- "$P") -P (echo -e -- "$R$T") -l N |
|
# simple commands |
|
if test 2 -ge (string length -- "$N"); and string match --invert --quiet -- '=*' "$N" |
|
if test -z "$N" |
|
if test -z "$P" |
|
# don't advance line |
|
set R '\e[A' |
|
else |
|
set R '' |
|
end |
|
set P '' |
|
# do refresh clock |
|
set -- T (date $TIME_fmt) |
|
# where am i? |
|
echo -ne -- $cl'\e[2m' (date +%A\e\[m) (eval $STATUS)'\e[m' |
|
|
|
else if test '///' = "/$N/" -o '/*/' = "/$N/" |
|
set P '' |
|
set R '\e[A' |
|
set -l -- TT (math -s0 -- '('(date +%s)' - '$EPH') /60')' min' |
|
echo -ne -- $cl'\e[4m '$TT' \e[m' |
|
set -- T (date $TIME_fmt) |
|
set -e TT |
|
|
|
else if test '/--/' = "/$N/" |
|
set -- P (echo -e -- "$PLOG[-1]" |sed $ansi) |
|
if test -z "$P" |
|
set P ' ' |
|
end |
|
set R $ripcl'\e[A' |
|
set T '' |
|
set -- PLOG $PLOG[1..-2] |
|
# deleted lines are not logged |
|
|
|
else if test '/-+/' = "/$N/" -o '/+-/' = "/$N/" |
|
set P '' |
|
set R '' |
|
set -- PLOG[-1] "$T"(string trim --left -- "$PLOG[-1]") |
|
echo -e -- '\e[A'$ripcl$PLOG[-1] |
|
set -- EPH (date -j -f %H:%M.%S\ (echo -e -- $T |sed $ansi) +%s) |
|
|
|
else if test '/-/' = "/$N/" |
|
set P '' |
|
set R $ripcl'\e[A' |
|
set -- PLOG $PLOG[1..-2] |
|
# deleted lines are not logged |
|
|
|
else if test '/!/' = "/$N/" |
|
set P '' |
|
set R '' |
|
echo -ne '\e[?1049h' |
|
set -l -- G (eval $MENU) |
|
echo -ne '\e[?1049l' |
|
if test -n "$G" |
|
set -a -- PLOG "$T$G" |
|
echo -e -- '\e[A\e[10G'$G |
|
set -- T (date-fmt t)' ' |
|
else |
|
set -- R '\e[A' |
|
end |
|
|
|
else if test '/`/' = "/$N/" |
|
set -- R '\e[A' |
|
echo -ne '\e[?1049h' |
|
set -l -- G (eval $MENU) |
|
echo -ne '\e[?1049l\e' |
|
if test -n "$G" |
|
set -- P (echo -e -- $T$G |sed $ansi) |
|
set -- T '' |
|
end |
|
|
|
else |
|
set P '' |
|
set R '' |
|
if test '/../' = "/$N/" |
|
set -- ANS $ANS[1..-2] |
|
if test -z "$ANS[-1]" |
|
set -- ANS ' 0' |
|
end |
|
set -a -- PLOG "=$ANS[-1]" |
|
echo -e -- $ripcl$PLOG[-1] |
|
|
|
else if test '/./' = "/$N/" |
|
set -a -- PLOG "=$ANS[-1]" |
|
echo -e -- $ripcl$PLOG[-1] |
|
|
|
else if test '/++/' = "/$N/" |
|
set -a -- PLOG (echo -e -- $cl'\e[2m' (date +%A\e\[m) (eval $STATUS)'\e[m') |
|
echo -e -- $ripcl$PLOG[-1] |
|
set -- T (date $TIME_fmt) |
|
|
|
else if test '/+/' = "/$N/" |
|
set -a -- PLOG '' |
|
echo -e -- $ripcl |
|
|
|
# prompt commands |
|
else if test '/!w/' = "/$N/" |
|
# works with `cat FILE | time-log -` |
|
set -l FILE (date +%Y-%m\ %d\ %H-%M).plog |
|
if touch "$FILE" |
|
string join -- \n $PLOG \n >"$FILE" |
|
echo -e -- $upcl"\e[3m$FILE now saved\e[m" |
|
else |
|
echo -e -- ' \e[1;31mErr\e[m: write to file '$FILE |
|
end |
|
|
|
else |
|
# WONTFIX: don't care if this part slow, you're not running it 10K times |
|
if test '/?/' = "/$N/" -o '/!/' = "/$N/" |
|
echo -ne '\e[3m' |
|
echo -e -- ' <Enter> to refresh the time |
|
⋯ type anything and TIME stays |
|
-- edit last line, replacing it |
|
+⋯ add a NOTE (default: empty line) |
|
-⋯ replace last NOTE (default: delete last note)\n |
|
-+ prepend current TIME to the last line (+- too) |
|
/ show time difference to the last line (* also) |
|
++ insert weekday & STATUS (default: HH:MM.)\n |
|
=⋯ calculate and note down the result (default: 0) |
|
.⋯ use last result (default: show it) |
|
.. discard last result, use one before (or 0 if none)\n |
|
` run MENU and edit resulting string |
|
! insert result of MENU immediately\n |
|
!w (over)write to file (default: DATE HH:MM.plog) |
|
to strip ANSI coloring use `cat FILE | time-log -`\n' |
|
echo -ne -- ' tag: note\n mark:specific.time' |
|
echo -e '\e[m\n' |
|
|
|
else if string match --quiet -- '+*' "$N" |
|
set -l -- M (string sub -s 2 -- "$N") |
|
set -a -- PLOG " $M" |
|
echo -e -- $upcl$PLOG[-1] |
|
# added lines are logged |
|
|
|
else if string match --quiet -- '-*' "$N" |
|
set -l -- M (string sub -s 2 -- "$N") |
|
set -- PLOG[-1] " $M" |
|
echo -e -- $upcl$upcl" $M" |
|
# overwritten lines are overwritten in log |
|
|
|
# just a short note |
|
else |
|
set -a -- PLOG "$T$N" |
|
end |
|
if test -n "$T" |
|
set -- EPH (date -j -f %H:%M.%S\ (echo -e -- "$T" |sed $ansi) +%s) |
|
end |
|
set -- T (date $TIME_fmt) |
|
end |
|
end |
|
|
|
# not simple commands |
|
else |
|
set R '' |
|
if string match --quiet -- '=*' "$N" |
|
set -- N (string sub -s 2 -- "$N") |
|
if test -z "$N" |
|
set N ' 0' |
|
end |
|
set -a -- ANS ' '(math -s2 -- "$N") |
|
set -l -- ANS_len (string length -- " =$ANS[-1]") |
|
## dim go to end back up left bold on cyan rst |
|
set -a -- PLOG (echo -e -- '\e[2m=\e[m'$N'\e['$COLUMNS'G\e['$ANS_len'D\e[1;46m ='$ANS[-1]' \e[m') |
|
echo -e -- $upcl$PLOG[-1] |
|
if test -n "$P" |
|
set P '' |
|
set -- T (date $TIME_fmt) |
|
end |
|
set -e ANS_len |
|
|
|
else if string match --quiet -- '.*' "$N" |
|
set -- N (string sub -s 2 -- "$N") |
|
set -a -- ANS ' '(math -s2 -- "$ANS[-1] $N") |
|
set -l -- ANS_len (string length -- " =$ANS[-1]") |
|
## dim go to end back up left bold on cyan rst |
|
set -a -- PLOG (echo -e -- '\e[2m.\e[m'$N'\e['$COLUMNS'G\e['$ANS_len'D\e[1;46m ='$ANS[-1]' \e[m') |
|
echo -e -- $upcl$PLOG[-1] |
|
if test -n "$P" |
|
set P '' |
|
set -- T (date $TIME_fmt) |
|
end |
|
set -e ANS_len |
|
|
|
else if string match --quiet -- '*:*.*' "$N" |
|
set -l -- M (string split --max 1 -- ' ' "$N") |
|
if test 7 -le (string length -- "$M[1]") |
|
set -a -- PLOG (echo -e -- "\e[m\e[4m$M[1]\e[m $M[2]") |
|
else |
|
set -- M[2] (string trim --left -- "$M[2]") |
|
set -a -- PLOG (echo -e -- "\e[m\e[4m$M[1]\e[m $M[2]") |
|
end |
|
echo -e -- $upcl$PLOG[-1] |
|
if test -n "$P" |
|
set P '' |
|
set -- T (date $TIME_fmt) |
|
end |
|
set -e M |
|
|
|
else if string match --quiet -- '*:*' "$N" |
|
set -l -- M (string split --max 1 -- ':' "$N") |
|
set -- M[2] (string trim --left -- "$M[2]") |
|
set -- M[1] (string upper -- "$M[1]") |
|
set -l -- TT (date +%H:%M.) |
|
## hide dim hide |
|
set -a -- PLOG (echo -e -- "\e[8m$TT.\e[m\e[2m$M[1]\e[m\e[8m:\e[m $M[2]") |
|
echo -e -- $upcl$PLOG[-1] |
|
if test -n "$P" |
|
set P '' |
|
set -- T (date $TIME_fmt) |
|
end |
|
set -e M |
|
set -e TT |
|
|
|
else |
|
if string match --quiet -- '+*' "$N" |
|
set P '' |
|
set -l -- M (string sub -s 2 -- "$N") |
|
set -a -- PLOG " $M" |
|
echo -e -- $upcl$PLOG[-1] |
|
set -- T (date $TIME_fmt) |
|
# added lines are logged |
|
|
|
else if string match --quiet -- '-*' "$N" |
|
set P '' |
|
set -l -- M (string sub -s 2 -- "$N") |
|
set -- PLOG[-1] " $M" |
|
echo -e -- $upcl$upcl" $M" |
|
set -- T (date $TIME_fmt) |
|
# overwritten lines are overwritten in log |
|
|
|
# just a note |
|
else |
|
if test -n "$P" |
|
set P '' |
|
set T '' |
|
end |
|
set -a -- PLOG (echo -e -- "$T$N") |
|
if test -n "$T" |
|
set -- EPH (date -j -f %H:%M.%S\ (echo -e -- $T |sed $ansi) +%s) |
|
end |
|
set -- T (date $TIME_fmt) |
|
end |
|
end # string match commands |
|
end # if len < 2 |
|
end |
|
|
License MIT, free to do whatever. Yours to fuck up.
asciinema.org/a/447537