Skip to content

Rich Console Status Visual Bug #1578

Closed

Description

@ShiftPoster

Description

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

import time
from argparse import Namespace

from cmd2 import Cmd, with_category
from rich.console import Console

TEST_CATAGORY: str = "Test Commands"
console = Console()


class MyCmd(Cmd):
    @with_category(TEST_CATAGORY)
    def do_exemplar(self, args: Namespace):
        """This is what the status is expected to look like."""
        with console.status("Working!"):
            for _ in range(10):
                console.log("console.log: Hello from cmd!")
                time.sleep(0.1)

    @with_category(TEST_CATAGORY)
    def do_problem(self, args: Namespace):
        """Demonstrate the problem."""
        with console.status("Working!"):
            for _ in range(10):
                self.pwarning("pwarning: Hello from cmd!")
                time.sleep(0.1)


if __name__ == "__main__":
    MyCmd().cmdloop()
Image

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 rich does.

Activity

  1. github-actions commented on Feb 19, 2026

    @github-actions
    Contributor

    🤖 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.

  2. kmvanbrunt commented on Feb 19, 2026

    @kmvanbrunt
    Member

    @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.

  3. added this to the 4.0.0 milestone on Feb 21, 2026
  4. ShiftPoster commented on Feb 22, 2026

    @ShiftPoster
    Author

    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

  5. AlwaysTwilight commented on Feb 22, 2026

    @AlwaysTwilight

    Agent run 4858c0ef4f894a2aa40cffc271396298 started.

  6. AlwaysTwilight commented on Feb 22, 2026

    @AlwaysTwilight

    Agent run 4858c0ef4f894a2aa40cffc271396298 finished without PR: Baseline pytest failed; aborting run

  7. ShiftPoster commented on Feb 24, 2026

    @ShiftPoster
    Author

    I am not sure what that means @AlwaysTwilight

  8. tleonhardt commented on Feb 24, 2026

    @tleonhardt
    Member

    @ShiftPoster I have no idea who @AlwaysTwilight is, but they are not affiliated with this project.

  9. self-assigned this
    on Feb 27, 2026
  10. tleonhardt commented on Mar 1, 2026

    @tleonhardt
    Member

    @ShiftPoster The cmd2 3.3.0 release is out that contains a modified print_to that you can now pass a console object to.

  11. ShiftPoster commented on Mar 2, 2026

    @ShiftPoster
    Author

    Awesome, thank you!

  12. kmvanbrunt commented on Mar 3, 2026

    @kmvanbrunt
    Member

    @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)
    with console.status("Working!"):
        for _ in range(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().

  13. ShiftPoster commented on Apr 12, 2026

    @ShiftPoster
    Author

    Sounds good, I like being able to maintain consistently formatted output. Ill keep in mind the use case for the p* methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementpythonPull requests that update Python code

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions