question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there a way to do this like textwrap.fill does?

See original GitHub issue

I am wondering if there is a way to do this in rich (sort of how textwrap does):

MyStr = textwrap.fill(line, width=xxx, initial_indent=“”, subsequent_indent=" ")

I love how rich handles the width of the screen, especially with colors, but it doesn’t have an option for subsequent wraps.

I’d like the output to look something like this:

definition:      this is a short definition
another here:    this is a longer definition of 
     things here, that just keeps on going
     and going 
some other term: you get the idea now

Rather than what I get now:


definition:      this is a short definition
another here:    this is a longer definition of 
things here, that just keeps on going and going 
some other term: you get the idea now

Colors help, but I would like the extra indent to differentiate as well.

A way around this would be if there was a function that would return the (colorized) string WITHOUT printing it. Then I could call the textwrap functions myself. I looked, and if there is one, I missed it.

Anyway, thanks for the great software. Really like using it!

Thanks! Mike

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
legacy-codecommented, Sep 4, 2020

@willmcgugan Thanks for the info.

That’s unfortunate with textwrap though. Good to know that it can be done though!

I do like the solution with the table/grid solutions. Those worked great!

Thanks for the quick turnaround!

1reaction
willmcgugancommented, Sep 4, 2020

You can render to a string with the following pattern:

console = Console(file=io.StringIO(), force_terminal=True)
console.print(text)
table_str = console.file.getvalue()

Unfortunately you won’t be able to use the result with textwrap, which won’t handle the invisible control codes correctly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

textwrap — Text wrapping and filling — Python 3.11.1 ...
wrap() , fill() and shorten() work by creating a TextWrapper instance and calling a single method on it. That instance is not reused,...
Read more >
How do textwrap.wrap() & textwrap.fill() exactly work in python ...
Wraps the single paragraph in text (a string) so every line is at most width characters long. Returns a list of output lines,...
Read more >
Textwrap – Text wrapping and filling in Python - GeeksforGeeks
This function wraps the input paragraph such that each line in the paragraph is at most width characters long. The wrap method returns...
Read more >
Wrap and truncate a string with textwrap in Python - nkmk note
Wrap a string: wrap() , fill() ... With the wrap() function of the textwrap module, you can get a list divided by word...
Read more >
Python Text Wrapping and Filling - Tutorialspoint
The fill() method is similar to the wrap method, but it does not generate a list. It generates a string. It adds the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found