mirror of
https://github.com/beetbox/beets.git
synced 2026-01-30 03:54:21 +01:00
Introduced input_select_items
alternative and more flexibile implementation to fulfil #1723 Added test case for new input method
This commit is contained in:
parent
6ee7c4f7c7
commit
7bdc7d37d3
2 changed files with 75 additions and 0 deletions
|
|
@ -367,6 +367,31 @@ def input_yn(prompt, require=False):
|
|||
return sel == 'y'
|
||||
|
||||
|
||||
def input_select_items(prompt, items, rep):
|
||||
"""Prompts the user to use all, none or some of the items
|
||||
Will return the list of items the user selected
|
||||
prompt: prompt to use for all and for selective choice
|
||||
items: full list of items
|
||||
rep: function which represents an item to the user
|
||||
is called with the item as argument
|
||||
function is responsive for newline at input
|
||||
"""
|
||||
out_items = []
|
||||
choice = input_options(
|
||||
('y', 'n', 's'), False,
|
||||
'%s (Yes/No/Selective)?' % prompt)
|
||||
print()
|
||||
if choice == 'y':
|
||||
out_items = items
|
||||
elif choice == 's':
|
||||
for item in items:
|
||||
rep(item)
|
||||
if input_yn('%s (y/n)?' % prompt, True):
|
||||
out_items.append(item)
|
||||
print()
|
||||
return out_items
|
||||
|
||||
|
||||
# Human output formatting.
|
||||
|
||||
def human_bytes(size):
|
||||
|
|
|
|||
|
|
@ -21,6 +21,56 @@ from test._common import unittest
|
|||
from beets import ui
|
||||
|
||||
|
||||
class InputMethodsTest(_common.TestCase):
|
||||
def setUp(self):
|
||||
super(InputMethodsTest, self).setUp()
|
||||
self.io.install()
|
||||
|
||||
def _print_helper(self, s):
|
||||
print(s)
|
||||
|
||||
def _print_helper2(self, s, prefix):
|
||||
print(prefix, s)
|
||||
|
||||
def test_input_select_items(self):
|
||||
full_items = ['1', '2', '3', '4', '5']
|
||||
|
||||
# Test no
|
||||
self.io.addinput('n')
|
||||
items = ui.input_select_items(
|
||||
"Prompt", full_items, self._print_helper)
|
||||
self.assertEqual(items, [])
|
||||
|
||||
# Test yes
|
||||
self.io.addinput('y')
|
||||
items = ui.input_select_items(
|
||||
"Prompt", full_items, self._print_helper)
|
||||
self.assertEqual(items, full_items)
|
||||
|
||||
# Test selective 1
|
||||
self.io.addinput('s')
|
||||
self.io.addinput('n')
|
||||
self.io.addinput('y')
|
||||
self.io.addinput('n')
|
||||
self.io.addinput('y')
|
||||
self.io.addinput('n')
|
||||
items = ui.input_select_items(
|
||||
"Prompt", full_items, self._print_helper)
|
||||
self.assertEqual(items, ['2', '4'])
|
||||
|
||||
# Test selective 2
|
||||
self.io.addinput('s')
|
||||
self.io.addinput('y')
|
||||
self.io.addinput('y')
|
||||
self.io.addinput('n')
|
||||
self.io.addinput('y')
|
||||
self.io.addinput('n')
|
||||
items = ui.input_select_items(
|
||||
"Prompt", full_items,
|
||||
lambda s: self._print_helper2(s, "Prefix"))
|
||||
self.assertEqual(items, ['1', '2', '4'])
|
||||
|
||||
|
||||
class InitTest(_common.LibTestCase):
|
||||
def setUp(self):
|
||||
super(InitTest, self).setUp()
|
||||
|
|
|
|||
Loading…
Reference in a new issue