Skip to content

Instantly share code, notes, and snippets.

@ouuan
Last active June 8, 2021 13:24
Show Gist options
  • Select an option

  • Save ouuan/c88295eea5dd89feecd712e8005d9dfa to your computer and use it in GitHub Desktop.

Select an option

Save ouuan/c88295eea5dd89feecd712e8005d9dfa to your computer and use it in GitHub Desktop.
Get rid of your yay addiction
# https://gist.github.com/ouuan/c88295eea5dd89feecd712e8005d9dfa
# the package dateutils is required for the datediff command
# $1: the command
# $2: the short name of the command
# $3: the command to eval when it's in cd
# $COMMAND_CD_PATH: the path to save the cd time, default is $HOME/.command_cd_<short name or full command>
# $COMMAND_CD_LENGTH: cd length in nanoseconds, default is one hour
command_cd() {
if [[ $# == 0 ]]; then
echo "command_cd <command to have cd> [<command short name> [command to eval in cd]]"
echo "See https://gist.github.com/ouuan/c88295eea5dd89feecd712e8005d9dfa"
return 0
fi
if [[ $COMMAND_CD_PATH == '' ]]; then
local COMMAND_CD_PATH="$HOME/.command_cd_${2:-$1}"
fi
if [[ $COMMAND_CD_LENGTH == '' ]]; then
local COMMAND_CD_LENGTH=36000
fi
local COMMAND_CD_LAST=$(cat "$COMMAND_CD_PATH") 2>/dev/null
local COMMAND_CD_NOW=$(\date +%s)
if [[ $((COMMAND_CD_NOW - COMMAND_CD_LAST)) -le $COMMAND_CD_LENGTH ]]; then
if [[ $3 != '' ]]; then
eval "$3"
fi
echo "\nPlease don't ${2:-$1} in $(datediff -i '%s' -f '%H hours %M minutes %S seconds' $COMMAND_CD_NOW $(( COMMAND_CD_LAST + COMMAND_CD_LENGTH )))"
else
eval "$1"
echo $COMMAND_CD_NOW > "$COMMAND_CD_PATH"
fi
}
yay() {
if [[ $# -gt 0 && "$*" != "-Syu" && "$*" != "-Syyu" ]]; then
paru "$@"
else
COMMAND_CD_LENGTH=72000 command_cd pacwatch yay "cowsay there is nothing to do"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment