fix: resolve Linux compatibility issue in Getch class

msvcrt platform detection was broken - the try/except ImportError in
__init__ never triggered since the import attempt already happened at
module level. Replaced with a _has_msvcrt boolean flag set at import
time to correctly route Linux to _getch_unix. Also fixed _getch_unix
to return ch.encode() so key comparisons behave consistently across
platforms.
This commit is contained in:
mystixmew
2026-05-28 17:43:18 -04:00
parent 1bd33a0635
commit e7b58edcd1
@@ -1,17 +1,21 @@
import os
import json
import re
try: import msvcrt
except: print("Could not import msvcrt")
try:
import msvcrt
_has_msvcrt = True
except ImportError:
_has_msvcrt = False
print("Could not import msvcrt")
import pandas as pd
import soundfile as sf
# Quick class to make getch work on all systems
class Getch:
def __init__(self):
try:
if _has_msvcrt:
self.impl = self._getch_windows
except ImportError:
else:
self.impl = self._getch_unix
def _getch_windows(self):
@@ -26,7 +30,7 @@ class Getch:
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
return ch.encode()
def __call__(self): return self.impl()
getch = Getch()
@@ -1002,7 +1006,4 @@ print("\nPress [Esc] to exit")
while True:
input = getch()
if input == b'\x1b':
quit()
quit()