diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e24f2006..8b8b0b720 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -131,6 +131,9 @@ prompt is displayed. specific `cmd2.Cmd` subclass (e.g.,`class MyCommandSet(CommandSet[MyApp]):`). This provides full type hints and IDE autocompletion for `self._cmd` without needing to override and cast the property. + - Updated `set` command to consolidate its confirmation output into a single, colorized line. + The confirmation now uses `pfeedback()`, allowing it to be silenced when the `quiet` settable + is enabled. ## 3.5.0 (April 13, 2026) diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 325985bbd..b37de25a0 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -4721,7 +4721,16 @@ def do_set(self, args: argparse.Namespace) -> None: except ValueError as ex: self.perror(f"Error setting {args.param}: {ex}") else: - self.poutput(f"{args.param} - was: {orig_value!r}\nnow: {settable.value!r}") + # Create the feedback message using Rich Text for color + feedback_msg = Text.assemble( + args.param, + ": ", + (f"{orig_value!r}", "red"), + " -> ", + (f"{settable.value!r}", "green"), + ) + self.pfeedback(feedback_msg) + self.last_result = True return diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 330ab97b7..b0fa004ad 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -161,16 +161,17 @@ def test_base_set(base_app) -> None: def test_set(base_app) -> None: - out, _err = run_cmd(base_app, "set quiet True") - expected = normalize( - """ -quiet - was: False -now: True -""" - ) - assert out == expected + out, err = run_cmd(base_app, "set quiet True") + assert not out + assert base_app.last_result is True + + # Test quiet respect + out, err = run_cmd(base_app, "set timing False") + assert not out + assert not err assert base_app.last_result is True + # Show one settable (this always goes to out) line_found = False out, _err = run_cmd(base_app, "set quiet") for line in out: @@ -181,6 +182,7 @@ def test_set(base_app) -> None: assert line_found assert len(base_app.last_result) == 1 assert base_app.last_result["quiet"] is True + base_app.quiet = False def test_set_val_empty(base_app) -> None: @@ -237,8 +239,8 @@ def test_set_allow_style(base_app, new_val, is_valid, expected) -> None: # Verify the results assert expected == ru.ALLOW_STYLE if is_valid: - assert not err - assert out + assert err + assert not out def test_set_with_choices(base_app) -> None: @@ -250,9 +252,10 @@ def test_set_with_choices(base_app) -> None: base_app.add_settable(fake_settable) # Try a valid choice - _out, err = run_cmd(base_app, f"set fake {fake_choices[1]}") + out, err = run_cmd(base_app, f"set fake {fake_choices[1]}") assert base_app.last_result is True - assert not err + assert not out + assert err == [f"fake: {fake_choices[0]!r} -> {fake_choices[1]!r}"] # Try an invalid choice _out, err = run_cmd(base_app, "set fake bad_value") @@ -276,15 +279,10 @@ def onchange_app(): def test_set_onchange_hook(onchange_app) -> None: - out, _err = run_cmd(onchange_app, "set quiet True") - expected = normalize( - """ -You changed quiet -quiet - was: False -now: True -""" - ) - assert out == expected + out, err = run_cmd(onchange_app, "set quiet True") + assert out == ["You changed quiet"] + # quiet: False -> True is not shown because quiet is now True + assert not err assert onchange_app.last_result is True @@ -875,17 +873,13 @@ def test_allow_clipboard(base_app) -> None: def test_base_timing(base_app) -> None: base_app.feedback_to_output = False out, err = run_cmd(base_app, "set timing True") - expected = normalize( - """timing - was: False -now: True -""" - ) - assert out == expected + assert not out + assert err[0] == "timing: False -> True" if sys.platform == "win32": - assert err[0].startswith("Elapsed: 0:00:00") + assert err[1].startswith("Elapsed: 0:00:00") else: - assert err[0].startswith("Elapsed: 0:00:00.0") + assert err[1].startswith("Elapsed: 0:00:00.0") def test_base_debug(base_app) -> None: @@ -899,13 +893,8 @@ def test_base_debug(base_app) -> None: # Set debug true out, err = run_cmd(base_app, "set debug True") - expected = normalize( - """ -debug - was: False -now: True -""" - ) - assert out == expected + assert not out + assert err == ["debug: False -> True"] # Verify that we now see the exception traceback out, err = run_cmd(base_app, "edit") diff --git a/tests/test_commandset.py b/tests/test_commandset.py index 72d31e52e..a9478f997 100644 --- a/tests/test_commandset.py +++ b/tests/test_commandset.py @@ -1103,11 +1103,8 @@ def __init__(self) -> None: # change the value and verify the value changed out, err = run_cmd(app, "set arbitrary_value 10") - expected = """ -arbitrary_value - was: 5 -now: 10 -""" - assert out == normalize(expected) + assert not out + assert err == ["arbitrary_value: 5 -> 10"] out, err = run_cmd(app, "set arbitrary_value") any("arbitrary_value" in line and "10" in line for line in out)