-
There's a minimal reproducer at the end, taken out of what I'm seeing while working on pypa/pip#10421. Broadly, pip's output infrastructure keeps track of indentation, and we increment the indentation as we go "deeper" into the processing of a package. This means, when presenting errors, I want to have the "rich-renderable error" I'm working on, to be presented in a manner that respects the indentation. Further, in keeping with our existing output behaviour, I'd like to let lines that are too long go through soft wrapping by the user's terminal, to not crop any potential content from our output -- it's the most sensible thing to do for non-interactive users as well (which includes all users of pip, because we do subprocess calls to pip itself). Realistically, what I want to do is put n spaces before each line of the renderable's rendered output. The recommended approach for indenting rich renderables seems to be to wrap them with import sys
from rich.console import Console, ConsoleOptions, RenderResult
from rich.padding import Padding
console = Console(file=sys.stderr, no_color=True, soft_wrap=True)
class MyFancyRenderable:
def __rich_console__(
self, console: Console, options: ConsoleOptions
) -> RenderResult:
yield "This is a normal line."
yield f"This is a {'very '*50} long line."
yield "This is a normal line."
renderable = MyFancyRenderable()
console.rule("plain")
console.print(renderable)
console.rule("padded")
console.print(Padding.indent(renderable, 4))
console.rule("plain ignore-overflow crop-false")
console.print(renderable, overflow="ignore", crop=False)
console.rule("padded ignore-overflow crop-false")
console.print(Padding.indent(renderable, 4), overflow="ignore", crop=False)
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
There isn't a way of indenting with padding that also soft wraps at the moment. Other than manually inserting spaces at the start of the line. This has been requested before, and I'm open to making it a first class concept. How about an indent method on Console which adds to an internal indent counter for the number of spaces (and respects soft_wrap). Something like this: console.indent(1)
console.print("foo")
console.indent(2)
console.print("bar")
foo
foo Add one to indent: console.indent(1) Subtract one from indent (i.e. dedent). console.indent(-1) Set the indent to an absolute value: console.indent(set=3) There should probably also be an indent size on the Console object, which acts like a multiplier. So Thoughts? |
Beta Was this translation helpful? Give feedback.
This would be neat... if you also add a way to explicitly set the level of indentation. Without that, it would not be particularly useful for my usecase: I've got multiple
Console
objects inside logging handlers and the indentation is tracked as a module-level global that affects all of the formatters/handlers. In other words, the object that uses theConsole
object is not involved in keeping track of the indentation and, instead, gets it from a module-level global.All that said, I have a fairly straightforward workaround for this -- following what you said about "manually inserting spaces" -- so I don't think this needs to be a first-class concept, but that's obviously your call. :)
@da…