bar.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. expanded_length = int(nphases * self.width * self.progress)
  54. filled_length = int(self.width * self.progress)
  55. empty_length = self.width - filled_length
  56. phase = expanded_length - (filled_length * nphases)
  57. message = self.message % self
  58. bar = self.phases[-1] * filled_length
  59. current = self.phases[phase] if phase > 0 else ''
  60. empty = self.empty_fill * max(0, empty_length - 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 ShadyBar(IncrementalBar):
  66. phases = (' ', '░', '▒', '▓', '█')