-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfold-at.py
More file actions
executable file
·65 lines (52 loc) · 1.28 KB
/
fold-at.py
File metadata and controls
executable file
·65 lines (52 loc) · 1.28 KB
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
#!/usr/bin/env -S uv --quiet run --no-project --script --
# https://peps.python.org/pep-0723/
# https://github.com/astral-sh/uv
# /// script
# # Docopt issues SyntaxWarning in Python 3.12
# requires-python = ">=3.11,<3.12"
# dependencies = [
# "docopt >=0.6.2",
# ]
# ///
"""
Usage:
{prog} REGEX_PATTERN
"""
import sys, locale, os, re
import docopt
def main(*, args, prog):
locale.setlocale(locale.LC_ALL, "")
params = docopt.docopt(
__doc__.replace("\t", " " * 4).format(prog=os.path.basename(prog)),
argv=args,
help=True,
version=True,
options_first=False
)
pattern = params.pop("REGEX_PATTERN")
assert not params, params
pattern = re.compile(pattern)
the_input = os.fdopen(sys.stdin.fileno(), mode="rb", buffering=0)
the_output = os.fdopen(sys.stdout.fileno(), mode="wb", buffering=0)
did_save_position = False
while line := the_input.readline():
if pattern.match(line.decode()):
if did_save_position:
the_output.write(b"\x1b8\x1b[0J")
else:
the_output.write(b"\x1b7")
did_save_position = True
the_output.write(line)
the_output.flush()
def smain(argv=None):
if argv is None:
argv = sys.argv
try:
return main(
args=argv[1:],
prog=argv[0]
)
except KeyboardInterrupt:
print(file=sys.stderr)
if __name__ == "__main__":
sys.exit(smain())