Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_cannot_move_beyond_upper_limit(self):
stdin = (key.UP
+ key.UP
+ key.UP
+ key.SPACE
+ key.ENTER)
message = 'Foo message'
variable = 'Bar variable'
choices = ['foo', 'bar', 'bazz']
sys.stdin = StringIO(stdin)
question = questions.Checkbox(variable, message, choices=choices)
sut = ConsoleRender(key_generator=fake_key_generator)
result = sut.render(question)
self.assertEqual(['foo'], result)
def test_cannot_move_beyond_upper_limit(self):
stdin = helper.event_factory(
key.UP,
key.UP,
key.UP,
key.SPACE,
key.ENTER,)
message = 'Foo message'
variable = 'Bar variable'
choices = ['foo', 'bar', 'bazz']
question = questions.Checkbox(variable, message, choices=choices)
sut = ConsoleRender(event_generator=stdin)
result = sut.render(question)
self.assertEqual(['foo'], result)
def test_move_up(self, *m):
with InputContext(readchar.key.UP, "\r"):
args_list = ["foo", "bar"]
selindex = cutie.select(args_list, selected_index=1)
self.assertEqual(selindex, 0)
def test_move_up_over_boundary(self, mock_print):
with InputContext(readchar.key.UP, readchar.key.UP, "\r"):
self.assertFalse(cutie.prompt_yes_or_no("foo"))
self.assertEqual(mock_print.call_args_list[-5:], self.default_no_print_calls)
def test_move_up(self):
stdin = helper.event_factory(key.DOWN, key.UP, key.ENTER)
message = 'Foo message'
variable = 'Bar variable'
choices = ['foo', 'bar', 'bazz']
question = questions.List(variable, message, choices=choices)
sut = ConsoleRender(event_generator=stdin)
result = sut.render(question)
self.assertEqual('foo', result)
def test_cursor_movement(self):
stdin_array = [
'a',
key.UP,
'b',
key.DOWN,
'c',
key.LEFT,
'd',
key.RIGHT,
'e',
key.ENTER,
]
stdin = helper.event_factory(*stdin_array)
message = 'Foo message'
variable = 'Bar variable'
question = questions.Text(variable, message)
sut = ConsoleRender(event_generator=stdin)
def process_input(self, pressed):
if pressed == key.UP:
self.current = max(0, self.current - 1)
return
elif pressed == key.DOWN:
self.current = min(len(self.question.choices) - 1,
self.current + 1)
return
elif pressed == key.SPACE:
if self.current in self.selection:
self.selection.remove(self.current)
else:
self.selection.append(self.current)
elif pressed == key.LEFT:
if self.current in self.selection:
self.selection.remove(self.current)
elif pressed == key.RIGHT:
if self.current not in self.selection:
def select(
options: List[str],
selected_index: int = 0) -> int:
print('\n' * (len(options) - 1))
while True:
print(f'\033[{len(options) + 1}A')
for i, option in enumerate(options):
print('\033[K{}{}'.format(
'\033[1m[\033[32;1m x \033[0;1m]\033[0m ' if i == selected_index else
'\033[1m[ ]\033[0m ', option))
keypress = readchar.readkey()
if keypress == readchar.key.UP:
new_index = selected_index
while new_index > 0:
new_index -= 1
selected_index = new_index
break
elif keypress == readchar.key.DOWN:
new_index = selected_index
while new_index < len(options) - 1:
new_index += 1
selected_index = new_index
break
elif keypress == readchar.key.ENTER:
break
elif keypress == readchar.key.CTRL_C:
raise KeyboardInterrupt
return selected_index
for index, target in enumerate(targets):
# Only print the items that are within the visible range.
# Due to lines changing their position on the screen when scrolling,
# we need to redraw the entire line + add padding to make sure all
# traces of the previous line are erased.
if index in visible_target_range:
line = (digits_format_specifier + '. %s ') % (index + 1, target['desc'].ljust(longest_line))
if index == selected_target:
puts(colored.green(' -> %s' % line))
else:
puts(colored.white(' %s' % line))
# Hang until we get a keypress
key = readchar.readkey()
if key == readchar.key.UP or key == 'k' and num_targets > 0:
# Ensure the new selection would be valid & reset number input buffer
if (selected_target - 1) >= 0:
selected_target -= 1
number_buffer = []
elif key == readchar.key.DOWN or key == 'j' and num_targets > 0:
# Ensure the new selection would be valid & reset number input buffer
if (selected_target + 1) <= (num_targets - 1):
selected_target += 1
number_buffer = []
elif key == 'g':
# Go to top & reset number input buffer
selected_target = 0
number_buffer = []
def process_input(self, pressed):
question = self.question
if pressed == key.UP:
if question.carousel and self.current == 0:
self.current = len(question.choices) - 1
else:
self.current = max(0, self.current - 1)
return
if pressed == key.DOWN:
if question.carousel and self.current == len(question.choices) - 1:
self.current = 0
else:
self.current = min(
len(self.question.choices) - 1,
self.current + 1
)
return
if pressed == key.ENTER:
value = self.question.choices[self.current]