@@ -114,15 +114,19 @@ def __init__(self, format, new_style=False, **kwargs):
114114 self .new_style = new_style
115115 self .format = format
116116
117+ def get_format (self , progress , data , format = None ):
118+ return format or self .format
119+
117120 def __call__ (self , progress , data , format = None ):
118121 '''Formats the widget into a string'''
122+ format = self .get_format (progress , data , format )
119123 try :
120124 if self .new_style :
121- return ( format or self . format ) .format (** data )
125+ return format .format (** data )
122126 else :
123- return ( format or self . format ) % data
127+ return format % data
124128 except (TypeError , KeyError ):
125- print ('Error while formatting %r' % self . format , file = sys .stderr )
129+ print ('Error while formatting %r' % format , file = sys .stderr )
126130 pprint .pprint (data , stream = sys .stderr )
127131 raise
128132
@@ -628,17 +632,18 @@ def __call__(self, progress, data, format=None):
628632class Percentage (FormatWidgetMixin , WidgetBase ):
629633 '''Displays the current percentage as a number with a percent sign.'''
630634
631- def __init__ (self , format = '%(percentage)3d%%' , ** kwargs ):
635+ def __init__ (self , format = '%(percentage)3d%%' , na = 'N/A%%' , ** kwargs ):
636+ self .na = na
632637 FormatWidgetMixin .__init__ (self , format = format , ** kwargs )
633638 WidgetBase .__init__ (self , format = format , ** kwargs )
634639
635- def __call__ (self , progress , data , format = None ):
640+ def get_format (self , progress , data , format = None ):
636641 # If percentage is not available, display N/A%
637- if ' percentage' in data and not data [ 'percentage' ]:
638- return FormatWidgetMixin . __call__ ( self , progress , data ,
639- format = 'N/A%%' )
642+ percentage = data . get ( 'percentage' , base . Undefined )
643+ if not percentage and percentage != 0 :
644+ return self . na
640645
641- return FormatWidgetMixin .__call__ (self , progress , data )
646+ return FormatWidgetMixin .get_format (self , progress , data , format )
642647
643648
644649class SimpleProgress (FormatWidgetMixin , WidgetBase ):
@@ -904,6 +909,32 @@ def get_values(self, progress, data):
904909 return ranges
905910
906911
912+ class FormatLabelBar (FormatLabel , Bar ):
913+ '''A bar which has a formatted label in the center.'''
914+ def __init__ (self , format , ** kwargs ):
915+ FormatLabel .__init__ (self , format , ** kwargs )
916+ Bar .__init__ (self , ** kwargs )
917+
918+ def __call__ (self , progress , data , width , format = None ):
919+ center = FormatLabel .__call__ (self , progress , data , format = format )
920+ bar = Bar .__call__ (self , progress , data , width )
921+
922+ # Aligns the center of the label to the center of the bar
923+ center_len = progress .custom_len (center )
924+ center_left = int ((width - center_len ) / 2 )
925+ center_right = center_left + center_len
926+ return bar [:center_left ] + center + bar [center_right :]
927+
928+
929+ class PercentageLabelBar (Percentage , FormatLabelBar ):
930+ '''A bar which displays the current percentage in the center.'''
931+ # %3d adds an extra space that makes it look off-center
932+ # %2d keeps the label somewhat consistently in-place
933+ def __init__ (self , format = '%(percentage)2d%%' , na = 'N/A%%' , ** kwargs ):
934+ Percentage .__init__ (self , format , na = na , ** kwargs )
935+ FormatLabelBar .__init__ (self , format , ** kwargs )
936+
937+
907938class Variable (FormatWidgetMixin , VariableMixin , WidgetBase ):
908939 '''Displays a custom variable.'''
909940
0 commit comments