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