mirror of
https://github.com/rr-/pq-cli.git
synced 2026-11-12 22:40:36 -07:00
misc: drop typing as T, use builtin generics
Python 3.10 is the minimum, so annotations use `X | None`, `list[X]` and friends, enforced by ruff's pyupgrade rules. The remaining typing names are imported directly, with abstract types coming from collections.abc. first() is now generic over the item and default.
This commit is contained in:
40 files changed
+248
-236
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import curses
|
||||
import typing as T
|
||||
from typing import Any
|
||||
|
||||
|
||||
class Widget:
|
||||
@@ -12,10 +12,10 @@ class Widget:
|
||||
|
||||
class WindowWrapper(Widget):
|
||||
def __init__(
|
||||
self, parent: T.Optional[T.Any], h: int, w: int, y: int, x: int
|
||||
self, parent: Any | None, h: int, w: int, y: int, x: int
|
||||
) -> None:
|
||||
self._parent = parent
|
||||
self._win: T.Optional[T.Any]
|
||||
self._win: Any | None
|
||||
|
||||
if not parent:
|
||||
self._win = None
|
||||
@@ -30,12 +30,12 @@ class WindowWrapper(Widget):
|
||||
del self._win
|
||||
self._win = None
|
||||
|
||||
def getbegyx(self) -> T.Tuple[int, int]:
|
||||
def getbegyx(self) -> tuple[int, int]:
|
||||
if not self._win:
|
||||
return (0, 0)
|
||||
return self._win.getbegyx()
|
||||
|
||||
def getmaxyx(self) -> T.Tuple[int, int]:
|
||||
def getmaxyx(self) -> tuple[int, int]:
|
||||
if not self._win:
|
||||
return (0, 0)
|
||||
return self._win.getmaxyx()
|
||||
@@ -1,5 +1,5 @@
|
||||
import curses
|
||||
import typing as T
|
||||
from typing import Any
|
||||
|
||||
from pqcli.ui.curses.colors import COLOR_HIGHLIGHT, has_colors
|
||||
|
||||
@@ -9,7 +9,7 @@ from .scrollable import Scrollable
|
||||
class DataTable(Scrollable):
|
||||
def __init__(
|
||||
self,
|
||||
parent: T.Any,
|
||||
parent: Any,
|
||||
h: int,
|
||||
w: int,
|
||||
y: int,
|
||||
@@ -19,8 +19,8 @@ class DataTable(Scrollable):
|
||||
super().__init__(parent, h, w, y, x)
|
||||
|
||||
self._align_right = align_right
|
||||
self._items: T.List[T.Tuple[str, str]]
|
||||
self._selected: T.Optional[int] = None
|
||||
self._items: list[tuple[str, str]]
|
||||
self._selected: int | None = None
|
||||
|
||||
def add(self, text: str, value: str) -> None:
|
||||
self._items.append((text, value))
|
||||
@@ -40,10 +40,10 @@ class DataTable(Scrollable):
|
||||
del self._items[idx]
|
||||
self.scroll_to_item(idx)
|
||||
|
||||
def select(self, text: T.Optional[str]) -> None:
|
||||
def select(self, text: str | None) -> None:
|
||||
self._selected = None if text is None else self.get_idx(text)
|
||||
|
||||
def get_idx(self, text: str) -> T.Optional[int]:
|
||||
def get_idx(self, text: str) -> int | None:
|
||||
for idx, row in enumerate(self._items):
|
||||
if row[0].strip() == text.strip():
|
||||
return idx
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
import contextlib
|
||||
import curses
|
||||
import typing as T
|
||||
from collections.abc import Generator
|
||||
from typing import Any
|
||||
|
||||
from pqcli.ui.curses.colors import COLOR_FOCUSED, has_colors
|
||||
from pqcli.ui.curses.event_handler import EventHandler
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def focus_standout(focused: bool, win: T.Any) -> T.Generator:
|
||||
def focus_standout(focused: bool, win: Any) -> Generator:
|
||||
if focused:
|
||||
if has_colors():
|
||||
win.attron(curses.color_pair(COLOR_FOCUSED))
|
||||
@@ -22,7 +23,7 @@ def focus_standout(focused: bool, win: T.Any) -> T.Generator:
|
||||
|
||||
|
||||
class Focusable:
|
||||
def __init__(self, *args: T.Any, **kwargs: T.Any) -> None:
|
||||
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
||||
self._focused = False
|
||||
self._on_focus_change = EventHandler()
|
||||
super().__init__(*args, **kwargs)
|
||||
@@ -37,6 +38,6 @@ class Focusable:
|
||||
self._on_focus_change()
|
||||
|
||||
@contextlib.contextmanager
|
||||
def focus_standout(self, win: T.Any) -> T.Generator:
|
||||
def focus_standout(self, win: Any) -> Generator:
|
||||
with focus_standout(self._focused, win):
|
||||
yield
|
||||
@@ -1,5 +1,5 @@
|
||||
import curses
|
||||
import typing as T
|
||||
from typing import Any
|
||||
|
||||
from pqcli.ui.curses.colors import COLOR_HIGHLIGHT, has_colors
|
||||
|
||||
@@ -7,11 +7,11 @@ from .scrollable import Scrollable
|
||||
|
||||
|
||||
class ListBox(Scrollable):
|
||||
_items: T.List[str]
|
||||
_items: list[str]
|
||||
|
||||
def __init__(self, parent: T.Any, h: int, w: int, y: int, x: int) -> None:
|
||||
def __init__(self, parent: Any, h: int, w: int, y: int, x: int) -> None:
|
||||
super().__init__(parent, h, w, y, x)
|
||||
self._selected: T.Optional[int] = None
|
||||
self._selected: int | None = None
|
||||
|
||||
def add(self, text: str) -> None:
|
||||
self._items.append(text)
|
||||
@@ -22,7 +22,7 @@ class ListBox(Scrollable):
|
||||
idx %= len(self._items)
|
||||
self._items[idx] = value
|
||||
|
||||
def get(self, idx: int) -> T.Optional[str]:
|
||||
def get(self, idx: int) -> str | None:
|
||||
if not self._items:
|
||||
return None
|
||||
if idx < 0:
|
||||
@@ -35,7 +35,7 @@ class ListBox(Scrollable):
|
||||
def delete(self, idx: int, count: int = 1) -> None:
|
||||
del self._items[idx : idx + count]
|
||||
|
||||
def select(self, idx: T.Optional[int]) -> None:
|
||||
def select(self, idx: int | None) -> None:
|
||||
if idx < 0 and len(self._items):
|
||||
idx %= len(self._items)
|
||||
self._selected = idx
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import curses
|
||||
import curses.ascii
|
||||
import typing as T
|
||||
from typing import Any
|
||||
|
||||
from pqcli.ui.curses.util import KEYS_CYCLE, KEYS_DOWN, KEYS_UP, Choice
|
||||
from pqcli.ui.curses.widgets.focusable import focus_standout
|
||||
@@ -12,7 +12,7 @@ class Menu(Widget):
|
||||
def __init__(
|
||||
self,
|
||||
header: str,
|
||||
choices: T.List[Choice],
|
||||
choices: list[Choice],
|
||||
active_choice: int,
|
||||
scr_height: int,
|
||||
scr_width: int,
|
||||
@@ -28,7 +28,7 @@ class Menu(Widget):
|
||||
|
||||
w = max(map(len, all_lines)) + 1
|
||||
h = len(all_lines) + 1
|
||||
self._pad: T.Optional[T.Any] = curses.newpad(h, w)
|
||||
self._pad: Any | None = curses.newpad(h, w)
|
||||
|
||||
for y, line in enumerate(self._header_lines):
|
||||
self._pad.move(y, 0)
|
||||
@@ -40,7 +40,7 @@ class Menu(Widget):
|
||||
del self._pad
|
||||
self._pad = None
|
||||
|
||||
def getmaxyx(self) -> T.Tuple[int, int]:
|
||||
def getmaxyx(self) -> tuple[int, int]:
|
||||
if not self._pad:
|
||||
return (0, 0)
|
||||
return self._pad.getmaxyx()
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import curses
|
||||
import datetime
|
||||
import typing as T
|
||||
from typing import Any
|
||||
|
||||
from pqcli.lingo import format_timespan
|
||||
from pqcli.ui.curses.colors import COLOR_PROGRESSBAR, has_colors
|
||||
@@ -9,7 +9,7 @@ from pqcli.ui.curses.widgets.base import WindowWrapper
|
||||
|
||||
class ProgressBar(WindowWrapper):
|
||||
def __init__(
|
||||
self, parent: T.Any, h: int, w: int, y: int, x: int, show_time: bool
|
||||
self, parent: Any, h: int, w: int, y: int, x: int, show_time: bool
|
||||
) -> None:
|
||||
super().__init__(parent, h, w, y, x)
|
||||
|
||||
@@ -17,10 +17,10 @@ class ProgressBar(WindowWrapper):
|
||||
self._max_pos = 1.0
|
||||
|
||||
self._show_time = show_time
|
||||
self._last_tick: T.Optional[T.Tuple[datetime.datetime, float]] = None
|
||||
self._last_tick: tuple[datetime.datetime, float] | None = None
|
||||
|
||||
@property
|
||||
def time_left(self) -> T.Optional[datetime.timedelta]:
|
||||
def time_left(self) -> datetime.timedelta | None:
|
||||
if self._last_tick is None:
|
||||
return None
|
||||
time_then, pos_then = self._last_tick
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import curses
|
||||
import typing as T
|
||||
from typing import Any
|
||||
|
||||
from pqcli.ui.curses.colors import (
|
||||
COLOR_SCROLLBAR_THUMB,
|
||||
@@ -11,11 +11,11 @@ from .base import WindowWrapper
|
||||
|
||||
|
||||
class Scrollable(WindowWrapper):
|
||||
def __init__(self, parent: T.Any, h: int, w: int, y: int, x: int) -> None:
|
||||
def __init__(self, parent: Any, h: int, w: int, y: int, x: int) -> None:
|
||||
super().__init__(parent, h, w, y, x)
|
||||
self._scroll_pos = 0
|
||||
self._pad: T.Optional[T.Any] = curses.newpad(1, 1)
|
||||
self._items: T.List[T.Any] = []
|
||||
self._pad: Any | None = curses.newpad(1, 1)
|
||||
self._items: list[Any] = []
|
||||
|
||||
def clear(self) -> None:
|
||||
self._items = []
|
||||
|
||||
Reference in new issue
Block a user