Attempt to work out the number of lines on the screen. This is called by page(). It can raise an error (e.g. when run in the test suite), so it's separated out so it can easily be called in a try block.
(screen_lines_def)
| 79 | print(last_escape + os.linesep.join(screens[-1])) |
| 80 | |
| 81 | def _detect_screen_size(screen_lines_def): |
| 82 | """Attempt to work out the number of lines on the screen. |
| 83 | |
| 84 | This is called by page(). It can raise an error (e.g. when run in the |
| 85 | test suite), so it's separated out so it can easily be called in a try block. |
| 86 | """ |
| 87 | TERM = os.environ.get('TERM',None) |
| 88 | if not((TERM=='xterm' or TERM=='xterm-color') and sys.platform != 'sunos5'): |
| 89 | # curses causes problems on many terminals other than xterm, and |
| 90 | # some termios calls lock up on Sun OS5. |
| 91 | return screen_lines_def |
| 92 | |
| 93 | try: |
| 94 | import termios |
| 95 | import curses |
| 96 | except ImportError: |
| 97 | return screen_lines_def |
| 98 | |
| 99 | # There is a bug in curses, where *sometimes* it fails to properly |
| 100 | # initialize, and then after the endwin() call is made, the |
| 101 | # terminal is left in an unusable state. Rather than trying to |
| 102 | # check every time for this (by requesting and comparing termios |
| 103 | # flags each time), we just save the initial terminal state and |
| 104 | # unconditionally reset it every time. It's cheaper than making |
| 105 | # the checks. |
| 106 | try: |
| 107 | term_flags = termios.tcgetattr(sys.stdout) |
| 108 | except termios.error as err: |
| 109 | # can fail on Linux 2.6, pager_page will catch the TypeError |
| 110 | raise TypeError('termios error: {0}'.format(err)) from err |
| 111 | |
| 112 | try: |
| 113 | scr = curses.initscr() |
| 114 | except AttributeError: |
| 115 | # Curses on Solaris may not be complete, so we can't use it there |
| 116 | return screen_lines_def |
| 117 | |
| 118 | screen_lines_real,screen_cols = scr.getmaxyx() |
| 119 | curses.endwin() |
| 120 | |
| 121 | # Restore terminal state in case endwin() didn't. |
| 122 | termios.tcsetattr(sys.stdout,termios.TCSANOW,term_flags) |
| 123 | # Now we have what we needed: the screen size in rows/columns |
| 124 | return screen_lines_real |
| 125 | # print('***Screen size:',screen_lines_real,'lines x', |
| 126 | # screen_cols,'columns.') # dbg |
| 127 | |
| 128 | def pager_page(strng, start=0, screen_lines=0, pager_cmd=None) -> None: |
| 129 | """Display a string, piping through a pager after a certain length. |
no test coverage detected
searching dependent graphs…