-
Notifications
You must be signed in to change notification settings - Fork 0
/
scroll.awk
executable file
·167 lines (151 loc) · 3.46 KB
/
scroll.awk
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/awk -f
# Scrolling Bar Graph
# by Roger Knobbe <[email protected]>
# specify f=3 on the command line to parse field 3 of the input
# specify p="foo.*bar" on the command line indicate field 3 lives in lines that
# match the pattern "foo.*bar"
# specify h="baz.*bimbo" on the command line to indicate the pattern
# that matches header strings in the input
# specify q=1 on the command line to supress header
# specify units=8 on the command line to display (say) bits per seconds
# instead of bytes per second
func min(x,y) {
if (x < y)
return x;
return y;
}
BEGIN {
log10 = log(10)
#HEIGHT=23
#WIDTH=70
#"tput li" | getline rows
#"tput co" | getline cols
"tput lines" | getline rows
"tput cols" | getline cols
HEIGHT = int((7*rows/8)/10) * 10
WIDTH=int(cols/2)
max=0
field=f
system("clear")
# print "screen is " HEIGHT "x" WIDTH
"tput home" | getline cls
# these are for linux
"tput el" | getline eol
"tput bold" | getline so
"tput rmso" | getline rmso
"tput civis"| getline civis
"tput ed" | getline cd
# these are for freebsd
# "tput ce" | getline eol
# "tput so" | getline so
# "tput se" |getline rmso
# "tput vi" | getline civis
# "tput cd" | getline cd
numeric=0
if ("" == p)
p = "."
if ("" == h)
h = "."
if (1 == q)
noheader = 1
if (0 == units)
units = 1
}
($0 ~ p) && ($field ~ /^[0-9 .]+$/) {
numeric=1
printf("%s%s", cls,civis )
newval = units * $field
if (( max < newval )) {
max=newval
maxline=$0
}
vals[WIDTH]=newval
lines[WIDTH]=$0
if (( max == 0 )) {
scale=1
} else {
pretty_max = exp(log10 * (1+int((log(max)/log10))))
scale_width = sprintf(":%%%d.%df", 1+log(pretty_max)/log10,
(pretty_max < HEIGHT) ? 2 : 0);
sample_width = sprintf("<-%%%d.%df", 1+log(pretty_max)/log10,
(pretty_max < HEIGHT) ? 2 : 0);
sample_pad = sprintf(" %%%d.%ds", 1+log(pretty_max)/log10 , (pretty_max < HEIGHT) ? 2 : 0);
scale=HEIGHT/pretty_max
}
newmax=0
least=max
tot=0
i=0
while (( i < WIDTH ))
{
# scroll left and scale
vals[i]=vals[i+1]
lines[i]=lines[i+1]
tot += vals[i]
dsp[i]= scale * vals[i]
if (( vals[i] > newmax )) {
newmax=vals[i]
maxline=lines[i]
maxi=i
}
if (( vals[i] < least )) {
least = vals[i]
}
# print "i=", i, "vals[i]=", vals[i], "dsp[i]=", dsp[i]
i=i+1
}
average=tot/WIDTH
dsp_average = int(scale * average)
dsp_max = int(scale * newmax)
dsp_min = int(scale * least)
dsp_current = int(scale * newval)
y=HEIGHT
while (( y>=0 ))
{
print eol
x=0
while (( x < WIDTH ))
{
if (( dsp[x] >= y )) {
#if ((y <= 0) && (x == maxi)){
if ((x == maxi)){
printf "^"
} else {
printf "#"
}
} else if (y == dsp_average) {
printf "."
} else {
printf " "
}
x=x+1
}
printf(scale_width, y/scale)
if (y == dsp_current)
printf(sample_width, newval);
else
printf(sample_pad, " ")
if (y == dsp_max) printf(" /%g\\", newmax);
if (y == dsp_average) printf(" [%g]", average);
if (y == dsp_min) printf(" \\%g/", least);
y=y-1
}
print eol
$field= so $field rmso
if (!noheader) {
print title,eol
# print $0,eol
#print "field", field, "value=" newval, "average=" average,eol
printf("%s <- Max %s\n", maxline,eol)
}
print cd
max=newmax
}
$0 ~ h {
if (numeric)
{
title=""
numeric=0
}
title = title eol "\n" $0
}