Show More
@@ -72,7 +72,7 b' from IPython.utils.strdispatch import StrDispatch' | |||
|
72 | 72 | from IPython.utils.syspathcontext import prepended_to_syspath |
|
73 | 73 | from IPython.utils.text import (format_screen, LSString, SList, |
|
74 | 74 | DollarFormatter) |
|
75 | from IPython.utils.traitlets import (Integer, CBool, CaselessStrEnum, Enum, | |
|
75 | from IPython.utils.traitlets import (Integer, Bool, CBool, CaselessStrEnum, Enum, | |
|
76 | 76 | List, Unicode, Instance, Type) |
|
77 | 77 | from IPython.utils.warn import warn, error |
|
78 | 78 | import IPython.core.hooks |
@@ -334,6 +334,10 b' class InteractiveShell(SingletonConfigurable):' | |||
|
334 | 334 | multiline_history = CBool(sys.platform != 'win32', config=True, |
|
335 | 335 | help="Save multi-line entries as one entry in readline history" |
|
336 | 336 | ) |
|
337 | display_page = Bool(False, config=True, | |
|
338 | help="""If True, anything that would be passed to the pager | |
|
339 | will be displayed as regular output instead.""" | |
|
340 | ) | |
|
337 | 341 | |
|
338 | 342 | # deprecated prompt traits: |
|
339 | 343 | |
@@ -820,7 +824,10 b' class InteractiveShell(SingletonConfigurable):' | |||
|
820 | 824 | # default hooks have priority 100, i.e. low; user hooks should have |
|
821 | 825 | # 0-100 priority |
|
822 | 826 | self.set_hook(hook_name,getattr(hooks,hook_name), 100, _warn_deprecated=False) |
|
823 | ||
|
827 | ||
|
828 | if self.display_page: | |
|
829 | self.set_hook('show_in_pager', page.as_hook(page.display_page), 90) | |
|
830 | ||
|
824 | 831 | def set_hook(self,name,hook, priority=50, str_key=None, re_key=None, |
|
825 | 832 | _warn_deprecated=True): |
|
826 | 833 | """set_hook(name,hook) -> sets an internal IPython hook. |
@@ -2,29 +2,17 b'' | |||
|
2 | 2 | """ |
|
3 | 3 | Paging capabilities for IPython.core |
|
4 | 4 | |
|
5 | Authors: | |
|
6 | ||
|
7 | * Brian Granger | |
|
8 | * Fernando Perez | |
|
9 | ||
|
10 | 5 | Notes |
|
11 | 6 | ----- |
|
12 | 7 | |
|
13 |
For now this uses |
|
|
8 | For now this uses IPython hooks, so it can't be in IPython.utils. If we can get | |
|
14 | 9 | rid of that dependency, we could move it there. |
|
15 | 10 | ----- |
|
16 | 11 | """ |
|
17 | 12 | |
|
18 | #----------------------------------------------------------------------------- | |
|
19 | # Copyright (C) 2008-2011 The IPython Development Team | |
|
20 | # | |
|
21 | # Distributed under the terms of the BSD License. The full license is in | |
|
22 | # the file COPYING, distributed as part of this software. | |
|
23 | #----------------------------------------------------------------------------- | |
|
13 | # Copyright (c) IPython Development Team. | |
|
14 | # Distributed under the terms of the Modified BSD License. | |
|
24 | 15 | |
|
25 | #----------------------------------------------------------------------------- | |
|
26 | # Imports | |
|
27 | #----------------------------------------------------------------------------- | |
|
28 | 16 | from __future__ import print_function |
|
29 | 17 | |
|
30 | 18 | import os |
@@ -35,6 +23,7 b' import tempfile' | |||
|
35 | 23 | from io import UnsupportedOperation |
|
36 | 24 | |
|
37 | 25 | from IPython import get_ipython |
|
26 | from IPython.core.display import display | |
|
38 | 27 | from IPython.core.error import TryNext |
|
39 | 28 | from IPython.utils.data import chop |
|
40 | 29 | from IPython.utils import io |
@@ -43,9 +32,24 b' from IPython.utils.terminal import get_terminal_size' | |||
|
43 | 32 | from IPython.utils import py3compat |
|
44 | 33 | |
|
45 | 34 | |
|
46 | #----------------------------------------------------------------------------- | |
|
47 | # Classes and functions | |
|
48 | #----------------------------------------------------------------------------- | |
|
35 | def display_page(strng, start=0, screen_lines=25): | |
|
36 | """Just display, no paging. screen_lines is ignored.""" | |
|
37 | if isinstance(strng, dict): | |
|
38 | data = strng | |
|
39 | else: | |
|
40 | if start: | |
|
41 | strng = u'\n'.join(strng.splitlines()[start:]) | |
|
42 | data = {'text/plain': strng} | |
|
43 | display(data, raw=True) | |
|
44 | ||
|
45 | ||
|
46 | def as_hook(page_func): | |
|
47 | """Wrap a pager func to strip the `self` arg | |
|
48 | ||
|
49 | so it can be called as a hook. | |
|
50 | """ | |
|
51 | return lambda self, *args, **kwargs: page_func(*args, **kwargs) | |
|
52 | ||
|
49 | 53 | |
|
50 | 54 | esc_re = re.compile(r"(\x1b[^m]+m)") |
|
51 | 55 | |
@@ -132,7 +136,7 b' def _detect_screen_size(screen_lines_def):' | |||
|
132 | 136 | #print '***Screen size:',screen_lines_real,'lines x',\ |
|
133 | 137 | #screen_cols,'columns.' # dbg |
|
134 | 138 | |
|
135 | def page(strng, start=0, screen_lines=0, pager_cmd=None): | |
|
139 | def pager_page(strng, start=0, screen_lines=0, pager_cmd=None): | |
|
136 | 140 | """Display a string, piping through a pager after a certain length. |
|
137 | 141 | |
|
138 | 142 | strng can be a mime-bundle dict, supplying multiple representations, |
@@ -163,16 +167,7 b' def page(strng, start=0, screen_lines=0, pager_cmd=None):' | |||
|
163 | 167 | # Some routines may auto-compute start offsets incorrectly and pass a |
|
164 | 168 | # negative value. Offset to 0 for robustness. |
|
165 | 169 | start = max(0, start) |
|
166 | ||
|
167 | # first, try the hook | |
|
168 | ip = get_ipython() | |
|
169 | if ip: | |
|
170 | try: | |
|
171 | ip.hooks.show_in_pager(strng) | |
|
172 | return | |
|
173 | except TryNext: | |
|
174 | pass | |
|
175 | ||
|
170 | ||
|
176 | 171 | # Ugly kludge, but calling curses.initscr() flat out crashes in emacs |
|
177 | 172 | TERM = os.environ.get('TERM','dumb') |
|
178 | 173 | if TERM in ['dumb','emacs'] and os.name != 'nt': |
@@ -252,6 +247,32 b' def page(strng, start=0, screen_lines=0, pager_cmd=None):' | |||
|
252 | 247 | page_dumb(strng,screen_lines=screen_lines) |
|
253 | 248 | |
|
254 | 249 | |
|
250 | def page(data, start=0, screen_lines=0, pager_cmd=None): | |
|
251 | """Display content in a pager, piping through a pager after a certain length. | |
|
252 | ||
|
253 | data can be a mime-bundle dict, supplying multiple representations, | |
|
254 | keyed by mime-type, or text. | |
|
255 | ||
|
256 | Pager is dispatched via the `show_in_pager` IPython hook. | |
|
257 | If no hook is registered, `pager_page` will be used. | |
|
258 | """ | |
|
259 | # Some routines may auto-compute start offsets incorrectly and pass a | |
|
260 | # negative value. Offset to 0 for robustness. | |
|
261 | start = max(0, start) | |
|
262 | ||
|
263 | # first, try the hook | |
|
264 | ip = get_ipython() | |
|
265 | if ip: | |
|
266 | try: | |
|
267 | ip.hooks.show_in_pager(data, start=start, screen_lines=screen_lines) | |
|
268 | return | |
|
269 | except TryNext: | |
|
270 | pass | |
|
271 | ||
|
272 | # fallback on default pager | |
|
273 | return pager_page(data, start, screen_lines, pager_cmd) | |
|
274 | ||
|
275 | ||
|
255 | 276 | def page_file(fname, start=0, pager_cmd=None): |
|
256 | 277 | """Page a file, using an optional pager command and starting line. |
|
257 | 278 | """ |
@@ -4,11 +4,9 b'' | |||
|
4 | 4 | # Copyright (c) IPython Development Team. |
|
5 | 5 | # Distributed under the terms of the Modified BSD License. |
|
6 | 6 | |
|
7 | import warnings | |
|
7 | 8 | from IPython.core.getipython import get_ipython |
|
8 | 9 | |
|
9 | #----------------------------------------------------------------------------- | |
|
10 | # Classes and functions | |
|
11 | #----------------------------------------------------------------------------- | |
|
12 | 10 | |
|
13 | 11 | def page(strng, start=0, screen_lines=0, pager_cmd=None): |
|
14 | 12 | """Print a string, piping through a pager. |
@@ -43,6 +41,12 b' def page(strng, start=0, screen_lines=0, pager_cmd=None):' | |||
|
43 | 41 | |
|
44 | 42 | |
|
45 | 43 | def install_payload_page(): |
|
46 | """Install this version of page as IPython.core.page.page.""" | |
|
44 | """DEPRECATED, use show_in_pager hook | |
|
45 | ||
|
46 | Install this version of page as IPython.core.page.page. | |
|
47 | """ | |
|
48 | warnings.warn("""install_payload_page is deprecated. | |
|
49 | Use `ip.set_hook('show_in_pager, page.as_hook(payloadpage.page))` | |
|
50 | """) | |
|
47 | 51 | from IPython.core import page as corepage |
|
48 | 52 | corepage.page = page |
@@ -31,7 +31,7 b' from IPython.core.displaypub import DisplayPublisher' | |||
|
31 | 31 | from IPython.core.error import UsageError |
|
32 | 32 | from IPython.core.magics import MacroToEdit, CodeMagics |
|
33 | 33 | from IPython.core.magic import magics_class, line_magic, Magics |
|
34 |
from IPython.core |
|
|
34 | from IPython.core import payloadpage | |
|
35 | 35 | from IPython.core.usage import default_gui_banner |
|
36 | 36 | from IPython.display import display, Javascript |
|
37 | 37 | from IPython.kernel.inprocess.socket import SocketABC |
@@ -391,9 +391,7 b' class ZMQInteractiveShell(InteractiveShell):' | |||
|
391 | 391 | raise UsageError("%s" % e) |
|
392 | 392 | |
|
393 | 393 | def init_environment(self): |
|
394 | """Configure the user's environment. | |
|
395 | ||
|
396 | """ | |
|
394 | """Configure the user's environment.""" | |
|
397 | 395 | env = os.environ |
|
398 | 396 | # These two ensure 'ls' produces nice coloring on BSD-derived systems |
|
399 | 397 | env['TERM'] = 'xterm-color' |
@@ -403,10 +401,11 b' class ZMQInteractiveShell(InteractiveShell):' | |||
|
403 | 401 | # subprocesses as much as possible. |
|
404 | 402 | env['PAGER'] = 'cat' |
|
405 | 403 | env['GIT_PAGER'] = 'cat' |
|
406 |
|
|
|
407 | # And install the payload version of page. | |
|
408 | install_payload_page() | |
|
409 | ||
|
404 | ||
|
405 | def init_hooks(self): | |
|
406 | super(ZMQInteractiveShell, self).init_hooks() | |
|
407 | self.set_hook('show_in_pager', page.as_hook(payloadpage.page), 99) | |
|
408 | ||
|
410 | 409 | def ask_exit(self): |
|
411 | 410 | """Engage the exit actions.""" |
|
412 | 411 | self.exit_now = (not self.keepkernel_on_exit) |
General Comments 0
You need to be logged in to leave comments.
Login now