##// END OF EJS Templates
add InteractiveShell.display_page config...
Min RK -
Show More
@@ -72,7 +72,7 b' from IPython.utils.strdispatch import StrDispatch'
72 from IPython.utils.syspathcontext import prepended_to_syspath
72 from IPython.utils.syspathcontext import prepended_to_syspath
73 from IPython.utils.text import (format_screen, LSString, SList,
73 from IPython.utils.text import (format_screen, LSString, SList,
74 DollarFormatter)
74 DollarFormatter)
75 from IPython.utils.traitlets import (Integer, CBool, CaselessStrEnum, Enum,
75 from IPython.utils.traitlets import (Integer, Bool, CBool, CaselessStrEnum, Enum,
76 List, Unicode, Instance, Type)
76 List, Unicode, Instance, Type)
77 from IPython.utils.warn import warn, error
77 from IPython.utils.warn import warn, error
78 import IPython.core.hooks
78 import IPython.core.hooks
@@ -334,6 +334,10 b' class InteractiveShell(SingletonConfigurable):'
334 multiline_history = CBool(sys.platform != 'win32', config=True,
334 multiline_history = CBool(sys.platform != 'win32', config=True,
335 help="Save multi-line entries as one entry in readline history"
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 # deprecated prompt traits:
342 # deprecated prompt traits:
339
343
@@ -821,6 +825,9 b' class InteractiveShell(SingletonConfigurable):'
821 # 0-100 priority
825 # 0-100 priority
822 self.set_hook(hook_name,getattr(hooks,hook_name), 100, _warn_deprecated=False)
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 def set_hook(self,name,hook, priority=50, str_key=None, re_key=None,
831 def set_hook(self,name,hook, priority=50, str_key=None, re_key=None,
825 _warn_deprecated=True):
832 _warn_deprecated=True):
826 """set_hook(name,hook) -> sets an internal IPython hook.
833 """set_hook(name,hook) -> sets an internal IPython hook.
@@ -2,29 +2,17 b''
2 """
2 """
3 Paging capabilities for IPython.core
3 Paging capabilities for IPython.core
4
4
5 Authors:
6
7 * Brian Granger
8 * Fernando Perez
9
10 Notes
5 Notes
11 -----
6 -----
12
7
13 For now this uses ipapi, so it can't be in IPython.utils. If we can get
8 For now this uses IPython hooks, so it can't be in IPython.utils. If we can get
14 rid of that dependency, we could move it there.
9 rid of that dependency, we could move it there.
15 -----
10 -----
16 """
11 """
17
12
18 #-----------------------------------------------------------------------------
13 # Copyright (c) IPython Development Team.
19 # Copyright (C) 2008-2011 The IPython Development Team
14 # Distributed under the terms of the Modified BSD License.
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 #-----------------------------------------------------------------------------
24
15
25 #-----------------------------------------------------------------------------
26 # Imports
27 #-----------------------------------------------------------------------------
28 from __future__ import print_function
16 from __future__ import print_function
29
17
30 import os
18 import os
@@ -35,6 +23,7 b' import tempfile'
35 from io import UnsupportedOperation
23 from io import UnsupportedOperation
36
24
37 from IPython import get_ipython
25 from IPython import get_ipython
26 from IPython.core.display import display
38 from IPython.core.error import TryNext
27 from IPython.core.error import TryNext
39 from IPython.utils.data import chop
28 from IPython.utils.data import chop
40 from IPython.utils import io
29 from IPython.utils import io
@@ -43,9 +32,24 b' from IPython.utils.terminal import get_terminal_size'
43 from IPython.utils import py3compat
32 from IPython.utils import py3compat
44
33
45
34
46 #-----------------------------------------------------------------------------
35 def display_page(strng, start=0, screen_lines=25):
47 # Classes and functions
36 """Just display, no paging. screen_lines is ignored."""
48 #-----------------------------------------------------------------------------
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 esc_re = re.compile(r"(\x1b[^m]+m)")
54 esc_re = re.compile(r"(\x1b[^m]+m)")
51
55
@@ -132,7 +136,7 b' def _detect_screen_size(screen_lines_def):'
132 #print '***Screen size:',screen_lines_real,'lines x',\
136 #print '***Screen size:',screen_lines_real,'lines x',\
133 #screen_cols,'columns.' # dbg
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 """Display a string, piping through a pager after a certain length.
140 """Display a string, piping through a pager after a certain length.
137
141
138 strng can be a mime-bundle dict, supplying multiple representations,
142 strng can be a mime-bundle dict, supplying multiple representations,
@@ -164,15 +168,6 b' def page(strng, start=0, screen_lines=0, pager_cmd=None):'
164 # negative value. Offset to 0 for robustness.
168 # negative value. Offset to 0 for robustness.
165 start = max(0, start)
169 start = max(0, start)
166
170
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
176 # Ugly kludge, but calling curses.initscr() flat out crashes in emacs
171 # Ugly kludge, but calling curses.initscr() flat out crashes in emacs
177 TERM = os.environ.get('TERM','dumb')
172 TERM = os.environ.get('TERM','dumb')
178 if TERM in ['dumb','emacs'] and os.name != 'nt':
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 page_dumb(strng,screen_lines=screen_lines)
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 def page_file(fname, start=0, pager_cmd=None):
276 def page_file(fname, start=0, pager_cmd=None):
256 """Page a file, using an optional pager command and starting line.
277 """Page a file, using an optional pager command and starting line.
257 """
278 """
@@ -4,11 +4,9 b''
4 # Copyright (c) IPython Development Team.
4 # Copyright (c) IPython Development Team.
5 # Distributed under the terms of the Modified BSD License.
5 # Distributed under the terms of the Modified BSD License.
6
6
7 import warnings
7 from IPython.core.getipython import get_ipython
8 from IPython.core.getipython import get_ipython
8
9
9 #-----------------------------------------------------------------------------
10 # Classes and functions
11 #-----------------------------------------------------------------------------
12
10
13 def page(strng, start=0, screen_lines=0, pager_cmd=None):
11 def page(strng, start=0, screen_lines=0, pager_cmd=None):
14 """Print a string, piping through a pager.
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 def install_payload_page():
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 from IPython.core import page as corepage
51 from IPython.core import page as corepage
48 corepage.page = page
52 corepage.page = page
@@ -31,7 +31,7 b' from IPython.core.displaypub import DisplayPublisher'
31 from IPython.core.error import UsageError
31 from IPython.core.error import UsageError
32 from IPython.core.magics import MacroToEdit, CodeMagics
32 from IPython.core.magics import MacroToEdit, CodeMagics
33 from IPython.core.magic import magics_class, line_magic, Magics
33 from IPython.core.magic import magics_class, line_magic, Magics
34 from IPython.core.payloadpage import install_payload_page
34 from IPython.core import payloadpage
35 from IPython.core.usage import default_gui_banner
35 from IPython.core.usage import default_gui_banner
36 from IPython.display import display, Javascript
36 from IPython.display import display, Javascript
37 from IPython.kernel.inprocess.socket import SocketABC
37 from IPython.kernel.inprocess.socket import SocketABC
@@ -391,9 +391,7 b' class ZMQInteractiveShell(InteractiveShell):'
391 raise UsageError("%s" % e)
391 raise UsageError("%s" % e)
392
392
393 def init_environment(self):
393 def init_environment(self):
394 """Configure the user's environment.
394 """Configure the user's environment."""
395
396 """
397 env = os.environ
395 env = os.environ
398 # These two ensure 'ls' produces nice coloring on BSD-derived systems
396 # These two ensure 'ls' produces nice coloring on BSD-derived systems
399 env['TERM'] = 'xterm-color'
397 env['TERM'] = 'xterm-color'
@@ -404,8 +402,9 b' class ZMQInteractiveShell(InteractiveShell):'
404 env['PAGER'] = 'cat'
402 env['PAGER'] = 'cat'
405 env['GIT_PAGER'] = 'cat'
403 env['GIT_PAGER'] = 'cat'
406
404
407 # And install the payload version of page.
405 def init_hooks(self):
408 install_payload_page()
406 super(ZMQInteractiveShell, self).init_hooks()
407 self.set_hook('show_in_pager', page.as_hook(payloadpage.page), 99)
409
408
410 def ask_exit(self):
409 def ask_exit(self):
411 """Engage the exit actions."""
410 """Engage the exit actions."""
General Comments 0
You need to be logged in to leave comments. Login now