Comments (8)
Ah, I'm afraid rich-cli
won't decode escape sequences at the moment.
Try the following. If you can cat in to this script, and force terminal output, it should capture escape codes.
import sys
from rich.console import Console
from rich.ansi import AnsiDecoder
console = Console(force_terminal=True, color_system="truecolor", highlight=False, record=True)
decoder = AnsiDecoder()
data = sys.stdin.read()
for line in decoder.decode(data):
console.print(line)
console.save_svg("out.svg")
from rich-cli.
Yeah, I had assumed that would be the trade-off. Oh well, thanks for confirming!
from rich-cli.
ps. The reason I'm not generating the SVG within the rich-click code itself (as @apcamargo is here Textualize/rich#2279 (comment)) is that this approach misses any other output besides the rich-click stuff.
For example, @nf-core / tools prints a custom header using rich before going on to the the rest of the code execution..
from rich-cli.
The easist way to output a single SVG would be to share a Console object for everything. But that might be tricky if you have separate stdout and stderr consoles.
I'm guessing you are capturing the output from one console and printing it in another? Rich generates ansi escape sequences but won't render them by default. You could try using Text.from_ansi
to convert the escape sequences back in to something Rich understands. Try this:
console.print(Text.from_ansi(captured_text))
from rich-cli.
The easiest way to output a single SVG would be to share a Console object for everything.
There's stdout and stderr, but worse I'm trying to do this across packages: in the example above there are Console objects from nf-core
and also rich-click
(which it imports and uses). Trying to share a single console object across libraries like this will get very messy.
I'm guessing you are capturing the output from one console and printing it in another?
I tried a few things, I was mostly just trying to pipe into the rich
CLI tool though (hence posting on this repo). For example:
python examples/click/01_simple.py --help 2>&1 | rich - --export-svg test.svg
..where I'm setting force_terminal
within 01_simple.py
to preserve colours. That's how I generated the SVG in my original post. Apologies, I should have pasted the command in my original post (it was late 👀 ).
I also tried capturing output from subprocesses and passing to rich Console objects, but with less success.
Context here is this idea where I want to be able to generate nice colourful SVGs of any arbitrary terminal command output (that screenshot had a literal string, but actually running the command would be better).
from rich-cli.
You're a prince amongst men @willmcgugan, a true code wizard 🧙🏻♂️ Worked like a charm first go!
python examples/click/01_simple.py --help 2>&1 | python from_will.py
(with force_terminal=True
set in the rich-click Console object)
Ok now I'm off.. 😆 Rest should be easy!
from rich-cli.
@ewels Will this also support colorized output when using rich_click.rich_click.USE_MARKDOWN
? I am currently playing around with the script @willmcgugan posted above, but it don't seem to be getting color for the cli output in the svg.
from rich-cli.
@taranlu-houzz - I don't believe that it's possible to have rich markup (colours) within markdown rendering I'm afraid. You have to pick one or the other.
from rich-cli.
Related Issues (20)
- List of every supported filename extensions HOT 2
- Feature request: support setting `--hyperlinks` via the `RICH_HYPERLINKS` environment variable HOT 3
- --width / --max-width in SVG export don't affect the width of the picture HOT 1
- head / tail operations are slow on larger files
- Provide help for pager
- No lexer fo csv HOT 4
- Multiple emoji encoding issues on Windows HOT 3
- Is it possible to format outputs of commands executed? HOT 4
- feature request: pv equivalent progress bar (using --size) feature HOT 4
- Rich capturing mouse movements from stdin
- Rich as a drop-in replacement for "more"/"less" HOT 1
- Reused option name: -j is used for both JSON and emoji options HOT 2
- Unable to install latest textual and rich-cli together
- Align title to the left in markdown HOT 2
- Feature request: Allow tailing a CSV HOT 2
- `rich --print` always adds an extra newline
- How to set background color inside strings should be documented better HOT 1
- Exposing Text.from_ansi in rich-cli HOT 1
- Feature request: horizontal padding HOT 1
- please detect encoding before opening a file
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rich-cli.