| 28 | os.chdir(cwd) |
| 29 | |
| 30 | def grab(self, prompt, **kwargs): |
| 31 | # Takes a prompt, a default, and a timeout and shows it with that timeout |
| 32 | # returning the result |
| 33 | timeout = kwargs.get("timeout", 0) |
| 34 | default = kwargs.get("default", None) |
| 35 | # If we don't have a timeout - then skip the timed sections |
| 36 | if timeout <= 0: |
| 37 | return input(prompt) |
| 38 | # Write our prompt |
| 39 | sys.stdout.write(prompt) |
| 40 | sys.stdout.flush() |
| 41 | if os.name == "nt": |
| 42 | start_time = time.time() |
| 43 | i = "" |
| 44 | while True: |
| 45 | if msvcrt.kbhit(): |
| 46 | c = msvcrt.getche() |
| 47 | if ord(c) == 13: # enter_key |
| 48 | break |
| 49 | elif ord(c) >= 32: # space_char |
| 50 | i += c.decode("utf-8") |
| 51 | if len(i) == 0 and (time.time() - start_time) > timeout: |
| 52 | break |
| 53 | else: |
| 54 | i, o, e = select.select([sys.stdin], [], [], timeout) |
| 55 | if i: |
| 56 | i = sys.stdin.readline().strip() |
| 57 | print("") # needed to move to next line |
| 58 | if len(i) > 0: |
| 59 | return i |
| 60 | else: |
| 61 | return default |
| 62 | |
| 63 | def cls(self): |
| 64 | os.system("cls" if os.name == "nt" else "clear") |