-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprompt.py
More file actions
114 lines (88 loc) · 3.71 KB
/
prompt.py
File metadata and controls
114 lines (88 loc) · 3.71 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# MIT License
# Copyright (c) 2024 Turun ammatti-instituutti
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import os
import msvcrt
import ctypes
from ctypes import c_long, c_ulong, c_wchar_p, c_void_p
gHandle = ctypes.windll.kernel32.GetStdHandle(c_long(-11))
def read():
"""
Read a key from the keyboard and return it's integer value of the character.
Some keys have 2 codes in which case this function should be invoked a second time
to read the second code.
:return: the code of the pressed key.
:rtype: int
"""
return ord(msvcrt.getch())
def readch():
"""
Read a key from the keyboard and return it's unicode string representation.
Some keys have 2 codes in which case this function should be invoked a second time
to read the second code unicode representation.
:return: a character representing the code of the pressed key.
:rtype: string
"""
return msvcrt.getch().decode("utf-8")
def write(string, line = None, column = None):
"""
Write a string in the terminal. If no poision is specified using the line and column
parameters, the text is displayed at the current cursor position.
:param string: the text to write in the terminal
:type string: string
:param line: line position in the terminal where to write the text
:type line: int
:param column: column position in the terminal where to write the text
:type column: int
"""
if line != None and column != None:
move(line, column)
ctypes.windll.kernel32.WriteConsoleW(gHandle, c_wchar_p(string), c_ulong(len(string)), c_void_p(), None)
def clear():
"""
Clear the terminal and set cursor to top/left corner.
"""
move(0, 0)
terminal_size = os.get_terminal_size()
write(" " * (terminal_size.columns * terminal_size.lines))
move(0, 0)
def size():
"""
Gets the current size of the terminal.
:return: a dictionary with lines and columns fields specifying the size of the terminal.
:rtype: dict
"""
return os.get_terminal_size()
def move(line, column):
"""
Move cursor to position indicated by x and y.
:param line: line position in the terminal where to move the cursor
:type line: int
:param column: column position in the terminal where to move the cursor
:type column: int
"""
value = column + (line << 16)
ctypes.windll.kernel32.SetConsoleCursorPosition(gHandle, c_ulong(value))
"""
NOTE: The special keys always provide 2 codes, so two executions of read() function
are required and the actual key code is provided by the second execution.
The first execution on read() provides either 0 or 224, depending on the machine.
"""
KEY_UP = 72
KEY_DOWN = 80
KEY_LEFT = 75
KEY_RIGHT = 77