Skip to content

Commit bb30375

Browse files
committed
some amendment
1 parent 484d5db commit bb30375

1 file changed

Lines changed: 20 additions & 28 deletions

File tree

  • implement-shell-tools/wc

‎implement-shell-tools/wc/wc.py‎

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@
2323
count_words = True
2424
count_bytes = True
2525

26+
def print_output(lines, words, bytes_count, label):
27+
output = []
28+
29+
if count_lines:
30+
output.append(f"{lines:8}")
31+
32+
if count_words:
33+
output.append(f"{words:8}")
34+
35+
if count_bytes:
36+
output.append(f"{bytes_count:8}")
37+
38+
output.append(f" {label}")
39+
40+
print("".join(output))
41+
2642

2743
def count_file(filename):
2844
try:
@@ -36,20 +52,7 @@ def count_file(filename):
3652
# UTF-8 bytes, same idea as Buffer.byteLength()
3753
bytes_count = len(content.encode("utf-8"))
3854

39-
output = []
40-
41-
if count_lines:
42-
output.append(str(lines))
43-
44-
if count_words:
45-
output.append(str(words))
46-
47-
if count_bytes:
48-
output.append(str(bytes_count))
49-
50-
output.append(filename)
51-
52-
print(" ".join(output))
55+
print_output(lines, words, bytes_count, filename)
5356

5457
return {
5558
"lines": lines,
@@ -66,6 +69,8 @@ def count_file(filename):
6669
total_words = 0
6770
total_bytes = 0
6871

72+
73+
6974
for file in files:
7075
counts = count_file(file)
7176

@@ -76,17 +81,4 @@ def count_file(filename):
7681

7782

7883
if len(files) > 1:
79-
output = []
80-
81-
if count_lines:
82-
output.append(str(total_lines))
83-
84-
if count_words:
85-
output.append(str(total_words))
86-
87-
if count_bytes:
88-
output.append(str(total_bytes))
89-
90-
output.append("total")
91-
92-
print(" ".join(output))
84+
print_output(total_lines, total_words, total_bytes, "total")

0 commit comments

Comments
 (0)