cmd2 icon indicating copy to clipboard operation
cmd2 copied to clipboard

Consider using rich to format output like exceptions

Open phrrngtn opened this issue 2 years ago • 11 comments

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)

phrrngtn avatar Mar 22 '22 03:03 phrrngtn

This could easily be turned into a mixin class

anselor avatar Mar 22 '22 03:03 anselor

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

kmvanbrunt avatar Apr 13 '22 18:04 kmvanbrunt

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

anselor avatar Apr 13 '22 19:04 anselor

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

anselor avatar Apr 18 '22 20:04 anselor

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.

phrrngtn avatar Apr 28 '22 21:04 phrrngtn

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.

anselor avatar Apr 28 '22 21:04 anselor

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.

tleonhardt avatar Jul 14 '22 17:07 tleonhardt

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

https://github.com/python-cmd2/cmd2/compare/master...1216-rich-print-mixin

Should be able to use it with something like:


class MyCmd(RichCmd, Cmd):
    ...


my_cmd = MyCmd()

anselor avatar Sep 09 '22 14:09 anselor

Sorry but I have done absolutely nothing on this! There is a heck of a lot of active development on Textual, a TUI framework by the people who did rich and I have been waiting for that to be released before I start digging into this (because one of my goals will be to run Cmd2 inside a widget in the TUI)

I feel a bit bad about raising this issue and then doing nothing about it but I hope I will be able to provide some answers as a by-product of the TUI work.

thanks for checking in. pjjH

phrrngtn avatar Sep 09 '22 16:09 phrrngtn

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 avatar Sep 09 '22 16:09 anselor

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

anselor avatar Sep 23 '22 14:09 anselor