Skip to content

Instantly share code, notes, and snippets.

@bougui505
Last active June 4, 2022 21:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bougui505/d838d439c7e1114dffec2a266baf2c19 to your computer and use it in GitHub Desktop.
Save bougui505/d838d439c7e1114dffec2a266baf2c19 to your computer and use it in GitHub Desktop.
#!/usr/bin/env zsh
# -*- coding: UTF8 -*-
# Author: Guillaume Bouvier -- guillaume.bouvier@pasteur.fr
# https://research.pasteur.fr/en/member/guillaume-bouvier/
# 2020-01-10 16:20:44 (UTC+0100)
# Simple bar chart plotting in a terminal by reading integer from stdin
# FIELD: Field number to read the value from
usage () {
cat << EOF
Usage:
bar [-f FIELD] [-s SCALE]
EOF
exit
}
SCALE=1
while getopts ':h:f:s:' opt; do
case $opt in
(f) FIELD=$OPTARG;;
(s) SCALE=$OPTARG;;
(h) usage;;
(*) usage;;
esac
done
MAXLEN=100 # Maximum length of the bar
cat /dev/stdin | awk -v FIELD=$FIELD -v MAXLEN=$MAXLEN -v SCALE=$SCALE '{
printf $0"\t"
LENGTH=$FIELD*SCALE
if (LENGTH > MAXLEN){
LENGTH=MAXLEN
}
for (i=1;i<=LENGTH;i++){
printf "■"
}
if (LENGTH > MAXLEN){
printf "..."
}
printf "\n"
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment