Skip to content

Commit b495348

Browse files
author
Bob Farrell
committed
Screwed up my commit.
Oops, messed up a little. Hopefully everything's okay. I moved internal.py to _internal.py so it isn't cluttering the namespace so much, and also I forgot to mention that I wrote a config parser using shlex so you can set any attribute of the OPTS object now, e.g. auto_display_list syntax arg_spec committer: Bob Farrell <bob@keys.(none)>
1 parent 91dfed9 commit b495348

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

‎bpython/_internal.py‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import pydoc
2+
import textwrap
3+
import sys
4+
5+
# window has to be a global so that the main bpython.py can load it and
6+
# alter its state and share it with the interpreter being used for the
7+
# actual user input, I couldn't think of another way of doing this.
8+
window = None
9+
10+
def _help( query ):
11+
"""Wrapper for the regular help() function but with a ghetto
12+
PAGER since curses + less = :(
13+
"""
14+
doc = pydoc.TextDoc()
15+
rows, columns = window.getmaxyx()
16+
rows -= 3
17+
columns -= 1
18+
output = doc.document( query )
19+
if not output:
20+
output = "No help found for %s" % query
21+
22+
output = output.replace('\t', ' ')
23+
24+
if '\n' in output:
25+
output = output.replace('\n\n', '\n')
26+
output = output.split('\n')
27+
else:
28+
output = [output]
29+
30+
paragraphs = []
31+
for o in output:
32+
paragraphs.append( textwrap.wrap( o, columns ) )
33+
34+
i = 0
35+
for j, paragraph in enumerate( paragraphs ):
36+
for line in paragraph:
37+
sys.stdout.write( line + '\n' )
38+
i += 1
39+
# This is a little unclear, but it just waits for a
40+
# keypress when the a page worth of text has been
41+
# displayed and returns if 'q' is pressed:
42+
if not i % rows and not wait_for_key():
43+
return
44+
45+
def wait_for_key():
46+
"""Block until a key is pressed for the ghetto paging."""
47+
48+
q = True
49+
window.addstr("Press any key, q to cancel.")
50+
while True:
51+
c = window.getch()
52+
if c and c == ord('q'):
53+
q = False
54+
if c:
55+
break
56+
clear_line()
57+
return q
58+
59+
def clear_line():
60+
y = window.getyx()[0]
61+
window.move(y, 0)
62+
window.clrtoeol()

0 commit comments

Comments
 (0)