def ls(pattern=None): """Show all items that match `pattern`.""" items = item.load() if items: if pattern is not None: # Search for keys that match pattern items = search(items, pattern) if len(items) == 0: return max_len = max([len(key) for key in items]) def format_name(name): return Fore.YELLOW + name + Fore.RESET def spacing(name): return (max_len - len(name)) + KEY_VALUE_SPACING lines = [ '{}{}{}'.format(format_name(name), ' ' * spacing(name), format_value(value)) for name, value in items.items() ] lines.sort() click.echo('\n'.join(lines)) else: click.echo(Style.DIM + 'No items' + Style.NORMAL)
def remove(pattern): """Remove an item.""" items = item.load() to_remove = search(item.load(), pattern) count = len(to_remove) if count == 0: echo_error('No items found') return new_items = { name: value for name, value in items.items() if not name in to_remove } item.save(new_items, items) click.echo('Removed {} item{}'.format(Fore.GREEN + str(count) + Fore.RESET, '' if count == 1 else 's') + Style.RESET_ALL)
def test_search_nonexistant_string_returns_empty_dictionary(): items = {'foo': 'bar'} assert search(items, 'baz') == {}
def test_search_string_returns_exact_match(): items = {'foo': 'bar'} assert search(items, 'foo') == items
def test_search_asterisk_matches_any_string(): items = {'foo': 'baz', 'bar': 'baz'} assert search(items, '*') == items
def test_search_string_with_question_mark_matches_any_character(): items = {'bar': 'foo', 'baz': 'foo'} assert search(items, 'ba?') == items