scwrypts/zsh/lib/misc/tally.module.zsh
yage fec8c5e560 basic runner format; write a MAIN function
introduce --verbosity flag rather than mixed logging settings; correct color misnaming to ANSI convention; added sanity-check; simplified hello-world; created FZF_USER_INPUT to replace the confusing FZF_HEAD and FZF_TAIL

swap INFO for DEBUG

v3-to-v4 upgrade docs

bring some much-needed tender love and care to the scwrypts runner

improved i/o handling on the run executable means this is no longer relevant

FINALLY fix the weird cases for zsh/read builtin (particularly around reading one character from tty/pipe/file); also gave a --force-user-input flag in case you want to require user input on a yn prompt

update ZLE plugin so it no more make errors

FZF_(HEAD|TAIL) refactor to FZF_USER_INPUT

plugins/kubectl migration from v3 to v4

plugins/ci migration from v3 to v4

refactor py/lib into python-scwrypts subproject

verbosity is stupid lets call it log-level

fix bug with virtualenv loading

mergedeep to slow so I made my options dict shallow

hokay first iteration of python-dudes is ready

circleci configuration for python builds

npm package for scwrypts

3.9.1

initial build/test steps for nodejs

go

go

ok

ok

fix output

ok

ok

finalize publish steps
2024-02-20 23:08:55 -07:00

107 lines
2.5 KiB
Bash

#####################################################################
DEPENDENCIES+=()
REQUIRED_ENV+=()
#####################################################################
TALLY_USE_REDIS=false # maybe someday
TALLY_PATH="$SCWRYPTS_DATA_PATH/tally"
#####################################################################
TALLY() {
local USAGE="
usage: [...options...]
options:
-c, --increment-count increment the tally by this much (default 1)
-n, --tally-name name of tally system (default 'default')
-g, --get only output the current value
-s, --set set the tally to a specific value
-r, --reset set the tally back to zero
--raw only output the tally value
-h, --help print this dialogue and exit
Simple tally mark system; keep track of a count.
"
local INCREMENT_COUNT=1
local TALLY_NAME=default
local RAW=false
local SET_VALUE=
while [[ $# -gt 0 ]]
do
case $1 in
-c | --increment-count ) INCREMENT_COUNT=$2; shift 1 ;;
-n | --tally-name ) TALLY_NAME=$2; shift 1 ;;
-g | --get ) INCREMENT_COUNT=0 ;;
-s | --set ) SET_VALUE=$2; shift 1 ;;
-r | --reset ) SET_VALUE=0 ;;
--raw ) RAW=true ;;
-h | --help ) USAGE; return 0 ;;
* ) ERROR "unknown argument '$1'" ;;
esac
shift 1
done
[ $TALLY_NAME ] && echo "$TALLY_NAME" | grep -qv '/' \
|| ERROR "invalid tally name '$TALLY_NAME'"
local TALLY_FILENAME="$TALLY_PATH/$TALLY_NAME.txt"
CHECK_ERRORS --no-fail || return 1
##########################################
local NEW_VALUE CURRENT_VALUE=0
[ $SET_VALUE ] && NEW_VALUE=$SET_VALUE || {
[ -f "$TALLY_FILENAME" ] && {
CURRENT_VALUE=$(cat "$TALLY_FILENAME" | tail -n1 | grep '^[0-9]\+')
}
[ $CURRENT_VALUE ] || {
ERROR "malformed tally file '$TALLY_FILENAME'; aborting"
return 1
}
NEW_VALUE=$(($CURRENT_VALUE + $INCREMENT_COUNT))
}
##########################################
local TALLY_DIR="$(dirname "$TALLY_FILENAME")"
[ -d "$TALLY_DIR" ] || mkdir -p "$TALLY_DIR"
[ -d "$TALLY_DIR" ] || {
ERROR "unable to write to '$TALLY_DIR'; aborting"
return 1
}
echo "# autogenerated tally file; avoid direct modification\n$NEW_VALUE" > "$TALLY_FILENAME" || {
ERROR "failed to write to '$TALLY_FILENAME': aborting"
return 1
}
##########################################
case $RAW in
true ) printf "$NEW_VALUE" ;;
false )
case $TALLY_NAME in
default ) SUCCESS "current tally : $NEW_VALUE" ;;
* ) SUCCESS "$TALLY_NAME : $NEW_VALUE" ;;
esac
esac
}