Display a string, piping through a pager after a certain length. strng can be a mime-bundle dict, supplying multiple representations, keyed by mime-type. The screen_lines parameter specifies the number of *usable* lines of your terminal screen (total lines minus lines you need to r
(strng, start=0, screen_lines=0, pager_cmd=None)
| 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. |
| 130 | |
| 131 | strng can be a mime-bundle dict, supplying multiple representations, |
| 132 | keyed by mime-type. |
| 133 | |
| 134 | The screen_lines parameter specifies the number of *usable* lines of your |
| 135 | terminal screen (total lines minus lines you need to reserve to show other |
| 136 | information). |
| 137 | |
| 138 | If you set screen_lines to a number <=0, page() will try to auto-determine |
| 139 | your screen size and will only use up to (screen_size+screen_lines) for |
| 140 | printing, paging after that. That is, if you want auto-detection but need |
| 141 | to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for |
| 142 | auto-detection without any lines reserved simply use screen_lines = 0. |
| 143 | |
| 144 | If a string won't fit in the allowed lines, it is sent through the |
| 145 | specified pager command. If none given, look for PAGER in the environment, |
| 146 | and ultimately default to less. |
| 147 | |
| 148 | If no system pager works, the string is sent through a 'dumb pager' |
| 149 | written in python, very simplistic. |
| 150 | """ |
| 151 | |
| 152 | # for compatibility with mime-bundle form: |
| 153 | if isinstance(strng, dict): |
| 154 | strng = strng['text/plain'] |
| 155 | |
| 156 | # Ugly kludge, but calling curses.initscr() flat out crashes in emacs |
| 157 | TERM = os.environ.get('TERM','dumb') |
| 158 | if TERM in ['dumb','emacs'] and os.name != 'nt': |
| 159 | print(strng) |
| 160 | return |
| 161 | # chop off the topmost part of the string we don't want to see |
| 162 | str_lines = strng.splitlines()[start:] |
| 163 | str_toprint = os.linesep.join(str_lines) |
| 164 | num_newlines = len(str_lines) |
| 165 | len_str = len(str_toprint) |
| 166 | |
| 167 | # Dumb heuristics to guesstimate number of on-screen lines the string |
| 168 | # takes. Very basic, but good enough for docstrings in reasonable |
| 169 | # terminals. If someone later feels like refining it, it's not hard. |
| 170 | numlines = max(num_newlines,int(len_str/80)+1) |
| 171 | |
| 172 | screen_lines_def = get_terminal_size()[1] |
| 173 | |
| 174 | # auto-determine screen size |
| 175 | if screen_lines <= 0: |
| 176 | try: |
| 177 | screen_lines += _detect_screen_size(screen_lines_def) |
| 178 | except (TypeError, UnsupportedOperation): |
| 179 | print(str_toprint) |
| 180 | return |
| 181 | |
| 182 | # print('numlines',numlines,'screenlines',screen_lines) # dbg |
| 183 | if numlines <= screen_lines : |
| 184 | # print('*** normal print') # dbg |
| 185 | print(str_toprint) |
no test coverage detected
searching dependent graphs…