Skip to content

Commit b47b918

Browse files
Ben-Regsebastinas
authored andcommitted
Progress
1 parent f42d1be commit b47b918

File tree

1 file changed

+75
-49
lines changed

1 file changed

+75
-49
lines changed

bpython/cli.py

Lines changed: 75 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -335,15 +335,22 @@ def file_prompt(self, s: str) -> str:
335335

336336

337337
class CLIRepl(repl.Repl):
338-
def __init__(self, scr: curses.window, interp: repl.Interpreter, statusbar: 'Statusbar', config: Config, idle: None = None):
338+
def __init__(
339+
self,
340+
scr: curses.window,
341+
interp: repl.Interpreter,
342+
statusbar: 'Statusbar',
343+
config: Config,
344+
idle: Optional[Callable] = None
345+
):
339346
super().__init__(interp, config)
340347
self.interp.writetb = self.writetb
341348
self.scr = scr
342349
self.stdout_hist = "" # native str (bytes in Py2, unicode in Py3)
343350
self.list_win = newwin(get_colpair(config, "background"), 1, 1, 1, 1)
344351
self.cpos = 0
345352
self.do_exit = False
346-
self.exit_value = ()
353+
self.exit_value: Tuple[Any, ...] = ()
347354
self.f_string = ""
348355
self.idle = idle
349356
self.in_hist = False
@@ -1138,7 +1145,7 @@ def redraw(self) -> None:
11381145
self.scr.refresh()
11391146
self.statusbar.refresh()
11401147

1141-
def repl(self) -> Tuple:
1148+
def repl(self) -> Tuple[Any, ...]:
11421149
"""Initialise the repl and jump into the loop. This method also has to
11431150
keep a stack of lines entered for the horrible "undo" feature. It also
11441151
tracks everything that would normally go to stdout in the normal Python
@@ -1277,8 +1284,13 @@ def write(self, s: str) -> None:
12771284
self.screen_hist.append(s.rstrip())
12781285

12791286
def show_list(
1280-
self, items, arg_pos, topline=None, formatter=None, current_item=None
1281-
):
1287+
self,
1288+
items: List[str],
1289+
arg_pos: Union[str, int],
1290+
topline: Any = None, # Named tuples don't play nice with mypy
1291+
formatter: Optional[Callable] = None,
1292+
current_item: Optional[bool] = None
1293+
) -> None:
12821294
shared = ShowListState()
12831295
y, x = self.scr.getyx()
12841296
h, w = self.scr.getmaxyx()
@@ -1290,7 +1302,7 @@ def show_list(
12901302
max_w = int(w * self.config.cli_suggestion_width)
12911303
self.list_win.erase()
12921304

1293-
if items:
1305+
if items and formatter:
12941306
items = [formatter(x) for x in items]
12951307
if current_item:
12961308
current_item = formatter(current_item)
@@ -1300,7 +1312,7 @@ def show_list(
13001312
else:
13011313
height_offset = 0
13021314

1303-
def lsize():
1315+
def lsize() -> bool:
13041316
wl = max(len(i) for i in v_items) + 1
13051317
if not wl:
13061318
wl = 1
@@ -1410,17 +1422,18 @@ def lsize():
14101422
self.scr.move(*self.scr.getyx())
14111423
self.list_win.refresh()
14121424

1413-
def size(self):
1425+
def size(self) -> None:
14141426
"""Set instance attributes for x and y top left corner coordinates
14151427
and width and height for the window."""
14161428
global stdscr
1417-
h, w = stdscr.getmaxyx()
1418-
self.y = 0
1419-
self.w = w
1420-
self.h = h - 1
1421-
self.x = 0
1422-
1423-
def suspend(self):
1429+
if stdscr:
1430+
h, w = stdscr.getmaxyx()
1431+
self.y: int = 0
1432+
self.w: int = w
1433+
self.h: int = h - 1
1434+
self.x: int = 0
1435+
1436+
def suspend(self) -> None:
14241437
"""Suspend the current process for shell job control."""
14251438
if platform.system() != "Windows":
14261439
curses.endwin()
@@ -1489,19 +1502,19 @@ def tab(self, back: bool = False) -> bool:
14891502
self.print_line(self.s, True)
14901503
return True
14911504

1492-
def undo(self, n=1):
1505+
def undo(self, n: int = 1) -> None:
14931506
repl.Repl.undo(self, n)
14941507

14951508
# This will unhighlight highlighted parens
14961509
self.print_line(self.s)
14971510

1498-
def writetb(self, lines):
1511+
def writetb(self, lines: List[str]) -> None:
14991512
for line in lines:
15001513
self.write(
15011514
"\x01{}\x03{}".format(self.config.color_scheme["error"], line)
15021515
)
15031516

1504-
def yank_from_buffer(self):
1517+
def yank_from_buffer(self) -> None:
15051518
"""Paste the text from the cut buffer at the current cursor location"""
15061519
self.addstr(self.cut_buffer)
15071520
self.print_line(self.s, clr=True)
@@ -1569,7 +1582,15 @@ class Statusbar:
15691582
15701583
"""
15711584

