Skip to content

Commit cabf0e6

Browse files
committed
Further experiments with the tooltip.
1 parent d758402 commit cabf0e6

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

bpython/urwid.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,14 @@ class URWIDRepl(repl.Repl):
216216

217217
# XXX this is getting silly, need to split this up somehow
218218
def __init__(self, main_loop, frame, listbox, listwalker, overlay,
219-
tooltiptext, interpreter, statusbar, config):
219+
tooltip, interpreter, statusbar, config):
220220
repl.Repl.__init__(self, interpreter, config)
221221
self.main_loop = main_loop
222222
self.frame = frame
223223
self.listbox = listbox
224224
self.listwalker = listwalker
225225
self.overlay = overlay
226-
self.tooltiptext = tooltiptext
226+
self.tooltip = tooltip
227227
self.edits = []
228228
self.edit = None
229229
self.statusbar = statusbar
@@ -279,12 +279,20 @@ def cw(self):
279279
return text[-i:]
280280

281281
def _populate_completion(self, main_loop, user_data):
282+
widget_list = self.tooltip.body
283+
widget_list[1] = urwid.Text('')
282284
# This is just me flailing around wildly. TODO: actually write.
283285
if self.complete():
284-
text = ' '.join(self.matches)
285286
if self.argspec:
286-
text = '%s\n\n%r' % (text, self.argspec)
287-
self.tooltiptext.set_text(text)
287+
text = repr(self.argspec)
288+
else:
289+
text = ''
290+
if self.matches:
291+
texts = [urwid.Text(match) for match in self.matches]
292+
width = max(text.pack()[0] for text in texts)
293+
gridflow = urwid.GridFlow(texts, width, 1, 0, 'left')
294+
widget_list[1] = gridflow
295+
widget_list[0].set_text(text)
288296
self.frame.body = self.overlay
289297
else:
290298
self.frame.body = self.listbox
@@ -342,13 +350,22 @@ def _reposition_tooltip(self):
342350
# Cursor off the screen (no clue if this can happen).
343351
# Just clamp to 0.
344352
y = 0
353+
354+
# XXX the tooltip is displayed way too huge now. The easiest way
355+
# to fix that is probably to figure out how much size the
356+
# listbox actually needs here and adjust height_amount.
357+
345358
# XXX not sure if these overlay attributes are meant to be public...
346359
if y * 2 < screen_rows:
347360
self.overlay.valign_type = 'fixed top'
348361
self.overlay.valign_amount = y + 1
362+
self.overlay.height_type = 'fixed bottom'
363+
self.overlay.height_amount = 0
349364
else:
350365
self.overlay.valign_type = 'fixed bottom'
351366
self.overlay.valign_amount = screen_rows - y - 1
367+
self.overlay.height_type = 'fixed top'
368+
self.overlay.height_amount = 0
352369

353370
def handle_input(self, event):
354371
if event == 'enter':
@@ -417,13 +434,11 @@ def main(args=None, locals_=None, banner=None):
417434
config.pastebin_key, config.last_output_key,
418435
config.show_source_key))
419436

420-
# XXX this is not great: if the tooltip is too large the bottom line
421-
# of the LineBox gets eaten. That should not happen, and there
422-
# should be a nice indicator that the Edit widget is truncated.
423-
tooltiptext = urwid.Text('')
424-
overlay = Tooltip(urwid.Filler(urwid.LineBox(tooltiptext)), listbox,
437+
tooltip = urwid.ListBox(urwid.SimpleListWalker([
438+
urwid.Text(''), urwid.Text('')]))
439+
overlay = Tooltip(urwid.LineBox(tooltip), listbox,
425440
'left', ('relative', 100),
426-
('fixed top', 0), ('relative', 50))
441+
('fixed top', 0), ('fixed bottom', 0))
427442

428443
frame = urwid.Frame(overlay, footer=statusbar.widget)
429444

@@ -437,7 +452,7 @@ def main(args=None, locals_=None, banner=None):
437452
loop = urwid.MainLoop(frame, palette, event_loop=event_loop)
438453

439454
# TODO: hook up idle callbacks somewhere.
440-
myrepl = URWIDRepl(loop, frame, listbox, listwalker, overlay, tooltiptext,
455+
myrepl = URWIDRepl(loop, frame, listbox, listwalker, overlay, tooltip,
441456
interpreter, statusbar, config)
442457

443458
# XXX HACK: circular dependency between the event loop and repl.

0 commit comments

Comments
 (0)