cmd2
cmd2 copied to clipboard
Consider using rich to format output like exceptions
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)
This could easily be turned into a mixin class
Printing exceptions with rich is a nice enhancement, but I'd rather not add rich as a requirement to use cmd2.
I'm going to look into doing a simple mixin for this or add an example, depending on what makes sense.
@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().
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.
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.
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.
@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()
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
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.
I think the discussion topic #1132 could also be enabled by integrating cmd2 with Textual.