Giter Site home page Giter Site logo

Comments (8)

willmcgugan avatar willmcgugan commented on May 23, 2024 1

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.

taranlu-houzz avatar taranlu-houzz commented on May 23, 2024 1

Yeah, I had assumed that would be the trade-off. Oh well, thanks for confirming!

from rich-cli.

ewels avatar ewels commented on May 23, 2024

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.

willmcgugan avatar willmcgugan commented on May 23, 2024

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.

ewels avatar ewels commented on May 23, 2024

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.

ewels avatar ewels commented on May 23, 2024

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)

out

Ok now I'm off.. 😆 Rest should be easy!

from rich-cli.

taranlu-houzz avatar taranlu-houzz commented on May 23, 2024

@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.

ewels avatar ewels commented on May 23, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.