Skip to content

Commit d8dc9a7

Browse files
Logic for totals completed.
Totals saved as an object just like the file details so we can format the output easily
1 parent 95b2ec8 commit d8dc9a7

1 file changed

Lines changed: 17 additions & 11 deletions

File tree

  • implement-shell-tools/wc

implement-shell-tools/wc/wc.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,38 @@
1515
file_paths = args.path
1616

1717
file_details_list = []
18+
line_count_total = 0
19+
word_count_total = 0
20+
file_size_total = 0
1821

1922
# Get file details
2023
for file_path in file_paths:
2124
with open(file_path, "r", encoding="utf-8") as f:
2225
content = f.read()
2326

2427
details = {
25-
"file_path": file_path,
26-
"file_size": os.path.getsize(file_path),
2728
"line_count": content.count("\n"), # matches real wc -l
2829
"word_count": len(content.split()), # split on whitespace
30+
"file_size": os.path.getsize(file_path),
31+
"file_path": file_path,
2932
}
33+
# Update totals
34+
line_count_total += details["line_count"]
35+
word_count_total += details["word_count"]
36+
file_size_total += details["file_size"]
37+
total_path = "Total"
3038

3139
file_details_list.append(details)
3240

33-
34-
print(file_details_list)
35-
36-
def get_line_count(text):
37-
return text.count("\n")
38-
39-
40-
def get_word_count(text):
41-
return len(text.split())
41+
totals_details = {
42+
"line_count": line_count_total,
43+
"word_count": word_count_total,
44+
"file_size": file_size_total,
45+
"file_path": "total"
46+
}
4247

4348

49+
print(totals_details)
4450

4551
show_all = not (args.lines or args.words or args.bytes)
4652

0 commit comments

Comments
 (0)