Skip to content

Commit 327b7b6

Browse files
committed
add polling rate
Add pollRate argument to all pytimedinput functions, as well as extending docs. Should not change behaviour for people already using the library.
1 parent 95a4edc commit 327b7b6

1 file changed

Lines changed: 15 additions & 10 deletions

File tree

pytimedinput/pytimedinput.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import termios
1212

1313

14-
def timedInput(prompt: str = "", timeout: int = 5, resetOnInput: bool = True, maxLength: int = 0, allowCharacters: str = "", endCharacters: str = "\x1b\n\r") -> Tuple[str, bool]:
14+
def timedInput(prompt: str = "", timeout: int = 5, resetOnInput: bool = True, maxLength: int = 0, allowCharacters: str = "", endCharacters: str = "\x1b\n\r", pollRate: float = 0) -> Tuple[str, bool]:
1515
"""Ask the user for text input with an optional timeout and limit on allowed characters.
1616
1717
Args:
@@ -21,6 +21,7 @@ def timedInput(prompt: str = "", timeout: int = 5, resetOnInput: bool = True, ma
2121
maxLength (int, optional): Maximum length of input user is to be allowed to type. Defaults to 0, use 0 to disable.
2222
allowCharacters (str, optional): Which characters the user is allowed to enter. Defaults to "", ie. any character.
2323
endCharacters (str, optional): On which characters to stop accepting input. Defaults to "\\x1b\\n\\r", ie. ESC and Enter. Cannot be empty.
24+
pollRate (float, optional): How long to sleep between polls. Defaults to 0, use 0 to disable.
2425
2526
Returns:
2627
Tuple[str, bool]: The characters input by the user and whether the input timed out or not.
@@ -29,65 +30,67 @@ def timedInput(prompt: str = "", timeout: int = 5, resetOnInput: bool = True, ma
2930
return "", False
3031
if(len(endCharacters) == 0):
3132
return "", False
32-
return __timedInput(prompt, timeout, resetOnInput, maxLength, allowCharacters, endCharacters)
33+
return __timedInput(prompt, timeout, resetOnInput, maxLength, allowCharacters, endCharacters, pollRate)
3334

3435

35-
def timedKey(prompt: str = "", timeout: int = 5, resetOnInput: bool = True, allowCharacters: str = "") -> Tuple[str, bool]:
36+
def timedKey(prompt: str = "", timeout: int = 5, resetOnInput: bool = True, allowCharacters: str = "", pollRate: float = 0) -> Tuple[str, bool]:
3637
"""Ask the user to press a single key out of an optional list of allowed ones.
3738
3839
Args:
3940
prompt (str, optional): The prompt to be displayed to the user. Defaults to "".
4041
timeout (int, optional): How many seconds to wait for input. Defaults to 5, use -1 to wait forever.
4142
resetOnInput (bool, optional): Reset the timeout-timer any time user presses a key. Defaults to True.
4243
allowCharacters (str, optional): Which characters the user is allowed to enter. Defaults to "", ie. any character.
44+
pollRate (float, optional): How long to sleep between polls. Defaults to 0, use 0 to disable.
4345
4446
Returns:
4547
Tuple[str, bool]: Which key the user pressed and whether the input timed out or not.
4648
"""
47-
return __timedInput(prompt, timeout, resetOnInput, maxLength=1, allowCharacters=allowCharacters, endCharacters="", inputType="single")
49+
return __timedInput(prompt, timeout, resetOnInput, maxLength=1, allowCharacters=allowCharacters, endCharacters="", inputType="single", pollRate = pollRate)
4850

4951

50-
def timedInteger(prompt: str = "", timeout: int = 5, resetOnInput: bool = True, allowNegative: bool = True) -> Tuple[Union[int, None], bool]:
52+
def timedInteger(prompt: str = "", timeout: int = 5, resetOnInput: bool = True, allowNegative: bool = True, pollRate: float = 0) -> Tuple[Union[int, None], bool]:
5153
"""Ask the user to enter an integer value.
5254
5355
Args:
5456
prompt (str, optional): The prompt to be displayed to the user. Defaults to "".
5557
timeout (int, optional): How many seconds to wait for input. Defaults to 5, use -1 to wait forever.
5658
resetOnInput (bool, optional): Reset the timeout-timer any time user presses a key. Defaults to True.
5759
allowNegative (bool, optional): Whether to allow the user to enter a negative value or not.
60+
pollRate (float, optional): How long to sleep between polls. Defaults to 0, use 0 to disable.
5861
5962
Returns:
6063
Tuple[Union[int, None], bool]: The value entered by the user and whether the input timed out or not.
6164
"""
6265
userInput, timedOut = __timedInput(
63-
prompt, timeout, resetOnInput, allowCharacters="-" if(allowNegative) else "", inputType="integer")
66+
prompt, timeout, resetOnInput, allowCharacters="-" if(allowNegative) else "", inputType="integer", pollRate = pollRate)
6467
try:
6568
return int(userInput), timedOut
6669
except:
6770
return None, timedOut
6871

6972

70-
def timedFloat(prompt: str = "", timeout: int = 5, resetOnInput: bool = True, allowNegative: bool = True) -> Tuple[Union[float, None], bool]:
73+
def timedFloat(prompt: str = "", timeout: int = 5, resetOnInput: bool = True, allowNegative: bool = True, pollRate: float = 0) -> Tuple[Union[float, None], bool]:
7174
"""Ask the user to enter a floating-point value.
7275
7376
Args:
7477
prompt (str, optional): The prompt to be displayed to the user. Defaults to "".
7578
timeout (int, optional): How many seconds to wait for input. Defaults to 5, use -1 to wait forever.
7679
resetOnInput (bool, optional): Reset the timeout-timer any time user presses a key. Defaults to True.
7780
allowNegative (bool, optional): Whether to allow the user to enter a negative value or not.
81+
pollRate (float, optional): How long to sleep between polls. Defaults to 0, use 0 to disable.
7882
7983
Returns:
8084
Tuple[Union[float, None], bool]: The value entered by the user and whether the input timed out or not.
8185
"""
8286
userInput, timedOut = __timedInput(
83-
prompt, timeout, resetOnInput, allowCharacters="-" if(allowNegative) else "", inputType="float")
87+
prompt, timeout, resetOnInput, allowCharacters="-" if(allowNegative) else "", inputType="float", pollRate = pollRate)
8488
try:
8589
return float(userInput), timedOut
8690
except:
8791
return None, timedOut
8892

89-
90-
def __timedInput(prompt: str = "", timeout: int = 5, resetOnInput: bool = True, maxLength: int = 0, allowCharacters: str = "", endCharacters: str = "\x1b\n\r", inputType: str = "text") -> Tuple[str, bool]:
93+
def __timedInput(prompt: str = "", timeout: int = 5, resetOnInput: bool = True, maxLength: int = 0, allowCharacters: str = "", endCharacters: str = "\x1b\n\r", inputType: str = "text", pollRate: float = 0) -> Tuple[str, bool]:
9194
def checkStdin():
9295
if(sys.platform == "win32"):
9396
return msvcrt.kbhit()
@@ -150,6 +153,8 @@ def readStdin():
150153
print("\x1b[1D\x1b[0K", end='', flush=True)
151154
if(resetOnInput and timeout > -1):
152155
timeStart = time.time()
156+
if(pollRate != 0):
157+
time.sleep(pollRate)
153158
print("")
154159
__setStdoutSettings(__savedConsoleSettings)
155160
return userInput, timedOut

0 commit comments

Comments
 (0)