|
1 | 1 | import pydoc |
2 | 2 | import textwrap |
3 | 3 | import sys |
| 4 | +import cStringIO |
4 | 5 |
|
5 | 6 | # window has to be a global so that the main bpython.py can load it and |
6 | 7 | # alter its state and share it with the interpreter being used for the |
7 | 8 | # actual user input, I couldn't think of another way of doing this. |
8 | 9 | window = None |
9 | 10 |
|
10 | | -def _help( query ): |
| 11 | +def _help( obj ): |
11 | 12 | """Wrapper for the regular help() function but with a ghetto |
12 | 13 | PAGER since curses + less = :( |
| 14 | + As per the vanilla help(), this function special-cases for str, |
| 15 | + so you can do help('isinstance') or help(isinstance) and get the |
| 16 | + same result. |
13 | 17 | """ |
| 18 | + io = cStringIO.StringIO() |
14 | 19 | doc = pydoc.TextDoc() |
| 20 | + helper = pydoc.Helper( None, io ) |
| 21 | + |
15 | 22 | rows, columns = window.getmaxyx() |
16 | 23 | rows -= 3 |
17 | 24 | columns -= 1 |
18 | | - output = doc.document( query ) |
| 25 | + output = None |
| 26 | + |
| 27 | +# Copied and pasted from Lib/pydoc.py and fiddled with |
| 28 | +# so it works fine with bpython. As far as I can tell |
| 29 | +# the bpython help is no compliant with the vanilla help. |
| 30 | +# Please let me know if you find this to be untrue. |
| 31 | + if type(obj) is type(''): |
| 32 | + if obj == 'help': helper.intro() |
| 33 | + elif obj == 'keywords': helper.listkeywords() |
| 34 | + elif obj == 'topics': helper.listtopics() |
| 35 | + elif obj == 'modules': helper.listmodules() |
| 36 | + elif obj[:8] == 'modules ': |
| 37 | + helper.listmodules(split(obj)[1]) |
| 38 | + elif obj in helper.keywords: helper.showtopic(obj) |
| 39 | + elif obj in helper.topics: helper.showtopic(obj) |
| 40 | + elif obj: output = doc.document( eval(obj) ) |
| 41 | +####################### |
| 42 | + |
| 43 | + else: |
| 44 | + output = doc.document( obj ) |
| 45 | + if not output: |
| 46 | + output = "No help found for %s" % obj |
| 47 | + return |
| 48 | + |
| 49 | + if output is None: |
| 50 | + output = io.getvalue() |
| 51 | + io.close() |
| 52 | + |
19 | 53 | if not output: |
20 | | - output = "No help found for %s" % query |
| 54 | + return |
21 | 55 |
|
22 | 56 | output = output.replace('\t', ' ') |
23 | 57 |
|
|
0 commit comments