|
1 | 1 | import pydoc |
2 | | -import textwrap |
3 | 2 | import sys |
4 | | -import cStringIO |
5 | 3 |
|
6 | | -# window has to be a global so that the main bpython.py can load it and |
7 | | -# alter its state and share it with the interpreter being used for the |
8 | | -# actual user input, I couldn't think of another way of doing this. |
9 | | -window = None |
| 4 | +from bpython.pager import page |
10 | 5 |
|
| 6 | +# Ugly monkeypatching |
| 7 | +pydoc.pager = page |
11 | 8 |
|
12 | | -def _help(obj): |
13 | | - """Wrapper for the regular help() function but with a ghetto |
14 | | - PAGER since curses + less = :( |
15 | | - As per the vanilla help(), this function special-cases for str, |
16 | | - so you can do help('isinstance') or help(isinstance) and get the |
17 | | - same result. |
18 | | - """ |
19 | | - io = cStringIO.StringIO() |
20 | | - doc = pydoc.TextDoc() |
21 | | - helper = pydoc.Helper(None, io) |
22 | 9 |
|
23 | | - rows, columns = window.getmaxyx() |
24 | | - rows -= 3 |
25 | | - columns -= 1 |
26 | | - output = None |
| 10 | +class _Helper(object): |
| 11 | + def __init__(self): |
| 12 | + self.helper = pydoc.Helper(sys.stdin, sys.stdout) |
27 | 13 |
|
28 | | -# Copied and pasted from Lib/pydoc.py and fiddled with |
29 | | -# so it works fine with bpython. As far as I can tell |
30 | | -# the bpython help is no compliant with the vanilla help. |
31 | | -# Please let me know if you find this to be untrue. |
32 | | - if type(obj) is type(''): |
33 | | - if obj == 'help': |
34 | | - helper.intro() |
35 | | - elif obj == 'keywords': |
36 | | - helper.listkeywords() |
37 | | - elif obj == 'topics': |
38 | | - helper.listtopics() |
39 | | - elif obj == 'modules': |
40 | | - helper.listmodules() |
41 | | - elif obj[:8] == 'modules ': |
42 | | - helper.listmodules(split(obj)[1]) |
43 | | - elif obj in helper.keywords: |
44 | | - helper.showtopic(obj) |
45 | | - elif obj in helper.topics: |
46 | | - helper.showtopic(obj) |
47 | | - elif obj: |
48 | | - output = doc.document(eval(obj)) |
49 | | -####################### |
| 14 | + def __repr__(self): |
| 15 | + return ("Type help() for interactive help, " |
| 16 | + "or help(object) for help about object.") |
50 | 17 |
|
51 | | - else: |
52 | | - output = doc.document(obj) |
53 | | - if not output: |
54 | | - output = "No help found for %s" % (obj, ) |
55 | | - return |
| 18 | + def __call__(self, *args, **kwargs): |
| 19 | + self.helper(*args, **kwargs) |
56 | 20 |
|
57 | | - if output is None: |
58 | | - output = io.getvalue() |
59 | | - io.close() |
| 21 | +_help = _Helper() |
60 | 22 |
|
61 | | - if not output: |
62 | | - return |
63 | | - |
64 | | - output = output.replace('\t', ' ') |
65 | | - |
66 | | - if '\n' in output: |
67 | | - output = output.replace('\n\n', '\n') |
68 | | - output = output.split('\n') |
69 | | - else: |
70 | | - output = [output] |
71 | | - |
72 | | - paragraphs = [] |
73 | | - for o in output: |
74 | | - paragraphs.append(textwrap.wrap(o, columns)) |
75 | | - |
76 | | - i = 0 |
77 | | - for j, paragraph in enumerate(paragraphs): |
78 | | - for line in paragraph: |
79 | | - sys.stdout.write(line + '\n') |
80 | | - i += 1 |
81 | | -# This is a little unclear, but it just waits for a |
82 | | -# keypress when the a page worth of text has been |
83 | | -# displayed and returns if 'q' is pressed: |
84 | | - if not i % rows and not wait_for_key(): |
85 | | - return |
86 | | - |
87 | | - |
88 | | -def wait_for_key(): |
89 | | - """Block until a key is pressed for the ghetto paging.""" |
90 | | - |
91 | | - q = True |
92 | | - window.addstr("Press any key, q to cancel.") |
93 | | - while True: |
94 | | - c = window.getch() |
95 | | - if c and c == ord('q'): |
96 | | - q = False |
97 | | - if c: |
98 | | - break |
99 | | - clear_line() |
100 | | - return q |
101 | | - |
102 | | - |
103 | | -def clear_line(): |
104 | | - y = window.getyx()[0] |
105 | | - window.move(y, 0) |
106 | | - window.clrtoeol() |
107 | 23 |
|
108 | 24 | # vim: sw=4 ts=4 sts=4 ai et |
0 commit comments