bar.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) 2012 Giorgos 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. from . import Progress
  17. from .helpers import WritelnMixin
  18. class Bar(WritelnMixin, Progress):
  19. width = 32
  20. message = ''
  21. suffix = '%(index)d/%(max)d'
  22. bar_prefix = ' |'
  23. bar_suffix = '| '
  24. empty_fill = ' '
  25. fill = '#'
  26. hide_cursor = True
  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 = self.fill * filled_length
  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. phases = (' ', '▏', '▎', '▍', '▌', '▋', '▊', '▉', '█')
  51. def update(self):
  52. nphases = len(self.phases)
  53. filled_len = self.width * self.progress
  54. nfull = int(filled_len) # Number of full chars
  55. phase = int((filled_len - nfull) * nphases) # Phase of last char
  56. nempty = self.width - nfull # Number of empty chars
  57. message = self.message % self
  58. bar = self.phases[-1] * nfull
  59. current = self.phases[phase] if phase > 0 else ''
  60. empty = self.empty_fill * max(0, nempty - len(current))
  61. suffix = self.suffix % self
  62. line = ''.join([message, self.bar_prefix, bar, current, empty,
  63. self.bar_suffix, suffix])
  64. self.writeln(line)
  65. class PixelBar(IncrementalBar):
  66. phases = ('⡀', '⡄', '⡆', '⡇', '⣇', '⣧', '⣷', '⣿')
  67. class ShadyBar(IncrementalBar):
  68. phases = (' ', '░', '▒', '▓', '█')