Skip to content

Instantly share code, notes, and snippets.

@amnonkhen

amnonkhen/.bashrc

Last active Apr 12, 2020
Embed
What would you like to do?
add the linux `watch` tool capability to windows git bash
# add to ~/.bashrc
source ~/.bash/watch.sh

#TODO

  • colored diff is always enabled, allow plain output
  • use getopt/getopts to parse command line options
  • remove the "old" characters drom the colored diff
# save in ~/.bash/watch.sh
function watch() {
POSITIONAL=()
INTERVAL=2
DIFFERENCES=
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-n|--interval)
INTERVAL="$2"
shift # past argument
shift # past value
;;
-d|--differences)
DIFFERENCES=true
shift # past argument
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
ARGS="${@}"
PERIOD=1
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
NORMAL=$(tput sgr0)
echo "" > ~/.last-output
while(true); do
clear
printf '%s %*s %s\n\n' "${GREEN}Every ${INTERVAL}s:${NORMAL} ${ARGS}" 80 "${YELLOW}`date`${NORMAL}"
OUTPUT=`bash -c "$ARGS"`
echo $OUTPUT > ~/.output
if [ "$DIFFERENCES"==true ]; then
git diff --color-words=. --no-index -- ~/.last-output ~/.output 2>/dev/nul | tail -n +6
else
echo -e "${OUTPUT[@]}"
fi
cp -f ~/.output ~/.last-output
sleep ${INTERVAL}
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.