1572-
def __init__(self, scr, pwin, background, config, s=None, c=None):
1585+
def __init__(
1586+
self,
1587+
scr: curses.window,
1588+
pwin: curses.window,
1589+
background: int,
1590+
config: Config,
1591+
s: Optional[str] = None,
1592+
c: Optional[int] = None
1593+
):
15731594
"""Initialise the statusbar and display the initial text (if any)"""
15741595
self.size()
15751596
self.win = newwin(background, self.h, self.w, self.y, self.x)
@@ -1581,9 +1602,10 @@ def __init__(self, scr, pwin, background, config, s=None, c=None):
15811602
self.c = c
15821603
self.timer = 0
15831604
self.pwin = pwin
1584-
self.settext(s, c)
1605+
if s:
1606+
self.settext(s, c)
15851607

1586-
def size(self):
1608+
def size(self) -> None:
15871609
"""Set instance attributes for x and y top left corner coordinates
15881610
and width and height for the window."""
15891611
h, w = gethw()
@@ -1592,7 +1614,7 @@ def size(self):
15921614
self.h = 1
15931615
self.x = 0
15941616

1595-
def resize(self, refresh=True):
1617+
def resize(self, refresh: bool = True) -> None:
15961618
"""This method exists simply to keep it straight forward when
15971619
initialising a window and resizing it."""
15981620
self.size()
@@ -1601,12 +1623,12 @@ def resize(self, refresh=True):
16011623
if refresh:
16021624
self.refresh()
16031625

1604-
def refresh(self):
1626+
def refresh(self) -> None:
16051627
"""This is here to make sure the status bar text is redraw properly
16061628
after a resize."""
16071629
self.settext(self._s)
16081630

1609-
def check(self):
1631+
def check(self) -> None:
16101632
"""This is the method that should be called every half second or so
16111633
to see if the status bar needs updating."""
16121634
if not self.timer:
@@ -1617,21 +1639,21 @@ def check(self):
16171639

16181640
self.settext(self._s)
16191641

1620-
def message(self, s, n=3):
1642+
def message(self, s: str, n: int = 3) -> None:
16211643
"""Display a message for a short n seconds on the statusbar and return
16221644
it to its original state."""
1623-
self.timer = time.time() + n
1645+
self.timer = int(time.time() + n)
16241646
self.settext(s)
16251647

1626-
def prompt(self, s=""):
1648+
def prompt(self, s: str = "") -> str:
16271649
"""Prompt the user for some input (with the optional prompt 's') and
16281650
return the input text, then restore the statusbar to its original
16291651
value."""
16301652

16311653
self.settext(s or "? ", p=True)
16321654
iy, ix = self.win.getyx()
16331655

1634-
def bs(s):
1656+
def bs(s: str) -> str:
16351657
y, x = self.win.getyx()
16361658
if x == ix:
16371659
return s
@@ -1656,14 +1678,14 @@ def bs(s):
16561678
raise ValueError
16571679
# literal
16581680
elif 0 < c < 127:
1659-
c = chr(c)
1660-
self.win.addstr(c, get_colpair(self.config, "prompt"))
1661-
o += c
1681+
d = chr(c)
1682+
self.win.addstr(d, get_colpair(self.config, "prompt"))
1683+
o += d
16621684

16631685
self.settext(self._s)
16641686
return o
16651687

