spinner.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # vim: fileencoding=utf-8
  2. #
  3. # The Qubes OS Project, https://www.qubes-os.org/
  4. #
  5. # Copyright (C) 2017 Wojtek Porczyk <woju@invisiblethingslab.com>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU Lesser General Public License as published by
  9. # the Free Software Foundation; either version 2.1 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Lesser General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public License
  18. # along with this program; if not, see <http://www.gnu.org/licenses/>.
  19. #
  20. '''Qubes CLI spinner
  21. A novice asked the master: “In the east there is a great tree-structure that
  22. men call `Corporate Headquarters'. It is bloated out of shape with vice
  23. presidents and accountants. It issues a multitude of memos, each saying `Go,
  24. Hence!' or `Go, Hither!' and nobody knows what is meant. Every year new names
  25. are put onto the branches, but all to no avail. How can such an unnatural
  26. entity be?"
  27. The master replied: “You perceive this immense structure and are disturbed that
  28. it has no rational purpose. Can you not take amusement from its endless
  29. gyrations? Do you not enjoy the untroubled ease of programming beneath its
  30. sheltering branches? Why are you bothered by its uselessness?”
  31. (Geoffrey James, “The Tao of Programming”, 7.1)
  32. '''
  33. import curses
  34. import io
  35. import itertools
  36. CHARSET = '-\\|/'
  37. ENTERPRISE_CHARSET = CHARSET * 4 + '-._.-^' * 2
  38. class AbstractSpinner(object):
  39. '''The base class for all Spinners
  40. :param stream: file-like object with ``.write()`` method
  41. :param str charset: the sequence of characters to display
  42. The spinner should be used as follows:
  43. 1. exactly one call to :py:meth:`show()`
  44. 2. zero or more calls to :py:meth:`update()`
  45. 3. exactly one call to :py:meth:`hide()`
  46. '''
  47. def __init__(self, stream, charset=CHARSET):
  48. self.stream = stream
  49. self.charset = itertools.cycle(charset)
  50. def show(self, prompt):
  51. '''Show the spinner, with a prompt
  52. :param str prompt: prompt, like "please wait"
  53. '''
  54. raise NotImplementedError()
  55. def hide(self):
  56. '''Hide the spinner and the prompt'''
  57. raise NotImplementedError()
  58. def update(self):
  59. '''Show next spinner character'''
  60. raise NotImplementedError()
  61. class DummySpinner(AbstractSpinner):
  62. '''Dummy spinner, does not do anything'''
  63. def show(self, prompt):
  64. pass
  65. def hide(self):
  66. pass
  67. def update(self):
  68. pass
  69. class QubesSpinner(AbstractSpinner):
  70. '''Basic spinner
  71. This spinner uses standard ASCII control characters'''
  72. def __init__(self, *args, **kwargs):
  73. super().__init__(*args, **kwargs)
  74. self.hidelen = 0
  75. self.cub1 = '\b'
  76. def show(self, prompt):
  77. self.hidelen = len(prompt) + 2
  78. self.stream.write('{} {}'.format(prompt, next(self.charset)))
  79. self.stream.flush()
  80. def hide(self):
  81. self.stream.write('\r' + ' ' * self.hidelen + '\r')
  82. self.stream.flush()
  83. def update(self):
  84. self.stream.write(self.cub1 + next(self.charset))
  85. self.stream.flush()
  86. class QubesSpinnerEnterpriseEdition(QubesSpinner):
  87. '''Enterprise spinner
  88. This is tty- and terminfo-aware spinner. Recommended.
  89. '''
  90. def __init__(self, stream, charset=None):
  91. # our Enterprise logic follows
  92. self.stream_isatty = stream.isatty()
  93. if charset is None:
  94. charset = ENTERPRISE_CHARSET if self.stream_isatty else '.'
  95. super().__init__(stream, charset)
  96. if self.stream_isatty:
  97. try:
  98. curses.setupterm()
  99. self.has_terminfo = True
  100. self.cub1 = curses.tigetstr('cub1').decode()
  101. except (curses.error, io.UnsupportedOperation):
  102. # we are in very non-Enterprise environment
  103. self.has_terminfo = False
  104. else:
  105. self.cub1 = ''
  106. def hide(self):
  107. if self.stream_isatty:
  108. hideseq = '\r' + ' ' * self.hidelen + '\r'
  109. if self.has_terminfo:
  110. hideseq_l = (curses.tigetstr('cr'), curses.tigetstr('clr_eol'))
  111. if all(seq is not None for seq in hideseq_l):
  112. hideseq = ''.join(seq.decode() for seq in hideseq_l)
  113. else:
  114. hideseq = '\n'
  115. self.stream.write(hideseq)
  116. self.stream.flush()