Last active
June 4, 2022 21:57
-
-
Save bougui505/d838d439c7e1114dffec2a266baf2c19 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env zsh | |
# -*- coding: UTF8 -*- | |
# Author: Guillaume Bouvier -- [email protected] | |
# 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