Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider using rich to format output like exceptions #1216

Open
phrrngtn opened this issue Mar 22, 2022 · 11 comments
Open

Consider using rich to format output like exceptions #1216

phrrngtn opened this issue Mar 22, 2022 · 11 comments

Comments

@phrrngtn
Copy link

I found my usage of Cmd2 to be enhanced by using rich to format exceptions.

class RichCmd(cmd2.Cmd):
    def __init__(self):
        super().__init__()
        self.console=Console()

[-]

    def pexcept(self, msg: Any, *, end: str = '\n', apply_style: bool = True) -> None:
        if self.debug and sys.exc_info() != (None,None,None):
            self.console.print_exception(show_locals=True)
        else:
            super().pexcept(msg,end=end,apply_style=apply_style)
@anselor
Copy link
Contributor

anselor commented Mar 22, 2022

This could easily be turned into a mixin class

@kmvanbrunt
Copy link
Member

Printing exceptions with rich is a nice enhancement, but I'd rather not add rich as a requirement to use cmd2.

@anselor
Copy link
Contributor

anselor commented Apr 13, 2022

I'm going to look into doing a simple mixin for this or add an example, depending on what makes sense.

@anselor anselor reopened this Apr 13, 2022
@anselor
Copy link
Contributor

anselor commented Apr 18, 2022

@phrrngtn I pushed up some initial structure of a rich mixin class for cmd2. Would you mind taking a look? My thought is the console.print() function can be called in poutput().

@phrrngtn
Copy link
Author

I did not notice the feature branch so only just found out what you are referring to. I will exercise it. My most recent deep interaction with Cmd2 was in 2011 or thereabouts. I have just used it as-is since then.

@anselor
Copy link
Contributor

anselor commented Apr 28, 2022

It's a pretty minimal start that just took what you provided and puts it in a mixin. I've not used rich so I'm not really familiar with it but my thought is all of our print functions can be optionally shunted over to rich using this mixin class.

@tleonhardt
Copy link
Member

I personally love rich and think it is fantastic and does a lot of things we have added features for in cmd2.

Right now I'm not sure we want to take a dependency on it, but we should consider doing so in the future.

@anselor Regardless, a mixin with an example of using it could be pretty awesome in the meantime.

@anselor
Copy link
Contributor

anselor commented Sep 9, 2022

@phrrngtn Hey, just checking in again. Did you get a chance to try out the rich mixin branch?

master...1216-rich-print-mixin

Should be able to use it with something like:


class MyCmd(RichCmd, Cmd):
    ...


my_cmd = MyCmd()

@phrrngtn
Copy link
Author

phrrngtn commented Sep 9, 2022 via email

@anselor
Copy link
Contributor

anselor commented Sep 9, 2022

No worries. I've also occasionally checked in on Textual's progress and I would love to be able to run cmd2 as the command interpreter inside of Textual Console UI. I have many project ideas if that can happen.

@anselor
Copy link
Contributor

anselor commented Sep 23, 2022

I think the discussion topic #1132 could also be enabled by integrating cmd2 with Textual.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

When branches are created from issues, their pull requests are automatically linked.

4 participants