selection_menu.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # Copyright 2022 The HuggingFace Team and Brian Chao. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """
  15. Main driver for the selection menu, based on https://github.com/bchao1/bullet
  16. """
  17. import builtins
  18. import sys
  19. from typing import Optional
  20. from ...utils.imports import _is_package_available
  21. from . import cursor, input
  22. from .helpers import Direction, clear_line, forceWrite, linebreak, move_cursor, reset_cursor, writeColor
  23. from .keymap import KEYMAP
  24. in_colab = False
  25. try:
  26. in_colab = _is_package_available("google.colab")
  27. except ModuleNotFoundError:
  28. pass
  29. @input.register
  30. class BulletMenu:
  31. """
  32. A CLI menu to select a choice from a list of choices using the keyboard.
  33. """
  34. def __init__(self, prompt: Optional[str] = None, choices: list = []):
  35. self.position = 0
  36. self.choices = choices
  37. self.prompt = prompt
  38. if sys.platform == "win32":
  39. self.arrow_char = "*"
  40. else:
  41. self.arrow_char = "➔ "
  42. def write_choice(self, index, end: str = ""):
  43. if sys.platform != "win32":
  44. writeColor(self.choices[index], 32, end)
  45. else:
  46. forceWrite(self.choices[index], end)
  47. def print_choice(self, index: int):
  48. "Prints the choice at the given index"
  49. if index == self.position:
  50. forceWrite(f" {self.arrow_char} ")
  51. self.write_choice(index)
  52. else:
  53. forceWrite(f" {self.choices[index]}")
  54. reset_cursor()
  55. def move_direction(self, direction: Direction, num_spaces: int = 1):
  56. "Should not be directly called, used to move a direction of either up or down"
  57. old_position = self.position
  58. if direction == Direction.DOWN:
  59. if self.position + 1 >= len(self.choices):
  60. return
  61. self.position += num_spaces
  62. else:
  63. if self.position - 1 < 0:
  64. return
  65. self.position -= num_spaces
  66. clear_line()
  67. self.print_choice(old_position)
  68. move_cursor(num_spaces, direction.name)
  69. self.print_choice(self.position)
  70. @input.mark(KEYMAP["up"])
  71. def move_up(self):
  72. self.move_direction(Direction.UP)
  73. @input.mark(KEYMAP["down"])
  74. def move_down(self):
  75. self.move_direction(Direction.DOWN)
  76. @input.mark(KEYMAP["newline"])
  77. def select(self):
  78. move_cursor(len(self.choices) - self.position, "DOWN")
  79. return self.position
  80. @input.mark(KEYMAP["interrupt"])
  81. def interrupt(self):
  82. move_cursor(len(self.choices) - self.position, "DOWN")
  83. raise KeyboardInterrupt
  84. @input.mark_multiple(*[KEYMAP[str(number)] for number in range(10)])
  85. def select_row(self):
  86. index = int(chr(self.current_selection))
  87. movement = index - self.position
  88. if index == self.position:
  89. return
  90. if index < len(self.choices):
  91. if self.position > index:
  92. self.move_direction(Direction.UP, -movement)
  93. elif self.position < index:
  94. self.move_direction(Direction.DOWN, movement)
  95. else:
  96. return
  97. else:
  98. return
  99. def run(self, default_choice: int = 0):
  100. "Start the menu and return the selected choice"
  101. if self.prompt:
  102. linebreak()
  103. forceWrite(self.prompt, "\n")
  104. if in_colab:
  105. forceWrite("Please input a choice index (starting from 0), and press enter", "\n")
  106. else:
  107. forceWrite("Please select a choice using the arrow or number keys, and selecting with enter", "\n")
  108. self.position = default_choice
  109. for i in range(len(self.choices)):
  110. self.print_choice(i)
  111. forceWrite("\n")
  112. move_cursor(len(self.choices) - self.position, "UP")
  113. with cursor.hide():
  114. while True:
  115. if in_colab:
  116. try:
  117. choice = int(builtins.input())
  118. except ValueError:
  119. choice = default_choice
  120. else:
  121. choice = self.handle_input()
  122. if choice is not None:
  123. reset_cursor()
  124. for _ in range(len(self.choices) + 1):
  125. move_cursor(1, "UP")
  126. clear_line()
  127. self.write_choice(choice, "\n")
  128. return choice