selector.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #!/usr/bin/env python
  2. import os
  3. import curses
  4. import curses.wrapper
  5. import locale
  6. import pprint
  7. import textwrap
  8. KEY_ESC = 27
  9. KEY_SDOWN = 336
  10. KEY_SUP = 337
  11. class Node(object):
  12. def __init__(self):
  13. self._parent = None
  14. self._children = []
  15. self._selected = False
  16. pass
  17. def child_count(self):
  18. return len(self._children)
  19. def find_root(self):
  20. if self._parent is None:
  21. return self
  22. else:
  23. return self._parent.find_root()
  24. def find_selected(self):
  25. selected_nodes = []
  26. if self.selected():
  27. selected_nodes.append(self)
  28. for child in self.get_children():
  29. selected_nodes = selected_nodes + child.find_selected()
  30. return selected_nodes
  31. def get_label(self, active_root):
  32. return ''
  33. def _clear_children(self):
  34. self._children = []
  35. def get_children(self):
  36. return self._children
  37. def _append_child(self, child):
  38. child._parent = self
  39. self._children.append(child)
  40. def get_parent(self):
  41. return self._parent
  42. def get_header_height(self):
  43. return 0
  44. def select(self):
  45. self._selected = True
  46. def unselect(self):
  47. self._selected = False
  48. def toggle(self):
  49. if self.selected():
  50. self.unselect()
  51. else:
  52. self.select()
  53. def selected(self):
  54. return self._selected
  55. class StaticNode(Node):
  56. def __init__(self, label):
  57. super(StaticNode, self).__init__()
  58. self.label = label
  59. def append_child(self, child):
  60. self._append_child(child)
  61. def get_label(self):
  62. return self.label
  63. class SelectionPad(object):
  64. def __init__(self, parent_window):
  65. self._pad = curses.newpad(1, 1)
  66. self._pad.keypad(1)
  67. def refresh(self, pminrow, sminrow, smaxrow, smaxcol):
  68. # window.refresh([pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol])
  69. # pminrow and pmincol specify the upper left-hand corner of the rectangle to be displayed in the pad.
  70. # sminrow, smincol, smaxrow, and smaxcol specify the edges of the rectangle to be displayed on the screen.
  71. # The lower right-hand corner of the rectangle to be displayed in the pad is calculated from the screen coordinates, since the rectangles must be the same size.
  72. self._pad.refresh(pminrow, 0, sminrow, 0, smaxrow, smaxcol)
  73. def addstr(self, line_index, col_index, text, attr = 0):
  74. text_encoded = text.encode(locale.getpreferredencoding())
  75. try:
  76. self._pad.addstr(line_index, col_index, text_encoded, attr)
  77. except Exception, ex:
  78. raise Exception('pad(width=%d, height=%d).addstr(%d, %d, %s, %d) failed'
  79. % (self.get_width(), self.get_height(), line_index, col_index, repr(text_encoded), attr))
  80. def clear(self):
  81. self._pad.clear()
  82. def get_height(self):
  83. return self._pad.getmaxyx()[0]
  84. def get_width(self):
  85. return self._pad.getmaxyx()[1]
  86. def getch(self):
  87. return self._pad.getch()
  88. def resize(self, nlines, ncols):
  89. try:
  90. self._pad.resize(nlines, ncols)
  91. except Exception, ex:
  92. raise Exception('pad(width=%d, height=%d).resize(%d, %d) failed'
  93. % (self.get_width(), self.get_height(), nlines, ncols))
  94. def resize_height(self, nlines):
  95. self.resize(nlines, self.get_width())
  96. def resize_width(self, ncols):
  97. self.resize(self.get_height(), ncols)
  98. def select(stdscr, active_root, multiple = False):
  99. curses.curs_set(0)
  100. pad = SelectionPad(stdscr)
  101. active_index = 0
  102. def get_screen_height():
  103. return stdscr.getmaxyx()[0]
  104. def get_active_node():
  105. if active_index < len(active_root.get_children()):
  106. return active_root.get_children()[active_index]
  107. else:
  108. return None
  109. def get_visible_pad_height():
  110. return get_screen_height() - active_root.get_header_height()
  111. def refresh():
  112. pad.clear()
  113. pad.resize_width(1)
  114. if len(active_root.get_children()) > 0:
  115. pad.resize_height(active_root.child_count())
  116. for child_index in range(len(active_root.get_children())):
  117. child = active_root.get_children()[child_index]
  118. label = child.get_label()
  119. # if the last line is the longest addstr() fails if
  120. # the width does not include one additional char.
  121. if len(label) > pad.get_width():
  122. pad.resize_width(len(label) + 1)
  123. pad.addstr(
  124. child_index,
  125. 0,
  126. label,
  127. (curses.A_UNDERLINE if child_index == active_index else 0)
  128. | (curses.A_BOLD if child.selected() else 0)
  129. )
  130. pad.refresh(
  131. max(
  132. 0,
  133. min(
  134. active_root.child_count() - get_visible_pad_height(),
  135. active_index - int(get_visible_pad_height() / 2)
  136. )
  137. ),
  138. active_root.get_header_height(),
  139. get_screen_height() - 1,
  140. stdscr.getmaxyx()[1] - 1
  141. )
  142. while True:
  143. refresh()
  144. try:
  145. key = pad.getch()
  146. except KeyboardInterrupt:
  147. return None
  148. active_node = get_active_node()
  149. if key == curses.KEY_RESIZE:
  150. refresh()
  151. elif key in [curses.KEY_DOWN, KEY_SDOWN, ord('j'), ord('J')]:
  152. if multiple and key in [KEY_SDOWN, ord('J')]:
  153. active_node.toggle()
  154. active_index = min(active_root.child_count() - 1, active_index + 1)
  155. elif key in [curses.KEY_UP, KEY_SUP, ord('k'), ord('K')]:
  156. if multiple and key in [KEY_SUP, ord('K')]:
  157. active_node.toggle()
  158. active_index = max(0, active_index - 1)
  159. elif key in [curses.KEY_NPAGE, ord('f') - ord('a') + 1]: # control-f
  160. active_index = min(active_root.child_count() - 1, active_index + int(get_visible_pad_height() / 2))
  161. elif key in [curses.KEY_PPAGE, ord('b') - ord('a') + 1]: # control-b
  162. active_index = max(0, active_index - int(get_visible_pad_height() / 2))
  163. elif active_node and key in [ord(' ')]:
  164. if multiple:
  165. active_node.toggle()
  166. else:
  167. active_node.select()
  168. return active_root.find_root().find_selected()
  169. elif active_node and key in [ord('\n')]:
  170. active_node.select()
  171. return active_root.find_root().find_selected()
  172. elif key in [ord('q'), KEY_ESC]:
  173. return None
  174. # else:
  175. # raise Exception(key)
  176. def select_string(stdscr, strings, multiple = False):
  177. root = StaticNode('root')
  178. for string in strings:
  179. root.append_child(StaticNode(string))
  180. selection = select(stdscr, root, multiple = multiple)
  181. if selection is None:
  182. return None
  183. else:
  184. return [n.get_label() for n in selection]