bar.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) 2012 Georgios Verigakis <verigak@gmail.com>
  3. #
  4. # Permission to use, copy, modify, and distribute this software for any
  5. # purpose with or without fee is hereby granted, provided that the above
  6. # copyright notice and this permission notice appear in all copies.
  7. #
  8. # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. from __future__ import unicode_literals
  16. import sys
  17. from . import Progress
  18. from .colors import color
  19. class Bar(Progress):
  20. width = 32
  21. suffix = '%(index)d/%(max)d'
  22. bar_prefix = ' |'
  23. bar_suffix = '| '
  24. empty_fill = ' '
  25. fill = '#'
  26. color = None
  27. def update(self):
  28. filled_length = int(self.width * self.progress)
  29. empty_length = self.width - filled_length
  30. message = self.message % self
  31. bar = color(self.fill * filled_length, fg=self.color)
  32. empty = self.empty_fill * empty_length
  33. suffix = self.suffix % self
  34. line = ''.join([message, self.bar_prefix, bar, empty, self.bar_suffix,
  35. suffix])
  36. self.writeln(line)
  37. class ChargingBar(Bar):
  38. suffix = '%(percent)d%%'
  39. bar_prefix = ' '
  40. bar_suffix = ' '
  41. empty_fill = '∙'
  42. fill = '█'
  43. class FillingSquaresBar(ChargingBar):
  44. empty_fill = '▢'
  45. fill = '▣'
  46. class FillingCirclesBar(ChargingBar):
  47. empty_fill = '◯'
  48. fill = '◉'
  49. class IncrementalBar(Bar):
  50. if sys.platform.startswith('win'):
  51. phases = (u' ', u'▌', u'█')
  52. else:
  53. phases = (' ', '▏', '▎', '▍', '▌', '▋', '▊', '▉', '█')
  54. def update(self):
  55. nphases = len(self.phases)
  56. filled_len = self.width * self.progress
  57. nfull = int(filled_len) # Number of full chars
  58. phase = int((filled_len - nfull) * nphases) # Phase of last char
  59. nempty = self.width - nfull # Number of empty chars
  60. message = self.message % self
  61. bar = color(self.phases[-1] * nfull, fg=self.color)
  62. current = self.phases[phase] if phase > 0 else ''
  63. empty = self.empty_fill * max(0, nempty - len(current))
  64. suffix = self.suffix % self
  65. line = ''.join([message, self.bar_prefix, bar, current, empty,
  66. self.bar_suffix, suffix])
  67. self.writeln(line)
  68. class PixelBar(IncrementalBar):
  69. phases = ('⡀', '⡄', '⡆', '⡇', '⣇', '⣧', '⣷', '⣿')
  70. class ShadyBar(IncrementalBar):
  71. phases = (' ', '░', '▒', '▓', '█')