Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make the printing of the prompt and the keys pressed optional #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions pytimedinput/pytimedinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def timedInput(prompt: str = "", timeout: int = 5, resetOnInput: bool = True, ma
return __timedInput(prompt, timeout, resetOnInput, maxLength, allowCharacters, endCharacters)


def timedKey(prompt: str = "", timeout: int = 5, resetOnInput: bool = True, allowCharacters: str = "") -> Tuple[str, bool]:
def timedKey(prompt: str = "", timeout: int = 5, resetOnInput: bool = True, allowCharacters: str = "", toprint: bool = True) -> Tuple[str, bool]:
"""Ask the user to press a single key out of an optional list of allowed ones.

Args:
Expand All @@ -44,7 +44,7 @@ def timedKey(prompt: str = "", timeout: int = 5, resetOnInput: bool = True, allo
Returns:
Tuple[str, bool]: Which key the user pressed and whether the input timed out or not.
"""
return __timedInput(prompt, timeout, resetOnInput, maxLength=1, allowCharacters=allowCharacters, endCharacters="", inputType="single")
return __timedInput(prompt, timeout, resetOnInput, maxLength=1, allowCharacters=allowCharacters, endCharacters="", inputType="single", toprint=toprint)


def timedInteger(prompt: str = "", timeout: int = 5, resetOnInput: bool = True, allowNegative: bool = True) -> Tuple[Union[int, None], bool]:
Expand Down Expand Up @@ -87,7 +87,7 @@ def timedFloat(prompt: str = "", timeout: int = 5, resetOnInput: bool = True, al
return None, timedOut


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]:
def __timedInput(prompt: str = "", timeout: int = 5, resetOnInput: bool = True, maxLength: int = 0, allowCharacters: str = "", endCharacters: str = "\x1b\n\r", inputType: str = "text", toprint: bool = True) -> Tuple[str, bool]:
def checkStdin():
if(sys.platform == "win32"):
return msvcrt.kbhit()
Expand Down Expand Up @@ -116,7 +116,7 @@ def readStdin():
userInput = ""
timeStart = time.time()
timedOut = False
if(len(prompt) > 0):
if(len(prompt) > 0) and toprint:
print(prompt, end='', flush=True)

while(True):
Expand All @@ -141,16 +141,19 @@ def readStdin():
if(inputCharacter == "." and inputCharacter in userInput):
inputCharacter = ""
userInput = userInput + inputCharacter
print(inputCharacter, end='', flush=True)
if toprint:
print(inputCharacter, end='', flush=True)
if(maxLength == 1 and len(userInput) == 1 and inputType == "single"):
break
else:
if(len(userInput)):
userInput = userInput[0:len(userInput) - 1]
print("\x1b[1D\x1b[0K", end='', flush=True)
if toprint:
print("\x1b[1D\x1b[0K", end='', flush=True)
if(resetOnInput and timeout > -1):
timeStart = time.time()
print("")
if toprint:
print("")
__setStdoutSettings(__savedConsoleSettings)
return userInput, timedOut

Expand Down Expand Up @@ -184,3 +187,4 @@ def __enableStdoutAnsiEscape():
else:
# Should be enabled by default under Linux (and OSX?), just set cbreak-mode
tty.setcbreak(sys.stdin.fileno(), termios.TCSADRAIN)