From 4010760065c406116e66a9a6663d536295dadff9 2006-01-14 13:22:58 From: vivainio Date: 2006-01-14 13:22:58 Subject: [PATCH] Configurable display hook --- diff --git a/IPython/Prompts.py b/IPython/Prompts.py index d8aee7e..dca990f 100644 --- a/IPython/Prompts.py +++ b/IPython/Prompts.py @@ -2,7 +2,7 @@ """ Classes for handling input/output prompts. -$Id: Prompts.py 1005 2006-01-12 08:39:26Z fperez $""" +$Id: Prompts.py 1020 2006-01-14 13:22:58Z vivainio $""" #***************************************************************************** # Copyright (C) 2001-2006 Fernando Perez @@ -525,19 +525,11 @@ class CachedOutput: def _display(self,arg): """Default printer method, uses pprint. - This can be over-ridden by the users to implement special formatting - of certain types of output.""" - - if self.Pprint: - out = pformat(arg) - if '\n' in out: - # So that multi-line strings line up with the left column of - # the screen, instead of having the output prompt mess up - # their first line. - Term.cout.write('\n') - print >>Term.cout, out - else: - print >>Term.cout, arg + Do ip.set_hook("result_display", my_displayhook) for custom result + display, e.g. when your own objects need special formatting. + """ + + self.shell.hooks.result_display(arg) # Assign the default display method: display = _display diff --git a/IPython/hooks.py b/IPython/hooks.py index 4c28dc3..ad863e4 100644 --- a/IPython/hooks.py +++ b/IPython/hooks.py @@ -32,7 +32,7 @@ ip_set_hook('editor',myiphooks.calljed) The ip_set_hook function is put by IPython into the builtin namespace, so it is always available from all running code. -$Id: hooks.py 1019 2006-01-14 13:02:12Z vivainio $""" +$Id: hooks.py 1020 2006-01-14 13:22:58Z vivainio $""" #***************************************************************************** # Copyright (C) 2005 Fernando Perez. @@ -47,10 +47,12 @@ __license__ = Release.license __version__ = Release.version import os,bisect +from genutils import Term +from pprint import pformat # List here all the default hooks. For now it's just the editor functions # but over time we'll move here all the public API for user-accessible things. -__all__ = ['editor', 'fix_error_editor'] +__all__ = ['editor', 'fix_error_editor', 'result_display'] def editor(self,filename, linenum=None): """Open the default editor at the given filename and linenumber. @@ -133,4 +135,15 @@ class CommandChainDispatcher: """ Add a func to the cmd chain with given priority """ bisect.insort(self.chain,(priority,func)) +def result_display(self,arg): + if self.rc.pprint: + out = pformat(arg) + if '\n' in out: + # So that multi-line strings line up with the left column of + # the screen, instead of having the output prompt mess up + # their first line. + Term.cout.write('\n') + print >>Term.cout, out + else: + print >>Term.cout, arg \ No newline at end of file diff --git a/doc/ChangeLog b/doc/ChangeLog index 9e1dd4c..2884174 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -13,8 +13,8 @@ as "chain of command", with priority. API stays the same, TryNext exception raised by a hook function signals that current hook failed and next hook should try handling it, as - suggested by Walter Dörwald . - + suggested by Walter Dörwald . Walter also + requested configurable display hook, which is now implemented. 2006-01-13 Ville Vainio