diff --git a/sopel/cli/plugins.py b/sopel/cli/plugins.py index 4212b2c15e..a936bcfe93 100644 --- a/sopel/cli/plugins.py +++ b/sopel/cli/plugins.py @@ -336,15 +336,12 @@ def _handle_disable_plugin(settings, plugin_name, force): def display_unknown_plugins(unknown_plugins): # at least one of the plugins does not exist - unknown_count = len(unknown_plugins) - if unknown_count == 1: - tools.stderr('No plugin named %s.' % unknown_plugins[0]) - elif unknown_count == 2: - tools.stderr('No plugin named %s or %s.' % unknown_plugins) - else: - left = ', '.join(unknown_plugins[:-1]) - last = unknown_plugins[-1] - tools.stderr('No plugin named %s, or %s.' % (left, last)) + tools.stderr(utils.get_many_text( + unknown_plugins, + one='No plugin named {item}.', + two='No plugin named {first} or {second}.', + many='No plugin named {left}, or {last}.' + )) def handle_disable(options): @@ -393,15 +390,12 @@ def handle_disable(options): return 0 # nothing to disable or save, but not an error case # display plugins actually disabled by the command - plugins_count = len(actually_disabled) - if plugins_count == 1: - print('Plugin %s disabled.' % actually_disabled[0]) - elif plugins_count == 2: - print('Plugin %s and %s disabled.' % actually_disabled) - else: - left = ', '.join(actually_disabled[:-1]) - last = actually_disabled[-1] - print('Plugin %s, and %s disabled.' % (left, last)) + print(utils.get_many_text( + actually_disabled, + one='Plugin {item} disabled.', + two='Plugin {first} and {second} disabled.', + many='Plugin {left}, and {last} disabled.' + )) return 0 @@ -481,16 +475,12 @@ def handle_enable(options): return 0 # nothing to disable or save, but not an error case # display plugins actually disabled by the command - plugins_count = len(actually_enabled) - if plugins_count == 1: - print('Plugin %s enabled.' % actually_enabled[0]) - elif plugins_count == 2: - print('Plugin %s and %s enabled.' % actually_enabled) - else: - left = ', '.join(actually_enabled[:-1]) - last = actually_enabled[-1] - print('Plugin %s, and %s enabled.' % (left, last)) - + print(utils.get_many_text( + actually_enabled, + one='Plugin {item} enabled.', + two='Plugin {first} and {second} enabled.', + many='Plugin {left}, and {last} enabled.' + )) return 0 diff --git a/sopel/cli/utils.py b/sopel/cli/utils.py index 0d081d4fab..afe9f461dc 100644 --- a/sopel/cli/utils.py +++ b/sopel/cli/utils.py @@ -339,3 +339,23 @@ def redirect_outputs(settings, is_quiet=False): logfile = os.path.os.path.join(settings.core.logdir, settings.basename + '.stdio.log') sys.stderr = tools.OutputRedirect(logfile, True, is_quiet) sys.stdout = tools.OutputRedirect(logfile, False, is_quiet) + + +def get_many_text(items, one, two, many): + """Get the right text based on the number of ``items``.""" + message = '' + if not items: + return message + + items_count = len(items) + + if items_count == 1: + message = one.format(item=items[0], items=items) + elif items_count == 2: + message = two.format(first=items[0], second=items[1], items=items) + else: + left = ', '.join(items[:-1]) + last = items[-1] + message = many.format(left=left, last=last, items=items) + + return message diff --git a/test/cli/test_cli_utils.py b/test/cli/test_cli_utils.py index 648bcae62f..7653147a5e 100644 --- a/test/cli/test_cli_utils.py +++ b/test/cli/test_cli_utils.py @@ -8,7 +8,12 @@ import pytest -from sopel.cli.utils import enumerate_configs, find_config, add_common_arguments +from sopel.cli.utils import ( + add_common_arguments, + enumerate_configs, + find_config, + get_many_text, +) @contextmanager @@ -124,3 +129,22 @@ def test_add_common_arguments_subparser(): options = parser.parse_args(['sub', '--config', 'test-long']) assert options.config == 'test-long' + + +MANY_TEXTS = ( + ([], ''), + (['a'], 'the a element'), + (['a', 'b'], 'elements a and b'), + (['a', 'b', 'c'], 'elements a, b, and c'), + (['a', 'b', 'c', 'd'], 'elements a, b, c, and d'), +) + + +@pytest.mark.parametrize('items, expected', MANY_TEXTS) +def test_get_many_text(items, expected): + result = get_many_text( + items, + 'the {item} element', + 'elements {first} and {second}', + 'elements {left}, and {last}') + assert result == expected