Skip to content

Commit 2ae5c45

Browse files
Ben-Regsebastinas
authored andcommitted
Done with cli.py, working on repl.py
1 parent 136631d commit 2ae5c45

File tree

2 files changed

+70
-48
lines changed

2 files changed

+70
-48
lines changed

bpython/cli.py

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@
6666
Optional,
6767
Union,
6868
Tuple,
69+
Collection,
70+
Dict
6971
)
7072
import unicodedata
7173
from dataclasses import dataclass
@@ -287,7 +289,7 @@ def get_colpair(config: Config, name: str) -> int:
287289
return curses.color_pair(get_color(config, name) + 1)
288290

289291

290-
def make_colors(config: Config) -> MutableMapping[str, int]:
292+
def make_colors(config: Config) -> Dict[str, int]:
291293
"""Init all the colours in curses and bang them into a dictionary"""
292294

293295
# blacK, Red, Green, Yellow, Blue, Magenta, Cyan, White, Default:
@@ -366,8 +368,10 @@ def __init__(
366368
idle: Optional[Callable] = None,
367369
):
368370
super().__init__(interp, config)
369-
self.interp.writetb = self.writetb
370-
self.scr = scr
371+
# mypy doesn't quite understand the difference between a class variable with a callable type and a method.
372+
# https://github.com/python/mypy/issues/2427
373+
self.interp.writetb = self.writetb # type:ignore[assignment]
374+
self.scr: curses.window = scr
371375
self.stdout_hist = "" # native str (bytes in Py2, unicode in Py3)
372376
self.list_win = newwin(get_colpair(config, "background"), 1, 1, 1, 1)
373377
self.cpos = 0
@@ -382,6 +386,10 @@ def __init__(
382386
self.statusbar = statusbar
383387
self.formatter = BPythonFormatter(config.color_scheme)
384388
self.interact = CLIInteraction(self.config, statusbar=self.statusbar)
389+
self.ix: int
390+
self.iy: int
391+
self.arg_pos: Union[str, int, None]
392+
self.prev_block_finished: int
385393

386394
if config.cli_suggestion_width <= 0 or config.cli_suggestion_width > 1:
387395
config.cli_suggestion_width = 0.8
@@ -505,13 +513,18 @@ def complete(self, tab: bool = False) -> None:
505513
return
506514

507515
list_win_visible = repl.Repl.complete(self, tab)
516+
517+
f = None
518+
if self.matches_iter.completer:
519+
f = self.matches_iter.completer.format
520+
508521
if list_win_visible:
509522
try:
510523
self.show_list(
511524
self.matches_iter.matches,
512525
self.arg_pos,
513526
topline=self.funcprops,
514-
formatter=self.matches_iter.completer.format,
527+
formatter=f,
515528
)
516529
except curses.error:
517530
# XXX: This is a massive hack, it will go away when I get
@@ -745,7 +758,7 @@ def lf(self) -> None:
745758
def mkargspec(
746759
self,
747760
topline: Any, # Named tuples don't seem to play nice with mypy
748-
in_arg: Union[str, int],
761+
in_arg: Union[str, int, None],
749762
down: bool,
750763
) -> int:
751764
"""This figures out what to do with the argspec and puts it nicely into
@@ -1310,11 +1323,12 @@ def write(self, s: str) -> None:
13101323
def show_list(
13111324
self,
13121325
items: List[str],
1313-
arg_pos: Union[str, int],
1326+
arg_pos: Union[str, int, None],
13141327
topline: Any = None, # Named tuples don't play nice with mypy
13151328
formatter: Optional[Callable] = None,
1316-
current_item: Optional[bool] = None,
1329+
current_item: Optional[str] = None,
13171330
) -> None:
1331+
v_items: Collection
13181332
shared = ShowListState()
13191333
y, x = self.scr.getyx()
13201334
h, w = self.scr.getmaxyx()
@@ -1475,6 +1489,10 @@ def tab(self, back: bool = False) -> bool:
14751489
and don't indent if there are only whitespace in the line.
14761490
"""
14771491

1492+
f = None
1493+
if self.matches_iter.completer:
1494+
f = self.matches_iter.completer.format
1495+
14781496
# 1. check if we should add a tab character
14791497
if self.atbol() and not back:
14801498
x_pos = len(self.s) - self.cpos
@@ -1505,15 +1523,16 @@ def tab(self, back: bool = False) -> bool:
15051523

15061524
# 4. swap current word for a match list item
15071525
elif self.matches_iter.matches:
1508-
current_match = (
1509-
back and self.matches_iter.previous() or next(self.matches_iter)
1526+
n: str = next(self.matches_iter)
1527+
current_match: Optional[str] = (
1528+
back and self.matches_iter.previous() or n
15101529
)
15111530
try:
15121531
self.show_list(
15131532
self.matches_iter.matches,
15141533
self.arg_pos,
15151534
topline=self.funcprops,
1516-
formatter=self.matches_iter.completer.format,
1535+
formatter=f,
15171536
current_item=current_match,
15181537
)
15191538
except curses.error:
@@ -1815,10 +1834,11 @@ def gethw() -> Tuple[int, int]:
18151834
), # type:ignore[call-overload]
18161835
)[0:2]
18171836
else:
1818-
from ctypes import (
1837+
# Ignoring mypy's windll error because it's Windows-specific
1838+
from ctypes import ( # type:ignore[attr-defined]
18191839
windll,
18201840
create_string_buffer,
1821-
) # type:ignore[attr-defined]
1841+
)
18221842

18231843
# stdin handle is -10
18241844
# stdout handle is -11

0 commit comments

Comments
 (0)