helpers.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # Copyright (c) 2012 Giorgos Verigakis <verigak@gmail.com>
  2. #
  3. # Permission to use, copy, modify, and distribute this software for any
  4. # purpose with or without fee is hereby granted, provided that the above
  5. # copyright notice and this permission notice appear in all copies.
  6. #
  7. # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. from __future__ import print_function
  15. from __future__ import unicode_literals
  16. HIDE_CURSOR = '\x1b[?25l'
  17. SHOW_CURSOR = '\x1b[?25h'
  18. class WriteMixin(object):
  19. hide_cursor = False
  20. def __init__(self, message=None, **kwargs):
  21. super(WriteMixin, self).__init__(**kwargs)
  22. self._width = 0
  23. if message:
  24. self.message = message
  25. if self.file.isatty():
  26. if self.hide_cursor:
  27. print(HIDE_CURSOR, end='', file=self.file)
  28. print(self.message, end='', file=self.file)
  29. self.file.flush()
  30. def write(self, s):
  31. if self.file.isatty():
  32. b = '\b' * self._width
  33. c = s.ljust(self._width)
  34. print(b + c, end='', file=self.file)
  35. self._width = max(self._width, len(s))
  36. self.file.flush()
  37. def finish(self):
  38. if self.file.isatty() and self.hide_cursor:
  39. print(SHOW_CURSOR, end='', file=self.file)
  40. class WritelnMixin(object):
  41. hide_cursor = False
  42. def __init__(self, message=None, **kwargs):
  43. super(WritelnMixin, self).__init__(**kwargs)
  44. if message:
  45. self.message = message
  46. if self.file.isatty() and self.hide_cursor:
  47. print(HIDE_CURSOR, end='', file=self.file)
  48. def clearln(self):
  49. if self.file.isatty():
  50. print('\r\x1b[K', end='', file=self.file)
  51. def writeln(self, line):
  52. if self.file.isatty():
  53. self.clearln()
  54. print(line, end='', file=self.file)
  55. self.file.flush()
  56. def finish(self):
  57. if self.file.isatty():
  58. print(file=self.file)
  59. if self.hide_cursor:
  60. print(SHOW_CURSOR, end='', file=self.file)
  61. from signal import signal, SIGINT
  62. from sys import exit
  63. class SigIntMixin(object):
  64. """Registers a signal handler that calls finish on SIGINT"""
  65. def __init__(self, *args, **kwargs):
  66. super(SigIntMixin, self).__init__(*args, **kwargs)
  67. signal(SIGINT, self._sigint_handler)
  68. def _sigint_handler(self, signum, frame):
  69. self.finish()
  70. exit(0)