page.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. # encoding: utf-8
  2. """
  3. Paging capabilities for IPython.core
  4. Notes
  5. -----
  6. For now this uses IPython hooks, so it can't be in IPython.utils. If we can get
  7. rid of that dependency, we could move it there.
  8. -----
  9. """
  10. # Copyright (c) IPython Development Team.
  11. # Distributed under the terms of the Modified BSD License.
  12. import os
  13. import io
  14. import re
  15. import sys
  16. import tempfile
  17. import subprocess
  18. from io import UnsupportedOperation
  19. from pathlib import Path
  20. from IPython import get_ipython
  21. from IPython.display import display
  22. from IPython.core.error import TryNext
  23. from IPython.utils.data import chop
  24. from IPython.utils.process import system
  25. from IPython.utils.terminal import get_terminal_size
  26. from IPython.utils import py3compat
  27. def display_page(strng, start=0, screen_lines=25):
  28. """Just display, no paging. screen_lines is ignored."""
  29. if isinstance(strng, dict):
  30. data = strng
  31. else:
  32. if start:
  33. strng = u'\n'.join(strng.splitlines()[start:])
  34. data = { 'text/plain': strng }
  35. display(data, raw=True)
  36. def as_hook(page_func):
  37. """Wrap a pager func to strip the `self` arg
  38. so it can be called as a hook.
  39. """
  40. return lambda self, *args, **kwargs: page_func(*args, **kwargs)
  41. esc_re = re.compile(r"(\x1b[^m]+m)")
  42. def page_dumb(strng, start=0, screen_lines=25):
  43. """Very dumb 'pager' in Python, for when nothing else works.
  44. Only moves forward, same interface as page(), except for pager_cmd and
  45. mode.
  46. """
  47. if isinstance(strng, dict):
  48. strng = strng.get('text/plain', '')
  49. out_ln = strng.splitlines()[start:]
  50. screens = chop(out_ln,screen_lines-1)
  51. if len(screens) == 1:
  52. print(os.linesep.join(screens[0]))
  53. else:
  54. last_escape = ""
  55. for scr in screens[0:-1]:
  56. hunk = os.linesep.join(scr)
  57. print(last_escape + hunk)
  58. if not page_more():
  59. return
  60. esc_list = esc_re.findall(hunk)
  61. if len(esc_list) > 0:
  62. last_escape = esc_list[-1]
  63. print(last_escape + os.linesep.join(screens[-1]))
  64. def _detect_screen_size(screen_lines_def):
  65. """Attempt to work out the number of lines on the screen.
  66. This is called by page(). It can raise an error (e.g. when run in the
  67. test suite), so it's separated out so it can easily be called in a try block.
  68. """
  69. TERM = os.environ.get('TERM',None)
  70. if not((TERM=='xterm' or TERM=='xterm-color') and sys.platform != 'sunos5'):
  71. # curses causes problems on many terminals other than xterm, and
  72. # some termios calls lock up on Sun OS5.
  73. return screen_lines_def
  74. try:
  75. import termios
  76. import curses
  77. except ImportError:
  78. return screen_lines_def
  79. # There is a bug in curses, where *sometimes* it fails to properly
  80. # initialize, and then after the endwin() call is made, the
  81. # terminal is left in an unusable state. Rather than trying to
  82. # check every time for this (by requesting and comparing termios
  83. # flags each time), we just save the initial terminal state and
  84. # unconditionally reset it every time. It's cheaper than making
  85. # the checks.
  86. try:
  87. term_flags = termios.tcgetattr(sys.stdout)
  88. except termios.error as err:
  89. # can fail on Linux 2.6, pager_page will catch the TypeError
  90. raise TypeError('termios error: {0}'.format(err)) from err
  91. try:
  92. scr = curses.initscr()
  93. except AttributeError:
  94. # Curses on Solaris may not be complete, so we can't use it there
  95. return screen_lines_def
  96. screen_lines_real,screen_cols = scr.getmaxyx()
  97. curses.endwin()
  98. # Restore terminal state in case endwin() didn't.
  99. termios.tcsetattr(sys.stdout,termios.TCSANOW,term_flags)
  100. # Now we have what we needed: the screen size in rows/columns
  101. return screen_lines_real
  102. # print('***Screen size:',screen_lines_real,'lines x',
  103. # screen_cols,'columns.') # dbg
  104. def pager_page(strng, start=0, screen_lines=0, pager_cmd=None) -> None:
  105. """Display a string, piping through a pager after a certain length.
  106. strng can be a mime-bundle dict, supplying multiple representations,
  107. keyed by mime-type.
  108. The screen_lines parameter specifies the number of *usable* lines of your
  109. terminal screen (total lines minus lines you need to reserve to show other
  110. information).
  111. If you set screen_lines to a number <=0, page() will try to auto-determine
  112. your screen size and will only use up to (screen_size+screen_lines) for
  113. printing, paging after that. That is, if you want auto-detection but need
  114. to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for
  115. auto-detection without any lines reserved simply use screen_lines = 0.
  116. If a string won't fit in the allowed lines, it is sent through the
  117. specified pager command. If none given, look for PAGER in the environment,
  118. and ultimately default to less.
  119. If no system pager works, the string is sent through a 'dumb pager'
  120. written in python, very simplistic.
  121. """
  122. # for compatibility with mime-bundle form:
  123. if isinstance(strng, dict):
  124. strng = strng['text/plain']
  125. # Ugly kludge, but calling curses.initscr() flat out crashes in emacs
  126. TERM = os.environ.get('TERM','dumb')
  127. if TERM in ['dumb','emacs'] and os.name != 'nt':
  128. print(strng)
  129. return
  130. # chop off the topmost part of the string we don't want to see
  131. str_lines = strng.splitlines()[start:]
  132. str_toprint = os.linesep.join(str_lines)
  133. num_newlines = len(str_lines)
  134. len_str = len(str_toprint)
  135. # Dumb heuristics to guesstimate number of on-screen lines the string
  136. # takes. Very basic, but good enough for docstrings in reasonable
  137. # terminals. If someone later feels like refining it, it's not hard.
  138. numlines = max(num_newlines,int(len_str/80)+1)
  139. screen_lines_def = get_terminal_size()[1]
  140. # auto-determine screen size
  141. if screen_lines <= 0:
  142. try:
  143. screen_lines += _detect_screen_size(screen_lines_def)
  144. except (TypeError, UnsupportedOperation):
  145. print(str_toprint)
  146. return
  147. # print('numlines',numlines,'screenlines',screen_lines) # dbg
  148. if numlines <= screen_lines :
  149. # print('*** normal print') # dbg
  150. print(str_toprint)
  151. else:
  152. # Try to open pager and default to internal one if that fails.
  153. # All failure modes are tagged as 'retval=1', to match the return
  154. # value of a failed system command. If any intermediate attempt
  155. # sets retval to 1, at the end we resort to our own page_dumb() pager.
  156. pager_cmd = get_pager_cmd(pager_cmd)
  157. pager_cmd += ' ' + get_pager_start(pager_cmd,start)
  158. if os.name == 'nt':
  159. if pager_cmd.startswith('type'):
  160. # The default WinXP 'type' command is failing on complex strings.
  161. retval = 1
  162. else:
  163. fd, tmpname = tempfile.mkstemp('.txt')
  164. tmppath = Path(tmpname)
  165. try:
  166. os.close(fd)
  167. with tmppath.open("wt", encoding="utf-8") as tmpfile:
  168. tmpfile.write(strng)
  169. cmd = "%s < %s" % (pager_cmd, tmppath)
  170. # tmpfile needs to be closed for windows
  171. if os.system(cmd):
  172. retval = 1
  173. else:
  174. retval = None
  175. finally:
  176. Path.unlink(tmppath)
  177. else:
  178. try:
  179. retval = None
  180. # Emulate os.popen, but redirect stderr
  181. proc = subprocess.Popen(
  182. pager_cmd,
  183. shell=True,
  184. stdin=subprocess.PIPE,
  185. stderr=subprocess.DEVNULL,
  186. )
  187. pager = os._wrap_close(
  188. io.TextIOWrapper(proc.stdin, encoding="utf-8"), proc
  189. )
  190. try:
  191. pager_encoding = pager.encoding or sys.stdout.encoding
  192. pager.write(strng)
  193. finally:
  194. retval = pager.close()
  195. except IOError as msg: # broken pipe when user quits
  196. if msg.args == (32, 'Broken pipe'):
  197. retval = None
  198. else:
  199. retval = 1
  200. except OSError:
  201. # Other strange problems, sometimes seen in Win2k/cygwin
  202. retval = 1
  203. if retval is not None:
  204. page_dumb(strng,screen_lines=screen_lines)
  205. def page(data, start: int = 0, screen_lines: int = 0, pager_cmd=None):
  206. """Display content in a pager, piping through a pager after a certain length.
  207. data can be a mime-bundle dict, supplying multiple representations,
  208. keyed by mime-type, or text.
  209. Pager is dispatched via the `show_in_pager` IPython hook.
  210. If no hook is registered, `pager_page` will be used.
  211. """
  212. # Some routines may auto-compute start offsets incorrectly and pass a
  213. # negative value. Offset to 0 for robustness.
  214. start = max(0, start)
  215. # first, try the hook
  216. ip = get_ipython()
  217. if ip:
  218. try:
  219. ip.hooks.show_in_pager(data, start=start, screen_lines=screen_lines)
  220. return
  221. except TryNext:
  222. pass
  223. # fallback on default pager
  224. return pager_page(data, start, screen_lines, pager_cmd)
  225. def page_file(fname, start=0, pager_cmd=None):
  226. """Page a file, using an optional pager command and starting line.
  227. """
  228. pager_cmd = get_pager_cmd(pager_cmd)
  229. pager_cmd += ' ' + get_pager_start(pager_cmd,start)
  230. try:
  231. if os.environ['TERM'] in ['emacs','dumb']:
  232. raise EnvironmentError
  233. system(pager_cmd + ' ' + fname)
  234. except:
  235. try:
  236. if start > 0:
  237. start -= 1
  238. page(open(fname, encoding="utf-8").read(), start)
  239. except:
  240. print('Unable to show file',repr(fname))
  241. def get_pager_cmd(pager_cmd=None):
  242. """Return a pager command.
  243. Makes some attempts at finding an OS-correct one.
  244. """
  245. if os.name == 'posix':
  246. default_pager_cmd = 'less -R' # -R for color control sequences
  247. elif os.name in ['nt','dos']:
  248. default_pager_cmd = 'type'
  249. if pager_cmd is None:
  250. try:
  251. pager_cmd = os.environ['PAGER']
  252. except:
  253. pager_cmd = default_pager_cmd
  254. if pager_cmd == 'less' and '-r' not in os.environ.get('LESS', '').lower():
  255. pager_cmd += ' -R'
  256. return pager_cmd
  257. def get_pager_start(pager, start):
  258. """Return the string for paging files with an offset.
  259. This is the '+N' argument which less and more (under Unix) accept.
  260. """
  261. if pager in ['less','more']:
  262. if start:
  263. start_string = '+' + str(start)
  264. else:
  265. start_string = ''
  266. else:
  267. start_string = ''
  268. return start_string
  269. # (X)emacs on win32 doesn't like to be bypassed with msvcrt.getch()
  270. if os.name == 'nt' and os.environ.get('TERM','dumb') != 'emacs':
  271. import msvcrt
  272. def page_more():
  273. """ Smart pausing between pages
  274. @return: True if need print more lines, False if quit
  275. """
  276. sys.stdout.write('---Return to continue, q to quit--- ')
  277. ans = msvcrt.getwch()
  278. if ans in ("q", "Q"):
  279. result = False
  280. else:
  281. result = True
  282. sys.stdout.write("\b"*37 + " "*37 + "\b"*37)
  283. return result
  284. else:
  285. def page_more():
  286. ans = py3compat.input('---Return to continue, q to quit--- ')
  287. if ans.lower().startswith('q'):
  288. return False
  289. else:
  290. return True