@@ -332,4 +332,61 @@ def keypress(self, size, key):
332
332
333
333
return result
334
334
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
335
392
# }}}
0 commit comments