1666-
def settext(self, s, c=None, p=False):
1688+
def settext(self, s: str, c: Optional[int] = None, p: bool = False) -> None:
16671689
"""Set the text on the status bar to a new permanent value; this is the
16681690
value that will be set after a prompt or message. c is the optional
16691691
curses colour pair to use (if not specified the last specified colour
@@ -1690,12 +1712,12 @@ def settext(self, s, c=None, p=False):
16901712
else:
16911713
self.win.refresh()
16921714

1693-
def clear(self):
1715+
def clear(self) -> None:
16941716
"""Clear the status bar."""
16951717
self.win.clear()
16961718

16971719

1698-
def init_wins(scr, config):
1720+
def init_wins(scr: curses.window, config: Config) -> Tuple[curses.window, Statusbar]:
16991721
"""Initialise the two windows (the main repl interface and the little
17001722
status bar at the bottom with some stuff in it)"""
17011723
# TODO: Document better what stuff is on the status bar.
@@ -1705,7 +1727,9 @@ def init_wins(scr, config):
17051727

17061728
main_win = newwin(background, h - 1, w, 0, 0)
17071729
main_win.scrollok(True)
1708-
main_win.keypad(1)
1730+
1731+
# I think this is supposed to be True instead of 1?
1732+
main_win.keypad(1) # type:ignore[arg-type]
17091733
# Thanks to Angus Gibson for pointing out this missing line which was causing
17101734
# problems that needed dirty hackery to fix. :)
17111735

@@ -1728,18 +1752,18 @@ def init_wins(scr, config):
17281752
return main_win, statusbar
17291753

17301754

1731-
def sigwinch(unused_scr):
1755+
def sigwinch(unused_scr: curses.window) -> None:
17321756
global DO_RESIZE
17331757
DO_RESIZE = True
17341758

17351759

1736-
def sigcont(unused_scr):
1760+
def sigcont(unused_scr: curses.window) -> None:
17371761
sigwinch(unused_scr)
17381762
# Forces the redraw
17391763
curses.ungetch("\x00")
17401764

17411765

1742-
def gethw():
1766+
def gethw() -> Tuple[int, int]:
17431767
"""I found this code on a usenet post, and snipped out the bit I needed,
17441768
so thanks to whoever wrote that, sorry I forgot your name, I'm sure you're
17451769
a great guy.
@@ -1757,10 +1781,10 @@ def gethw():
17571781

17581782
if platform.system() != "Windows":
17591783
h, w = struct.unpack(
1760-
"hhhh", fcntl.ioctl(sys.__stdout__, termios.TIOCGWINSZ, "\000" * 8)
1784+
"hhhh", fcntl.ioctl(sys.__stdout__, termios.TIOCGWINSZ, "\000" * 8) # type:ignore[call-overload]
17611785
)[0:2]
17621786
else:
1763-
from ctypes import windll, create_string_buffer
1787+
from ctypes import windll, create_string_buffer # type:ignore[attr-defined]
17641788

17651789
# stdin handle is -10
17661790
# stdout handle is -11
@@ -1786,15 +1810,15 @@ def gethw():
17861810
) = struct.unpack("hhhhHhhhhhh", csbi.raw)
17871811
sizex = right - left + 1
17881812
sizey = bottom - top + 1
1789-
else:
1813+
elif stdscr:
17901814
# can't determine actual size - return default values
17911815
sizex, sizey = stdscr.getmaxyx()
17921816

17931817
h, w = sizey, sizex
17941818
return h, w
17951819

17961820

1797-
def idle(caller):
1821+
def idle(caller: CLIRepl) -> None:
17981822
"""This is called once every iteration through the getkey()
17991823
loop (currently in the Repl class, see the get_line() method).
18001824
The statusbar check needs to go here to take care of timed
@@ -1817,7 +1841,7 @@ def idle(caller):
18171841
do_resize(caller)
18181842

18191843

1820-
def do_resize(caller):
1844+
def do_resize(caller: CLIRepl) -> None:
18211845
"""This needs to hack around readline and curses not playing
18221846
nicely together. See also gethw() above."""
18231847
global DO_RESIZE
@@ -1844,30 +1868,31 @@ class FakeDict:
18441868
used as a hacky solution to using a colours dict containing colour codes if
18451869
colour initialisation fails."""
18461870

1847-
def __init__(self, val):
1871+
def __init__(self, val: int):
18481872
self._val = val
18491873

1850-
def __getitem__(self, k):
1874+
def __getitem__(self, k: Any) -> int:
18511875
return self._val
18521876

18531877

1854-
def newwin(background, *args):
1878+
def newwin(background: int, *args: int) -> curses.window:
18551879
"""Wrapper for curses.newwin to automatically set background colour on any
18561880
newly created window."""
18571881
win = curses.newwin(*args)
18581882
win.bkgd(" ", background)
18591883
return win
18601884

18611885

1862-
def curses_wrapper(func, *args, **kwargs):
1886+
def curses_wrapper(func: Callable, *args: Any, **kwargs: Any) -> Any:
18631887
"""Like curses.wrapper(), but reuses stdscr when called again."""
18641888
global stdscr
18651889
if stdscr is None:
18661890
stdscr = curses.initscr()
18671891
try:
18681892
curses.noecho()
18691893
curses.cbreak()
1870-
stdscr.keypad(1)
1894+
# Should this be keypad(True)?
1895+
stdscr.keypad(1) # type:ignore[arg-type]
18711896

18721897
try:
18731898
curses.start_color()
@@ -1876,7 +1901,8 @@ def curses_wrapper(func, *args, **kwargs):
18761901

18771902
return func(stdscr, *args, **kwargs)
18781903
finally:
1879-
stdscr.keypad(0)
1904+
# Should this be keypad(False)?
1905+
stdscr.keypad(0) # type:ignore[arg-type]
18801906
curses.echo()
18811907
curses.nocbreak()
18821908
curses.endwin()

0 commit comments

Comments
 (0)