mirror of
https://github.com/kennethreitz-archive/bpython-gist.git
synced 2026-06-05 23:50:18 +00:00
help() fixed up so it's internalised
A few people pointed out that help() can cause problems, specifically when the help string is really big, so I've internalised it and injected my own help() function into the interpreter which pages the output, but it's pretty ghetto so I'm open to suggestions for improvement. That said, it's pretty obvious that scrolling up and down (like less) would be the main requested improvement so I should get to work on that at some point.
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
import pydoc
|
||||
import textwrap
|
||||
import sys
|
||||
|
||||
window = None
|
||||
|
||||
def _help( query ):
|
||||
"""Wrapper for the regular help() function but with a ghetto
|
||||
PAGER since curses + less = :(
|
||||
query : the actual search thing
|
||||
window : a curses window instance to use getch() on
|
||||
and for extrapolating the window size to work it all out"""
|
||||
|
||||
rows, columns = window.getmaxyx()
|
||||
rows -= 3
|
||||
columns -= 1
|
||||
output = pydoc.getdoc( query )
|
||||
if '\n' in output:
|
||||
output = output.replace('\n\n', '\n')
|
||||
output = output.split('\n')
|
||||
else:
|
||||
output = [output]
|
||||
|
||||
paragraphs = []
|
||||
for o in output:
|
||||
paragraphs.append( textwrap.wrap( o, columns ) )
|
||||
|
||||
i = 0
|
||||
for j, paragraph in enumerate( paragraphs ):
|
||||
for line in paragraph:
|
||||
sys.stdout.write( line + '\n' )
|
||||
i += 1
|
||||
if not i % rows:
|
||||
wait_for_key( window )
|
||||
if j + 1 < len( paragraphs ):
|
||||
sys.stdout.write('\n ')
|
||||
|
||||
def wait_for_key( window ):
|
||||
"""Block until a key is pressed for the ghetto paging."""
|
||||
|
||||
window.addstr("Press any key...")
|
||||
while True:
|
||||
c = window.getch()
|
||||
if c:
|
||||
break
|
||||
|
||||
y = window.getyx()[0]
|
||||
window.move(y-1, 0)
|
||||
window.clrtoeol()
|
||||
Reference in New Issue
Block a user