Skip to content

Commit 03a6c1a

Browse files
committed
reset: Add reset.py
1 parent a907167 commit 03a6c1a

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

src/reset.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/python3
2+
3+
import os
4+
import sys
5+
from optparse import OptionParser
6+
7+
8+
# reset(1), roughly modelled off the ncurses implementation.
9+
10+
11+
def reset(opts, term: str | None = os.environ.get("TERM")):
12+
if opts.q:
13+
if not term:
14+
print(f"unknown terminal type ", file=sys.stderr)
15+
try:
16+
while True:
17+
if term := input("Terminal type? "):
18+
break
19+
except KeyboardInterrupt:
20+
print()
21+
sys.exit(130)
22+
23+
print(term)
24+
return
25+
26+
print("\x1bc", end="")
27+
28+
if opts.r:
29+
print(f"Terminal type is {term}.")
30+
31+
if opts.s:
32+
print(f"TERM={term};")
33+
34+
35+
if __name__ == "__main__":
36+
parser = OptionParser(
37+
usage="Usage: %prog [OPTION]... [IGNORED]...",
38+
description="Initialize or reset the terminal state.",
39+
add_help_option=False,
40+
)
41+
parser.add_option("--help", action="help", help="show usage information and exit")
42+
43+
parser.add_option("-I", action="store_true", help="(unimplemented)")
44+
parser.add_option("-c", action="store_true", help="(unimplemented)")
45+
parser.add_option("-Q", action="store_true", help="(unimplemented)")
46+
47+
parser.add_option(
48+
"-q",
49+
action="store_true",
50+
help="print the terminal type; do not initialize the terminal",
51+
)
52+
parser.add_option(
53+
"-r", action="store_true", help="print the terminal type to standard error"
54+
)
55+
parser.add_option(
56+
"-s",
57+
action="store_true",
58+
help="print the sequence of shell commands to initialize the TERM environment variable",
59+
)
60+
61+
parser.add_option("-w", action="store_true", help="(unimplemented)")
62+
63+
parser.add_option("-e", metavar="CHAR", help="(unimplemented)")
64+
parser.add_option("-i", metavar="CHAR", help="(unimplemented)")
65+
parser.add_option("-k", metavar="CHAR", help="(unimplemented)")
66+
67+
parser.add_option("-m", metavar="MAPPING", help="(unimplemented)")
68+
69+
opts, args = parser.parse_args()
70+
71+
if args and args[0] == "-":
72+
opts.q = True
73+
del args[0]
74+
75+
if args:
76+
reset(opts, args[0])
77+
else:
78+
reset(opts)

0 commit comments

Comments
 (0)