-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtermsize.py
More file actions
executable file
·46 lines (36 loc) · 1.33 KB
/
termsize.py
File metadata and controls
executable file
·46 lines (36 loc) · 1.33 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
#!/usr/bin/env python3
# Procedure for gathering terminal screen size
#
# Magnetic-Fox, 30.06.2024, 05.07.2024, 07.09.2025
#
# (C)2024-2025 Bartłomiej "Magnetic-Fox" Węgrzyn
import readchar
import ansi
# Procedure for gathering terminal's screen size
def getTermSize():
input = ""
# Do the algorithm - set cursor position to 999x999 and then ask terminal for its position (which will automatically be changed to the possible maximum)
ansi.saveCurPos()
ansi.setCurPos(999, 999)
ansi.askCurPos() # Terminal will answer automatically, regardless of what comes next
ansi.restoreCurPos()
ansi.reverseLineFeed() # I put reverse line feed here, because, for some strange reason,
print("") # the whole code works only when the next line is passed
while True:
input2 = readchar.readkey()
input += input2
# You'll be able to interrupt this code if terminal haven't responded - just press ENTER
if(input2[-1] == 'R') or (input2[-1] == readchar.key.ENTER):
break
if(input.find(readchar.key.ESC) > 0):
input = input[input.find(readchar.key.ESC):]
if (len(input) > 2) and (input[0] == readchar.key.ESC and input[1] == "[" and input[-1] == 'R'):
try:
rows = input[2:input.index(';')]
cols = input[input.index(';') + 1:input.index('R')]
output = (int(cols), int(rows))
except:
output = None
else:
output = None
return output