Skip to content

Commit

Permalink
Use actual enum, rework imports
Browse files Browse the repository at this point in the history
  • Loading branch information
inducer committed Jan 22, 2025
1 parent 6cdf15c commit 99a3328
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
1 change: 1 addition & 0 deletions .pylintrc-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
- IPython
- py.test
- pytest
- msvcrt
2 changes: 1 addition & 1 deletion pudb/debugger.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

import urwid

from pudb.lowlevel import decode_lines, ConsoleSingleKeyReader, ui_log
from pudb.lowlevel import ConsoleSingleKeyReader, decode_lines, ui_log
from pudb.settings import get_save_config_path, load_config, save_config


Expand Down
37 changes: 21 additions & 16 deletions pudb/lowlevel.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
__copyright__ = """
Copyright (C) 2009-2017 Andreas Kloeckner
Copyright (C) 2014-2017 Aaron Meurer
Copyright (C) 2024 Gerhard Sittig
"""

__license__ = """
Expand All @@ -27,6 +28,7 @@
import logging
import sys
from datetime import datetime
from enum import Enum, auto


logfile = [None]
Expand Down Expand Up @@ -286,21 +288,22 @@ def decode_lines(lines):

# {{{ get single key press from console outside of curses

( _KEYREAD_IMPL_INPUT, _KEYREAD_IMPL_GETCH, _KEYREAD_IMPL_SELECT, ) = range(3)
_keyread_impl = _KEYREAD_IMPL_INPUT
class KeyReadImpl(Enum):
INPUT = auto()
GETCH = auto()
SELECT = auto()


_keyread_impl = KeyReadImpl.INPUT
if sys.platform in ("emscripten", "wasi"):
pass
elif sys.platform in ("win32",):
import msvcrt
_keyread_impl = _KEYREAD_IMPL_GETCH
_keyread_impl = KeyReadImpl.GETCH
else:
import select
import termios
import tty
_keyread_impl = _KEYREAD_IMPL_SELECT
_keyread_impl = KeyReadImpl.SELECT


class ConsoleSingleKeyReader(object):
class ConsoleSingleKeyReader:
"""
Get a single key press from a terminal without a prompt.
Expand All @@ -314,21 +317,22 @@ class ConsoleSingleKeyReader(object):
method. Call sites remain simple and straight forward.
"""

def __init__(self):
pass

def __enter__(self):
if _keyread_impl == _KEYREAD_IMPL_SELECT:
if _keyread_impl == KeyReadImpl.SELECT:
import termios
import tty
self.prev_settings = termios.tcgetattr(sys.stdin)
tty.setcbreak(sys.stdin.fileno())
return self

def __exit__(self, type, value, traceback):
if _keyread_impl == _KEYREAD_IMPL_SELECT:
if _keyread_impl == KeyReadImpl.SELECT:
import termios
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, self.prev_settings)

def get_single_key(self):
if _keyread_impl == _KEYREAD_IMPL_GETCH:
if _keyread_impl == KeyReadImpl.GETCH:
import msvcrt
# https://docs.python.org/3/library/msvcrt.html#msvcrt.getch
# Most keys are returned in the first getch() call. Some
# special keys (function keys, cursor, keypad) require
Expand All @@ -338,7 +342,8 @@ def get_single_key(self):
c = msvcrt.getch()
return c

elif _keyread_impl == _KEYREAD_IMPL_SELECT:
elif _keyread_impl == KeyReadImpl.SELECT:
import select
rset, _, _ = select.select([sys.stdin], [], [], None)
assert sys.stdin in rset
return sys.stdin.read(1)
Expand Down

0 comments on commit 99a3328

Please sign in to comment.