Skip to content

Commit 3fa045f

Browse files
committed
clean up wc, added total printing
1 parent 08500cb commit 3fa045f

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

darkbox/commands/wc.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,19 @@ def get_parser(self):
2626
def run(self):
2727
args = self.get_args()
2828

29+
settings = ['lines', 'bytes', 'words']
30+
2931
# if none are chosen, then choose all (per GNU behavior)
30-
if not any([args['lines'], args['bytes'], args['words']]):
31-
args['lines'] = True
32-
args['bytes'] = True
33-
args['words'] = True
32+
if not any(args[i] for i in settings):
33+
for i in settings:
34+
args[i] = True
3435

35-
totals = {'words': 0, 'lines': 0, 'chars': 0}
36+
totals = {k:0 for k in settings}
3637

3738
for i in args['files']:
3839
try:
3940
with open(i, 'rb') as f:
40-
file_metrics = {'lines': 0, 'bytes': 0, 'words': 0}
41+
file_metrics = {k:0 for k in settings}
4142

4243
curr_char = f.read(1)
4344
while curr_char != b'':
@@ -46,10 +47,16 @@ def run(self):
4647
if curr_char in b'\n\t ': file_metrics['words'] += 1
4748
curr_char = f.read(1)
4849

50+
for k in settings:
51+
totals[k] += file_metrics[k]
52+
4953
print("{} {} {} {}".format(file_metrics['lines'], file_metrics['words'], file_metrics['bytes'], i))
5054

5155
except FileNotFoundError:
5256
self.file_not_found_error(i)
5357

5458
except IsADirectoryError:
5559
self.directory_error(i)
60+
61+
if len(args['files']) > 1:
62+
print("{} {} {} total".format(totals['lines'], totals['words'], totals['bytes']))

0 commit comments

Comments
 (0)