You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.9 KiB

4 years ago
  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. import sys
  17. from . import Progress
  18. from .helpers import WritelnMixin
  19. class Bar(WritelnMixin, Progress):
  20. width = 32
  21. message = ''
  22. suffix = '%(index)d/%(max)d'
  23. bar_prefix = ' |'
  24. bar_suffix = '| '
  25. empty_fill = ' '
  26. fill = '#'
  27. hide_cursor = True
  28. def update(self):
  29. filled_length = int(self.width * self.progress)
  30. empty_length = self.width - filled_length
  31. message = self.message % self
  32. bar = self.fill * filled_length
  33. empty = self.empty_fill * empty_length
  34. suffix = self.suffix % self
  35. line = ''.join([message, self.bar_prefix, bar, empty, self.bar_suffix,
  36. suffix])
  37. self.writeln(line)
  38. class ChargingBar(Bar):
  39. suffix = '%(percent)d%%'
  40. bar_prefix = ' '
  41. bar_suffix = ' '
  42. empty_fill = ''
  43. fill = ''
  44. class FillingSquaresBar(ChargingBar):
  45. empty_fill = ''
  46. fill = ''
  47. class FillingCirclesBar(ChargingBar):
  48. empty_fill = ''
  49. fill = ''
  50. class IncrementalBar(Bar):
  51. if sys.platform.startswith('win'):
  52. phases = (u' ', u'', u'')
  53. else:
  54. phases = (' ', '', '', '', '', '', '', '', '')
  55. def update(self):
  56. nphases = len(self.phases)
  57. filled_len = self.width * self.progress
  58. nfull = int(filled_len) # Number of full chars
  59. phase = int((filled_len - nfull) * nphases) # Phase of last char
  60. nempty = self.width - nfull # Number of empty chars
  61. message = self.message % self
  62. bar = self.phases[-1] * nfull
  63. current = self.phases[phase] if phase > 0 else ''
  64. empty = self.empty_fill * max(0, nempty - len(current))
  65. suffix = self.suffix % self
  66. line = ''.join([message, self.bar_prefix, bar, current, empty,
  67. self.bar_suffix, suffix])
  68. self.writeln(line)
  69. class PixelBar(IncrementalBar):
  70. phases = ('', '', '', '', '', '', '', '')
  71. class ShadyBar(IncrementalBar):
  72. phases = (' ', '', '', '', '')