-
Notifications
You must be signed in to change notification settings - Fork 0
/
v-barplot
executable file
·132 lines (112 loc) · 3.31 KB
/
v-barplot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/bin/bash
# statistical: gernerate easy statistical informations in bash
# Copyright: (C) 2011 Florian Baumann <flo@noqqe.de>
# License: GPL-3 <http://www.gnu.org/licenses/gpl-3.0.txt>
# Date: Tuesday 2011-04-12
### Locales
SUM="$*" # see all key value pairs
OUTPUTCHAR='#' # configure this to your favorite ascii char
BORDERCHAR='|' # configure this to your favorite ascii char
BOTTOMCHAR='='
# Read from stdin hack
if [ -z "$*" ]; then
SUM=$(cat /dev/stdin)
fi
### Scaling and environment analysis for optimal output on
# the most terminals
for data in $SUM; do
# Getting informations from parameters
KEY=${data%%:*}
VALUE=${data##*:}
# Fishing errors from wrong usage
if [[ ! $VALUE =~ ^[[:digit:]]+$ ]]; then
echo "ERROR: Do not use characters as value! Key:$KEY Value:$VALUE"
exit 1
fi
# Bash couldn't handle values with more than 19 numbers
# Values longer than 19 will be transformed into strings.
# String could not be graphed :)
if [[ ${#VALUE} -gt 19 ]]; then
echo "Error: Bash couldn't handle values with more than 19 numbers. Sorry."
exit 1
fi
# Fishing the biggest value
(( VALUE > MAXVALUE )) && MAXVALUE=$VALUE
done
# Scaling action
FACTORCOUNT=0
if [ ${MAXVALUE:0:1} -gt 7 ] && [ ${#MAXVALUE} -gt 1 ]; then
FACTOR=3
elif [ ${MAXVALUE:0:1} -gt 2 ] && [ ${#MAXVALUE} -gt 1 ]; then
FACTOR=2
else
FACTOR=1
fi
while [ ${FACTORCOUNT} -lt $(( ${#MAXVALUE} - 2 )) ]; do
FACTOR="${FACTOR}0"
((FACTORCOUNT++))
done
# Start graphing
COUNTER="$(($MAXVALUE / $FACTOR))"
while [ $COUNTER -gt 0 ]; do
# Print (scaled) number and limiter
SIGNC=$(($COUNTER * $FACTOR))
while [ ${#SIGNC} -lt ${#MAXVALUE} ]; do
SIGNC=" $SIGNC"
done
echo -n "$SIGNC $BORDERCHAR"
# Looping all data sets for each Step (downwards)
for data in $SUM ; do
# Parsing data
KEY=${data%%:*}
VALUE=${data##*:}
VALUE=$(($VALUE / $FACTOR))
# If number equal or less the actual counter print a char
if [ $COUNTER -le $VALUE ]; then
echo -n "${OUTPUTCHAR}${OUTPUTCHAR}${OUTPUTCHAR} "
else
echo -n " "
fi
done
# Round ends, do next (scaled) round
((COUNTER--))
echo
# Bulding bottom lines
if [ $COUNTER -le 0 ]; then
# Bottom of Graph
SPACEC=0
while [ $SPACEC -le ${#MAXVALUE} ]; do
echo -n " "
((SPACEC++))
done
echo -n "$BORDERCHAR"
for name in $SUM; do
echo -n "${BOTTOMCHAR}${BOTTOMCHAR}${BOTTOMCHAR}${BOTTOMCHAR}"
done
echo
# Key names
SPACEC=0
while [ $SPACEC -le ${#MAXVALUE} ]; do
echo -n " "
((SPACEC++))
done
echo -n " "
for name in $SUM; do
KEY=${name%%:*}
if [ ${#KEY} -ge 4 ]; then
KEY=${KEY:0:3}
fi
# Do key scaling action for terminal
if [ ${#KEY} -ge 4 ]; then
KEY=${KEY:0:3}
elif [ ${#KEY} -eq 2 ]; then
echo -n "$KEY "
elif [ ${#KEY} -eq 1 ]; then
echo -n "$KEY "
else
echo -n "$KEY "
fi
done
echo
fi
done