forked from realpython/materials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_gather.py
More file actions
35 lines (25 loc) · 951 Bytes
/
Copy pathcount_gather.py
File metadata and controls
35 lines (25 loc) · 951 Bytes
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
import asyncio
import sys
import colorama
from colorama import Cursor
colorama.init()
async def print_at(row, text):
print(Cursor.POS(1, 1 + row) + str(text))
await asyncio.sleep(0.03)
async def count_lines_in_file(file_num, file_name):
counter_text = f"{file_name[:20]:<20} "
with open(file_name, mode="rt", encoding="utf-8") as file:
for line_num, _ in enumerate(file, start=1):
counter_text += "□"
await print_at(file_num, counter_text)
await print_at(file_num, f"{counter_text} ({line_num})")
async def count_all_files(file_names):
tasks = [
asyncio.create_task(count_lines_in_file(file_num, file_name))
for file_num, file_name in enumerate(file_names, start=1)
]
await asyncio.gather(*tasks)
if __name__ == "__main__":
print(colorama.ansi.clear_screen())
asyncio.run(count_all_files(sys.argv[1:]))
print(Cursor.POS(1, 1 + len(sys.argv)))