-
-
Notifications
You must be signed in to change notification settings - Fork 8
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
feat: Support rich markup (instead of Markdown) in HelpFormatter.default_format
?
#192
Comments
This is somewhat more complicated because the default value string is being templated inside of a larger string. Or i guess not more complicated if you, for example, expected seems like it'll have to string format some sentinel default value into the string then split on the sentinel, yielding Although the same logic would apply to |
I suppose it's reasonable to let the user choose between markup/markdown. That would make the code base more consistent (instead of having some text rendered as markdown and some other as markup). How would you detect one or the other though? Surely |
Relevant: Textualize/rich#1249, Textualize/rich#1272. |
I dont know that i would detect markdown/markup string-wise. moreso i think it would be fair to accept Text/Markdown objects where strings are currently allowed, and help/show_default would be wrapped in Markdown if a string. The only way i can think of to compose this with rich's API as-is would be
I think after all that, the default behavior would remain consistent with how it works today, while still ultimately enabling what you're suggesting, and enabling more custom stuff like your |
Another relevant discussion: Textualize/rich#1951. See how the author manages to combine different renderables into one (Markdown and markup): ewels/rich-click@6309974 and ewels/rich-click@a004ad4 using
|
I managed to get something working with the following: help_formatter = cappa.HelpFormatter(
arg_format=(
"{help}",
"{choices}",
lambda default, **_: default and Markdown(default, style="dim italic"),
),
default_format="Default: {default}.",
) In if callable(format_segment):
segment = format_segment(**context)
else:
segment = format_segment.format(**context)
if segment:
if isinstance(segment, str):
segment = Markdown(segment)
segments.append(segment)
return segments ...passing whole context to callable instead of just table.add_row(
Padding(format_arg_name(arg, ", "), help_formatter.left_padding),
Columns(format_arg(help_formatter, arg)),
) (there might be a parameter on Columns that would prevent new lines between elements, see screenshot below) Not bad 😄 |
My solution might be too specific to my use-case. The UUID splitting approach might yield a more consistent API. Anyway, let me know if you want a diff 🙂 |
Got something working to prevent newlines: Textualize/rich#3603. |
i'm imagining that it needs to format then split because of the possibility of arbitrary text before/after the actual format specifier. Per your original example: fwiw, i think it looks nice (maybe even better) for at least your particular set of screenshotted arguments on newlines, lol. Also kinda makes me want to set the default format to be dim by default. |
Of course, whatever is best in terms of API and backwards compatibility Yeah About newlines vs no newlines, yeah, I'm unsure which one I prefer 😄 |
I went down somewhat of a rabbit hole on my original take, and i'm not sure it's feasible. I guess...maybe so your code would or could look something like help_formatter = cappa.HelpFormatter(
default_part = HelpPart.default(style="dim italic")
) and there'd arg_format would maybe be like... this is also very conceptual in my head and i'm not sure if i'm explaining it intelligbly or if it will work either |
Yeah I thought this might prove complicated to support in a robust/elegant way. Honestly I'd be fine with leaving out the italic if you just end up dimming the default string by default. Otherwise the |
Just like we can do:
...I was wondering if it would make sense to allow such markup in
HelpFormatter.default_format
too, for example:Note that the default value themselves should still be rendered as Markdown, for example:
The reason
cappa.Output
andcappa.HelpFormatter
can use rich markup whileshow_default
should only use Markdown is that the former two are exclusively displayed in the terminal, while the latter can be programmatically rendered into other formats (HTML page describing the CLI for example).Not sure if it's possible to combine both. I mean it sounds like it should work if default values (
show_default
) are rendered in isolation as Markdown, then the whole default (formatter) is rendered in isolation as markup, but that might not be the case (both concatenated/formatted as a single string then rendered as Markdown).The text was updated successfully, but these errors were encountered: