fhpythonutils / ansitoimg Goto Github PK
View Code? Open in Web Editor NEWConvert an ansi string to an image. Great for adding terminal output into a readme.
License: MIT License
Convert an ansi string to an image. Great for adding terminal output into a readme.
License: MIT License
Please add a parameter for width + height for larger ansi art, as the terminal size limits it and makes the project unusable as a library.
Here is where the width + height should be determined by the user: https://github.com/FHPythonUtils/AnsiToImg/blob/master/ansitoimg/render.py#L25-L28
Here's an example of the issue:
Parsing the following test results in ValueError: invalid literal for int() with base 10: '3;96'
\x1b[34m╭─\x1b[0m\x1b[34m────────────────────────────────────────────────────────────────────── \x1b[0m\x1b[1;34m<\x1b[0m\x1b[1;95mclass\x1b[0m\x1b[39m \x1b[0m\x1b[32m'jarvis.config.JarvisConfig'\x1b[0m\x1b[1;34m>\x1b[0m\x1b[34m ──────────────────────────────────────────────────────────────────────\x1b[0m\x1b[34m─╮\x1b[0m
\x1b[34m│\x1b[0m \x1b[3;96mclass \x1b[0m\x1b[1;31mJarvisConfig\x1b[0m\x1b[1m(\x1b[0m*args: list, **kwargs: dict\x1b[1m)\x1b[0m:
\x1b[34m│\x1b[0m
\x1b[34m│\x1b[0m
\x1b[34m│\x1b[0m
\x1b[34m│\x1b[0m \x1b[3;33mOPTIONAL\x1b[0m = \x1b[1m{\x1b[0m\x1b[32m'sync'\x1b[0m: \x1b[3;91mFalse\x1b[0m, \x1b[32m'log_level'\x1b[0m: \x1b[32m'WARNING'\x1b[0m, \x1b[32m'cogs'\x1b[0m: \x1b[3;35mNone\x1b[0m, \x1b[32m'events'\x1b[0m: \x1b[3;92mTrue\x1b[0m, \x1b[32m'gitlab_token'\x1b[0m: \x1b[3;35mNone\x1b[0m, \x1b[32m'max_messages'\x1b[0m: \x1b[1;36m1000\x1b[0m, \x1b[32m'twitter'\x1b[0m: \x1b[3;35mNone\x1b[0m, \x1b[32m'reddit'\x1b[0m: \x1b[3;35mNone\x1b[0m, \x1b[32m'rook_token'\x1b[0m: \x1b[3;35mNone\x1b[0m\x1b[1m}\x1b[0m \x1b[34m│\x1b[0m
\x1b[34m│\x1b[0m \x1b[3;33mREQUIRED\x1b[0m = \x1b[1m(\x1b[0m\x1b[32m'token'\x1b[0m, \x1b[32m'mongo'\x1b[0m, \x1b[32m'urls'\x1b[0m, \x1b[32m'redis'\x1b[0m\x1b[1m)\x1b[0m \x1b[34m│\x1b[0m
\x1b[34m│\x1b[0m \x1b[3;33mfrom_yaml\x1b[0m = \x1b[3;96mdef \x1b[0m\x1b[1;31mfrom_yaml\x1b[0m\x1b[1m(\x1b[0mfilepath: Union\x1b[1m[\x1b[0mpathlib.Path, str\x1b[1m]\x1b[0m = \x1b[1;35mWindowsPath\x1b[0m\x1b[1m(\x1b[0m\x1b[32m'config.yaml'\x1b[0m\x1b[1m)\x1b[0m\x1b[1m)\x1b[0m -> \x1b[32m'Config'\x1b[0m: \x1b[2mLoad the yaml config file.\x1b[0m \x1b[34m│\x1b[0m
\x1b[34m│\x1b[0m \x1b[3;33minit\x1b[0m = \x1b[3;96mdef \x1b[0m\x1b[1;31minit\x1b[0m\x1b[1m(\x1b[0mself, **kwargs: dict\x1b[1m)\x1b[0m -> \x1b[3;35mNone\x1b[0m: \x1b[2mInitialize the object.\x1b[0m \x1b[34m│\x1b[0m
\x1b[34m│\x1b[0m \x1b[3;33mreload\x1b[0m = \x1b[3;96mdef \x1b[0m\x1b[1;31mreload\x1b[0m\x1b[1m(\x1b[0m\x1b[1m)\x1b[0m -> bool: \x1b[2mReload the config.\x1b[0m \x1b[34m│\x1b[0m
\x1b[34m╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\x1b[0m
The error occurs in ansitoimg.utils
on line 81. I think this is due to incomplete parsing of ansi modifiers like bold, italic, etc., or at least improper checking for these
A proper render of the text, similar to the following:
The aforementioned error
As I was thinking more about #10, I realized that I'd prefer to not spend any space
in the README on the titlebar of the terminal (and thus not have any title).
This is similar to how your own demo in https://github.com/FHPythonUtils/AnsiToImg/blob/master/README.md#example-output currently has titles removed.
I would like the option to set title=None
in the API and a --no-title
option in the CLI
that removes the titlebar entirely.
It's likely possible to crop the image like suggested in #7 (comment), but I'd like a solution that doesn't depend on knowing the exact image size.
Think it's a good idea to add a real example into README, how to use it.
Something like this:
pipx install ansitoimg
ls --color=always /etc | ansitoimg ls.svg
git diff --color=always | ansitoimg diff.svg
Have the ability to choose width size from the CLI tool.
Now that #4 is fixed thanks to fd1666e, it should be easy to replace the --wide
option that just make the fixed width move from 49 to 89 pixel with --width <size>
where we would be able to provide a custom width.
--wide, -w Use a 'wide' virtual terminal (89 chars vs 49)
AnsiToImg (courtesy of Rich)
)add a --titlebar <text>
option that override the hardcoded watermark
I get:
$ poetry add ansitoimg@latest
$ poetry install
$ poetry run -- ansitoimg
Traceback (most recent call last):
File "/home/ilyagr/.cache/pypoetry/virtualenvs/jj-docs-ZVbGoaMc-py3.11/bin/ansitoimg", line 5, in <module>
from ansitoimg import cli
File "/home/ilyagr/.cache/pypoetry/virtualenvs/jj-docs-ZVbGoaMc-py3.11/lib/python3.11/site-packages/ansitoimg/__init__.py", line 10, in <module>
from ansitoimg.render import (
File "/home/ilyagr/.cache/pypoetry/virtualenvs/jj-docs-ZVbGoaMc-py3.11/lib/python3.11/site-packages/ansitoimg/render.py", line 15, in <module>
from ansitoimg.utils import TEXT_HEIGHT, TEXT_WIDTH, TITLE, WIDTH_DEFAULT, _resolveWidth
ImportError: cannot import name '_resolveWidth' from 'ansitoimg.utils' (/home/ilyagr/.cache/pypoetry/virtualenvs/jj-docs-ZVbGoaMc-py3.11/lib/python3.11/site-packages/ansitoimg/utils.py)
I don't get such problems if I poetry add [email protected]
.
OS: Linux dragon 5.15.124-20253-g52415d073cb4 #1 SMP PREEMPT Mon Aug 21 18:50:56 PDT 2023 x86_64 GNU/Linux
Python:
$ poetry run -- python --version
Python 3.11.4
$ poetry --version
Poetry (version 1.5.1)
#7 seems to have addressed this for the library invocation of ansitoimg, but could you also add a --title
version to the CLI?
I may send a PR for this soon, if you are interested.
A more minor suggestion: the default title made me think your name is Rich. I didn't make the connection to the Rich library. Perhaps something like "colors powered by Rich" or "powered by Rich for Python" or "powered by rich
" (with backticks) would be better.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.