-
Notifications
You must be signed in to change notification settings - Fork 0
/
h-barplot
executable file
·112 lines (92 loc) · 2.97 KB
/
h-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
#!/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
BIGKEY=0 # usually you dont have to touch this
# 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
# Doing the right formatting for a global
# tabulator spaces work
if [ ${#KEY} -gt 23 ]; then
BIGKEY=3
elif [ ${#KEY} -gt 15 ]; then
BIGKEY=2
elif [ ${#KEY} -gt 7 ]; then
BIGKEY=1
fi
# Fishing the biggest value
(( VALUE > MAXVALUE )) && MAXVALUE=$VALUE
done
# Creating the right factor to fit the bars up to your terminal
FACTORCOUNT=0
FACTOR=1
while [ ${FACTORCOUNT} -lt $(( ${#MAXVALUE} - 2 )) ]; do
FACTOR="${FACTOR}0"
((FACTORCOUNT++))
done
# Start graphing
for data in $SUM; do
KEY=${data%%:*}
VALUE=${data##*:}
REAL_VALUE="$VALUE"
COUNTER="0"
# Do value scaling action for terminal
VALUE=$(($VALUE / $FACTOR))
# Do key scaling action for terminal
if [ ${#KEY} -ge 23 ]; then
KEY=${KEY:0:22}
fi
# Formatting the keys for your graph
if [ $BIGKEY -eq 3 -a ${#KEY} -lt 8 ]; then
echo -n -e "${KEY}\t\t\t\t"
elif [ $BIGKEY -eq 3 -a ${#KEY} -lt 15 ]; then
echo -n -e "${KEY}\t\t\t"
elif [ $BIGKEY -eq 3 -a ${#KEY} -lt 23 ]; then
echo -n -e "${KEY}\t\t"
elif [ $BIGKEY -eq 2 -a ${#KEY} -lt 8 ]; then
echo -n -e "${KEY}\t\t\t"
elif [ $BIGKEY -eq 2 -a ${#KEY} -lt 15 ]; then
echo -n -e "${KEY}\t\t"
elif [ $BIGKEY -eq 1 -a ${#KEY} -lt 8 ]; then
echo -n -e "${KEY}\t\t"
else
echo -n -e "${KEY}\t"
fi
# Putting the choosen char for border
echo -n "$BORDERCHAR"
# Putting a output char for each (scaled) value
# If the last char was written, write the real value
while [ $COUNTER -le $VALUE ]; do
((COUNTER++))
if [ $COUNTER -gt $VALUE ]; then
echo -n " ($REAL_VALUE)" ; echo
else
echo -n "$OUTPUTCHAR"
fi
done
done