gvwilson / sdxpy

Software Design by Example: a tool-based introduction with Python
https://third-bit.com/sdxpy/
Other
280 stars 49 forks source link

/viewer – Fix `Window` size bug #269

Closed DTaskiran closed 2 months ago

DTaskiran commented 5 months ago

The Window classes defined in both src/viewer/size_window.py and src/viewer/cursor_const.py accept user input to determine the size of the drawable area. If no input is given curses.LINES and curses.COLS are used instead.

If the user unwittingly provides a size input which is larger than the available area and tries to draw into that area, the curses raises an error. Example (assuming 15 lines are actually available):

$ python3 cursor_const.py 16 log.txt 20 15
Traceback (most recent call last):
  File "/Users/diyartaskiran/*/SDXPY/src/viewer/cursor_const.py", line 36, in <module>
    curses.wrapper(lambda stdscr: main(stdscr, size, lines))
  File "/Users/diyartaskiran/anaconda3/envs/softwareconstruction/lib/python3.11/curses/__init__.py", line 94, in wrapper
    return func(stdscr, *args, **kwds)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/diyartaskiran/*/SDXPY/src/viewer/cursor_const.py", line 36, in <lambda>
    curses.wrapper(lambda stdscr: main(stdscr, size, lines))
                                  ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/diyartaskiran/*/SDXPY/src/viewer/cursor_const.py", line 28, in main
    window.draw(lines)
  File "/Users/diyartaskiran/*/SDXPY/src/viewer/cursor_const.py", line 23, in draw
    self._screen.addstr(y, 0, line[:self._size[COL]])
_curses.error: addwstr() returned ERR

In both classes, this is easily changed by accepting at most curses.LINES and curses.COLS as window size.

Note: this issue may also extend into the Window classes defined in src/undo, but I have not been able to look into that yet.

gvwilson commented 2 months ago

thank you for the fix