@@ -348,15 +348,18 @@ def _get_text(self, text):
348
348
self
349
349
"""
350
350
animation = self ._animation
351
- stripped_text = text .strip ()
351
+ if hasattr (text , "__call__" ):
352
+ stripped_text = lambda : text ().strip ()
353
+ else :
354
+ stripped_text = text .strip ()
352
355
353
356
# Check which frame of the animation is the widest
354
357
max_spinner_length = max ([len (i ) for i in self ._spinner ["frames" ]])
355
358
356
- # Subtract to the current terminal size the max spinner length
359
+ # Subtract from the current terminal size the max spinner length
357
360
# (-1 to leave room for the extra space between spinner and text)
358
361
terminal_width = get_terminal_columns () - max_spinner_length - 1
359
- text_length = len (stripped_text )
362
+ text_length = len (stripped_text ()) if hasattr ( text , '__call__' ) else len ( stripped_text )
360
363
361
364
frames = []
362
365
@@ -366,18 +369,30 @@ def _get_text(self, text):
366
369
Make the text bounce back and forth
367
370
"""
368
371
for x in range (0 , text_length - terminal_width + 1 ):
369
- frames .append (stripped_text [x : terminal_width + x ])
372
+ if hasattr (text , '__call__' ):
373
+ frame = stripped_text ()[x : terminal_width + x ]
374
+ else :
375
+ frame = stripped_text [x : terminal_width + x ]
376
+ frames .append (frame )
370
377
frames .extend (list (reversed (frames )))
371
378
elif "marquee" :
372
379
"""
373
380
Make the text scroll like a marquee
374
381
"""
375
- stripped_text = stripped_text + " " + stripped_text [:terminal_width ]
376
- for x in range (0 , text_length + 1 ):
377
- frames .append (stripped_text [x : terminal_width + x ])
382
+ if hasattr (text , '__call__' ):
383
+ new_stripped_text = lambda : stripped_text () + " " + stripped_text ()[:terminal_width ]
384
+ for x in range (0 , text_length + 1 ):
385
+ frames .append (new_stripped_text ()[x : terminal_width + x ])
386
+ else :
387
+ stripped_text = stripped_text + " " + stripped_text [:terminal_width ]
388
+ for x in range (0 , text_length + 1 ):
389
+ frames .append (stripped_text [x : terminal_width + x ])
378
390
elif terminal_width < text_length and not animation :
379
391
# Add ellipsis if text is larger than terminal width and no animation was specified
380
- frames = [stripped_text [: terminal_width - 6 ] + " (...)" ]
392
+ if hasattr (text , '__call__' ):
393
+ frames = [lambda : stripped_text ()[: terminal_width - 6 ] + " (...)" ]
394
+ else :
395
+ frames = [stripped_text [: terminal_width - 6 ] + " (...)" ]
381
396
else :
382
397
frames = [stripped_text ]
383
398
@@ -454,14 +469,19 @@ def text_frame(self):
454
469
self
455
470
"""
456
471
if len (self ._text ["frames" ]) == 1 :
472
+ frame = self ._text ["frames" ][0 ]
473
+ if hasattr (frame , '__call__' ):
474
+ frame = frame ()
457
475
if self ._text_color :
458
- return colored_frame (self . _text [ "frames" ][ 0 ] , self ._text_color )
476
+ return colored_frame (frame , self ._text_color )
459
477
460
478
# Return first frame (can't return original text because at this point it might be ellipsed)
461
- return self . _text [ "frames" ][ 0 ]
479
+ return frame
462
480
463
481
frames = self ._text ["frames" ]
464
482
frame = frames [self ._text_index ]
483
+ if hasattr (frame , '__call__' ):
484
+ frame = frame ()
465
485
466
486
self ._text_index += 1
467
487
self ._text_index = self ._text_index % len (frames )
0 commit comments