Update utils.py

This commit is contained in:
CorpNewt 2025-01-29 18:18:42 -06:00 committed by GitHub
parent ea0360e3b4
commit 6d0c238e5d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -137,16 +137,19 @@ class Utils:
def grab(self, prompt, **kwargs): def grab(self, prompt, **kwargs):
# Takes a prompt, a default, and a timeout and shows it with that timeout # Takes a prompt, a default, and a timeout and shows it with that timeout
# returning the result # returning the result
timeout = kwargs.get("timeout", 0) timeout = kwargs.get("timeout",0)
default = kwargs.get("default", None) default = kwargs.get("default","")
if not self.interactive: if not self.interactive:
return default return default
# If we don't have a timeout - then skip the timed sections # If we don't have a timeout - then skip the timed sections
if timeout <= 0: if timeout <= 0:
if sys.version_info >= (3, 0): try:
return input(prompt) if sys.version_info >= (3, 0):
else: return input(prompt)
return str(raw_input(prompt)) else:
return str(raw_input(prompt))
except EOFError:
return default
# Write our prompt # Write our prompt
sys.stdout.write(prompt) sys.stdout.write(prompt)
sys.stdout.flush() sys.stdout.flush()
@ -158,8 +161,10 @@ class Utils:
c = msvcrt.getche() c = msvcrt.getche()
if ord(c) == 13: # enter_key if ord(c) == 13: # enter_key
break break
elif ord(c) >= 32: #space_char elif ord(c) >= 32: # space_char
i += c i += c.decode() if sys.version_info >= (3,0) and isinstance(c,bytes) else c
else:
time.sleep(0.02) # Delay for 20ms to prevent CPU workload
if len(i) == 0 and (time.time() - start_time) > timeout: if len(i) == 0 and (time.time() - start_time) > timeout:
break break
else: else:
@ -175,7 +180,10 @@ class Utils:
def cls(self): def cls(self):
if not self.interactive: if not self.interactive:
return return
os.system('cls' if os.name=='nt' else 'clear') if os.name == "nt":
os.system("cls")
elif os.environ.get("TERM"):
os.system("clear")
def cprint(self, message, **kwargs): def cprint(self, message, **kwargs):
strip_colors = kwargs.get("strip_colors", False) strip_colors = kwargs.get("strip_colors", False)