-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcounter
More file actions
executable file
·101 lines (86 loc) · 2.79 KB
/
counter
File metadata and controls
executable file
·101 lines (86 loc) · 2.79 KB
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
#!/usr/bin/env python
#
# Count occurrences of specified characters from stdin
#
# examples:
# ruby -e '10.times{print %w{- +}[rand 2];sleep 0.25}' | counter +-
# Counter: +:6 -:4
#
# perl -e '$|++;map{print int rand 2?"+":"-";sleep 1}1..10' | counter +-
# Counter: +:3 -:7
#
# echo -n "201005+++--+-201006+---+" | counter +- -g "^[0-9]{6}$"
# 201005 +:4 -:3
# 201006 +:2 -:3
#
# Unexpected characters are sent to stderr
# echo -n "201005+?++--+-201006+---+" | counter +- -g "^[0-9]{6}$"
# ?
# 201005 +:4 -:3
# 201006 +:2 -:3
#
# and they can be redirected without hiding the count
# echo -n "201005+?++--+-201006+---+" | counter +- -g "^[0-9]{6}$" 2>/dev/null
# 201005 +:4 -:3
# 201006 +:2 -:3
#
# or they can be silenced all together
# echo -n "201005+?++--+-201006+---+" | counter +- -qg "^[0-9]{6}$"
# 201005 +:4 -:3
# 201006 +:2 -:3
#
import sys
import time
import signal
import re
import argparse
def signal_handler(signal, frame):
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
triggers = {}
parser = argparse.ArgumentParser(description='Count occurrences of specified characters from stdin')
parser.add_argument("characters", type=str, help="The characters to count. ex: +-")
parser.add_argument("-g", "--group", type=str, dest="group", help="A regexp pattern to group counts by")
parser.add_argument("-s", "-q", "--suppress", action="store_true", dest="suppress", help="Don't send unrecognised characters to stderr")
parser.add_argument("-n", "--no-return", action="store_true", dest="noreturn",
help="Don't start stderr output with a carriage return. Useful when redirecting stderr")
options = parser.parse_args()
for s in options.characters:
triggers[s] = 0
buffer = ''
group = ''
prev_row_length = -1
while 1:
c = sys.stdin.read(1)
if not c:
break
if triggers.has_key(c):
if options.group and buffer:
m = re.search(options.group, buffer)
if m:
if group:
sys.stdout.write("\n")
group = m.group(0)
buffer = buffer[:m.start()] + buffer[m.end():]
for trigger in triggers.keys():
triggers[trigger] = 0
if buffer:
if not options.suppress:
if not options.noreturn:
sys.stderr.write( "\r" )
sys.stderr.write( "%s\n" % buffer.ljust(prev_row_length) )
sys.stderr.flush()
buffer = ''
triggers[c] = triggers[c] + 1
else:
buffer += c
if options.group and not group:
continue
output = group or "Counter"
for trigger, count in triggers.items():
output += " %s:%s" % (trigger, count)
sys.stdout.write("\r%s" % output)
sys.stdout.flush()
prev_row_length = len(output)
sys.stdout.write("\n")
sys.stdout.flush()