progress.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/env python
  2. # License: GPLv3 Copyright: 2021, Kovid Goyal <kovid at kovidgoyal.net>
  3. from .operations import repeat, styled
  4. def render_progress_bar(frac: float, width: int = 80) -> str:
  5. if frac >= 1:
  6. return styled('🬋' * width, fg='green')
  7. if frac <= 0:
  8. return styled('🬋' * width, dim=True)
  9. w = frac * width
  10. fl = int(w)
  11. overhang = w - fl
  12. filled = repeat('🬋', fl)
  13. if overhang < 0.2:
  14. needs_break = True
  15. elif overhang < 0.8:
  16. filled += '🬃'
  17. fl += 1
  18. needs_break = False
  19. else:
  20. if fl < width - 1:
  21. filled += '🬋'
  22. fl += 1
  23. needs_break = True
  24. else:
  25. filled += '🬃'
  26. fl += 1
  27. needs_break = False
  28. ans = styled(filled, fg='blue')
  29. unfilled = '🬇' if width > fl and needs_break else ''
  30. filler = width - fl - len(unfilled)
  31. if filler > 0:
  32. unfilled += repeat('🬋', filler)
  33. if unfilled:
  34. ans += styled(unfilled, dim=True)
  35. return ans