-1

Suppose I am adding a large number of lines to a curses screen.

Minimal non-working example:

import curses


class MyApp(object):

    def __init__(self, stdscreen):
        self.screen = stdscreen

        for i in range(0,100):

            self.screen.addstr(str(i) + '\n')
            self.screen.refresh()

        self.screen.getch()

if __name__ == '__main__':
    curses.wrapper(MyApp)

The above code returns:

Traceback (most recent call last):
  File "test.py", line 17, in <module>
    curses.wrapper(MyApp)
  File "/usr/lib/python3.7/curses/__init__.py", line 94, in wrapper
    return func(stdscr, *args, **kwds)
  File "test.py", line 11, in __init__
    self.screen.addstr(str(i) + '\n')
_curses.error: addwstr() returned ERR
Press ENTER to continue

1) What is this error? 2) If the error is because I am adding too many lines to the screen, how could I list those entries with curses? Perhaps with a scroll view of some sort?

Justapigeon
  • 560
  • 1
  • 8
  • 22
  • 1
    Does this answer your question? [python curses addstr error - but only on my computer](https://stackoverflow.com/questions/5372809/python-curses-addstr-error-but-only-on-my-computer) – Thomas Dickey Jan 20 '20 at 17:57
  • @Thomas Dickey The logic of the accepted answer makes sense but no real solution for quickly checking the length of the window is provided, so I have posted a complete minimal working solution below. – Justapigeon Jan 21 '20 at 03:13

1 Answers1

0

It occurred to me that I could use try/except to determine the maximum number of lines that can be printed on the screen to avoid this error:

import curses


class MyApp(object):

    def __init__(self, stdscreen):
        self.screen = stdscreen

        maximum = self.maxlines()

        for i in range(maximum):

            self.screen.addstr(str(i) + '\n')
            self.screen.refresh()

        self.screen.getch()

    def maxlines(self):

        n = 0

        try:
            for i in range(100):
                self.screen.addstr(str(i) + '\n')
                n += 1

        except:
            pass

        self.screen.erase()

        return n


if __name__ == '__main__':
    curses.wrapper(MyApp)
Justapigeon
  • 560
  • 1
  • 8
  • 22
  • Did you find what this max n is? I am getting an error adding 19th line, where each line has approx 80 characters. – omsrisagar May 05 '22 at 21:51