-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
75 lines (63 loc) · 1.84 KB
/
main.py
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
#!/usr/bin/python3
import sys
import subprocess
import re
import click
from colorama import Fore, Style
from time import sleep
def test_func():
""" TODO delete function"""
identify = 0
for i in range(3):
identify += i
list_date = True
if identify is not None:
list_date = list(range(5))
res = identify in list_date
return res
def split_string(string2, range2):
"""разбивает строку по диапазону"""
before = string2[:range2[0]]
middle = string2[range2[0]:range2[1]]
after = string2[range2[1]:]
return before, middle, after
def color_string(regexp, text):
"""разукращивает в тексте совпадения по регулярке"""
if re.search(regexp, text) is None:
return False
res = ""
symbol = True
while symbol:
symbol = re.search(regexp, text)
if not symbol:
break
split = split_string(text, symbol.span())
res += split[0] + f"{Fore.RED}{split[1]}{Style.RESET_ALL}"
text = split[2]
res += text
return res
@click.command()
@click.argument('pattern')
@click.argument('file')
def main(pattern, file):
"""occur"""
file = file
try:
while True:
subprocess.call("clear")
with open(file, "r", encoding="utf-8") as f:
for line in enumerate(f, start=1):
string = color_string(pattern, line[1])
if string is False:
continue
print(Fore.GREEN + str(line[0]) + ":" + Style.RESET_ALL, end="")
print(string, end="")
sleep(1)
except KeyboardInterrupt:
print("[ Stop ]")
sys.exit(0)
except re.error:
print("[ error regexp ]")
sys.exit(0)
if __name__ == "__main__":
main()