You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using the Cmd.p* output methods from while a rich status spinner is running causes the messages to merge on the terminal.
Environment
Windows 11
Windows Terminal Version: 1.23.20211.0
Python 3.12.2
cmd2-3.2.0
Steps to Reproduce
importtimefromargparseimportNamespacefromcmd2importCmd, with_categoryfromrich.consoleimportConsoleTEST_CATAGORY: str="Test Commands"console=Console()
classMyCmd(Cmd):
@with_category(TEST_CATAGORY)defdo_exemplar(self, args: Namespace):
"""This is what the status is expected to look like."""withconsole.status("Working!"):
for_inrange(10):
console.log("console.log: Hello from cmd!")
time.sleep(0.1)
@with_category(TEST_CATAGORY)defdo_problem(self, args: Namespace):
"""Demonstrate the problem."""withconsole.status("Working!"):
for_inrange(10):
self.pwarning("pwarning: Hello from cmd!")
time.sleep(0.1)
if__name__=="__main__":
MyCmd().cmdloop()
Solutions
I did some digging into rich and cmd2 and could not find a way to insert my own console instance into the cmd2.Cmd for use with the print methods (cmd2.Cmd.p*). If there is an obvious solution, please let me know. I have been a long time user of this package and the new changes integrating rich are very exciting.
One possible fix would be to not create a new console object for each call to the print methods. I am not really sure how easy that will be to change or maintain. There could be a console attribute for the cmd2.Cmd class or cmd2 could manage a global console object like how richdoes.
@ShiftPoster Let me look into a good way to support Rich features where a custom console is needed. One reason we recreate the console each time is because we set certain arguments which are state-based. (e.g. cmd2's current theme and ALLOW_STYLE value).
Most likely the answer will be just to add an optional console argument to the print_to() method.
I think a print_to() console argument would work perfectly, its not really a major issue.
I went down a rabbit hole with the console attribute scenario and had an idea for a context manager to handle the state. If you guys decide to go down the attribute route, hopefully it will be helpful. The test cases are pretty bare, I had trouble wrapping head around existing tests and trying to think of better test cases. Source is here: https://github.com/ShiftPoster/cmd2-poc
@ShiftPoster Sorry, adding the ability to pass a console to print_to() wasn't the best approach.
I've reverted this in cmd2 3.4.0 and actually made it unnecessary.
I moved the cmd2-specific logic from print_to() to Cmd2BaseConsole.print() and Cmd2BaseConsole.log().
Now, assuming you are using a Cmd2BaseConsole-related class, your original pwarning code becomes.
console=ru.Cmd2GeneralConsole(file=sys.stderr)
withconsole.status("Working!"):
for_inrange(10):
console.print("Hello from cmd!", style=Cmd2Style.WARNING)
time.sleep(0.1)
Note: You still can't call the cmd2.Cmd.p* print methods in a console.status() or a console.capture() since these methods are only convenience wrappers meant to quickly print something. However, you still get the same input handling that previously only resided in cmd2.Cmd.print_to().
Activity
github-actions commentedon Feb 19, 2026
🤖 Hi @ShiftPoster, I've received your request, and I'm working on it now! You can track my progress in the logs for more details.
kmvanbrunt commentedon Feb 19, 2026
@ShiftPoster Let me look into a good way to support Rich features where a custom console is needed. One reason we recreate the console each time is because we set certain arguments which are state-based. (e.g. cmd2's current theme and ALLOW_STYLE value).
Most likely the answer will be just to add an optional console argument to the
print_to()method.ShiftPoster commentedon Feb 22, 2026
I think a
print_to()console argument would work perfectly, its not really a major issue.I went down a rabbit hole with the console attribute scenario and had an idea for a context manager to handle the state. If you guys decide to go down the attribute route, hopefully it will be helpful. The test cases are pretty bare, I had trouble wrapping head around existing tests and trying to think of better test cases. Source is here: https://github.com/ShiftPoster/cmd2-poc
AlwaysTwilight commentedon Feb 22, 2026
Agent run
4858c0ef4f894a2aa40cffc271396298started.AlwaysTwilight commentedon Feb 22, 2026
Agent run
4858c0ef4f894a2aa40cffc271396298finished without PR: Baseline pytest failed; aborting runShiftPoster commentedon Feb 24, 2026
I am not sure what that means @AlwaysTwilight
tleonhardt commentedon Feb 24, 2026
@ShiftPoster I have no idea who @AlwaysTwilight is, but they are not affiliated with this project.
tleonhardt commentedon Mar 1, 2026
@ShiftPoster The
cmd23.3.0 release is out that contains a modifiedprint_tothat you can now pass a console object to.ShiftPoster commentedon Mar 2, 2026
Awesome, thank you!
kmvanbrunt commentedon Mar 3, 2026
@ShiftPoster Sorry, adding the ability to pass a console to
print_to()wasn't the best approach.I've reverted this in cmd2 3.4.0 and actually made it unnecessary.
I moved the cmd2-specific logic from
print_to()toCmd2BaseConsole.print()andCmd2BaseConsole.log().Now, assuming you are using a
Cmd2BaseConsole-related class, your originalpwarningcode becomes.Note: You still can't call the
cmd2.Cmd.p*print methods in aconsole.status()or aconsole.capture()since these methods are only convenience wrappers meant to quickly print something. However, you still get the same input handling that previously only resided incmd2.Cmd.print_to().ShiftPoster commentedon Apr 12, 2026
Sounds good, I like being able to maintain consistently formatted output. Ill keep in mind the use case for the
p*methods.