Skip to content

Commit 7035e96

Browse files
committed
Use class Caption(urwid.Text) to represent the header.
`Caption` will handle the display of header's content, including the current source filename
1 parent eb97760 commit 7035e96

File tree

2 files changed

+74
-22
lines changed

2 files changed

+74
-22
lines changed

pudb/debugger.py

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,8 @@ def helpside(w, size, key):
858858
],
859859
dividechars=1)
860860

861-
self.caption = urwid.Text("")
861+
from pudb.ui_tools import Caption
862+
self.caption = Caption("")
862863
header = urwid.AttrMap(self.caption, "header")
863864
self.top = SignalWrap(urwid.Frame(
864865
urwid.AttrMap(self.columns, "background"),
@@ -2618,37 +2619,31 @@ def interaction(self, exc_tuple, show_exc_dialog=True):
26182619

26192620
from pudb import VERSION
26202621
separator = " - "
2621-
info_string = separator.join(["PuDB %s" % VERSION, "?:help"])
2622-
2623-
def get_source_filename():
2624-
available_width = self.screen.get_cols_rows(
2625-
)[0] - (len(info_string) + len(separator))
2626-
full_filename = self.source_code_provider.get_source_identifier()
2627-
if (full_filename is None):
2628-
return "Source filename not available"
2629-
elif available_width > len(full_filename):
2630-
return full_filename
2631-
else:
2632-
trim_index = len(full_filename) - available_width
2633-
filename = full_filename[trim_index:]
2634-
first_dirname_index = filename.find(os.sep)
2635-
filename = filename[first_dirname_index + 1:]
2636-
return filename
2637-
2638-
caption = [(None, separator.join([info_string, get_source_filename()]))]
2622+
pudb_version = "PuDB %s" % VERSION
2623+
hotkey = "?:help"
2624+
if self.source_code_provider.get_source_identifier():
2625+
source_filename = self.source_code_provider.get_source_identifier()
2626+
else:
2627+
source_filename = "source filename is unavailable"
2628+
caption = [(None, pudb_version),
2629+
(None, separator),
2630+
(None, hotkey),
2631+
(None, separator),
2632+
(None, source_filename)
2633+
]
26392634

26402635
if self.debugger.post_mortem:
26412636
if show_exc_dialog and exc_tuple is not None:
26422637
self.show_exception_dialog(exc_tuple)
26432638

26442639
caption.extend([
2645-
(None, " "),
2640+
(None, separator),
26462641
("warning", "[POST-MORTEM MODE]")
26472642
])
26482643
elif exc_tuple is not None:
26492644
caption.extend([
2650-
(None, " "),
2651-
("warning", "[PROCESSING EXCEPTION - hit 'e' to examine]")
2645+
(None, separator),
2646+
("warning", "[PROCESSING EXCEPTION, hit 'e' to examine]")
26522647
])
26532648

26542649
self.caption.set_text(caption)

pudb/ui_tools.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,4 +332,61 @@ def keypress(self, size, key):
332332

333333
return result
334334

335+
336+
class Caption(urwid.Text):
337+
def __init__(self, markup, separator=" - "):
338+
self.separator = separator
339+
super().__init__(markup)
340+
341+
def set_text(self, markup):
342+
super().set_text(markup)
343+
if len(markup) > 0:
344+
# Assume the format of caption is:
345+
# <PuDB version> <hotkey> <source filename> [optional_alert]
346+
caption, _ = self.get_text()
347+
caption_elements = caption.split(self.separator)
348+
self.pudb_version = caption_elements[0]
349+
self.hotkey = caption_elements[1]
350+
self.full_source_filename = caption_elements[2]
351+
self.optional_alert = caption_elements[3] if len(
352+
caption_elements) > 3 else ""
353+
else:
354+
self.pudb_version = self.hotkey = ""
355+
self.full_source_filename = self.optional_alert = ""
356+
357+
def rows(self, size, focus=False):
358+
# Always return 1 to avoid
359+
# `assert head.rows() == hrows, "rows, render mismatch")`
360+
# in urwid.Frame.render() in urwid/container.py
361+
return 1
362+
363+
def render(self, size, focus=False):
364+
maxcol = size[0]
365+
if super().rows(size) > 1:
366+
filename = self.get_shortened_source_filename(size)
367+
else:
368+
filename = self.full_source_filename
369+
caption = self.separator.join(
370+
[self.pudb_version, self.hotkey, filename, self.optional_alert]
371+
).strip(self.separator)
372+
if self.optional_alert:
373+
attr = [("warning", len(caption))]
374+
else:
375+
attr = [(None, 0)]
376+
377+
return make_canvas([caption], [attr], maxcol)
378+
379+
def get_shortened_source_filename(self, size):
380+
import os
381+
maxcol = size[0]
382+
383+
occupied_width = (len(self.pudb_version) + len(self.hotkey)
384+
+ len(self.optional_alert) + len(self.separator)*3)
385+
available_width = maxcol - occupied_width
386+
trim_index = len(self.full_source_filename) - available_width
387+
filename = self.full_source_filename[trim_index:]
388+
first_dirname_index = filename.find(os.sep)
389+
filename = filename[first_dirname_index + 1:]
390+
391+
return filename
335392
# }}}

0 commit comments

Comments
 (0)