Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add handling for cow files with custom colors #2

Closed
2br-2b opened this issue Mar 15, 2024 · 4 comments
Closed

Add handling for cow files with custom colors #2

2br-2b opened this issue Mar 15, 2024 · 4 comments

Comments

@2br-2b
Copy link

2br-2b commented Mar 15, 2024

Hello!

First off, thank you for making this library!

Right now, if you use a custom colored cow file (like some of the examples here, the cow will not properly be converted to text.

String output:
image

cowfile: batman.cow

Theoretically, string substitution could be used to create variables and substitute throughout the file... but at that point, you're almost creating a bash shell in python

My proposed solution is to do a simple check in read_dot_cow(). If the line starts with a $ and has an = sign, it should remove all color formatting from the string (or have an option to leave it in) and add it to another dictionary, similar to ESCAPES, and replace all these variables at the end.

@2br-2b
Copy link
Author

2br-2b commented Mar 15, 2024

Another few options:

Turn the cowfiles into silhouettes; there's no way you can get this level of detail in a python string:
image

Remove all the $s:
image

@2br-2b
Copy link
Author

2br-2b commented Mar 15, 2024

On further testing, I don't like many of these ideas. Maybe a silhouette will be the best option, or just ignoring it

@James-Ansley
Copy link
Owner

James-Ansley commented Mar 15, 2024

Hi, thanks for pointing this out. Unfortunately, .cow files are usually nothing more than Perl files or modules. In order to properly handle all cow files, the Perl code would need to actually be run (with Perl) to generate the cow correctly. This means there will be limitations in treating .cow files as Python strings.

In saying that, if you really want to fill in the colours, and the .cow files you want to process are all formatted in the same way as the batman.cow file you linked to (e.g. a bunch of variable declarations for the ansi escape codes which are used in the cow heredoc), then the following code could be used to "fill in" the variables:

import re

from cowsay import read_dot_cow, cowsay

ANSI_PATTERN = re.compile(
    r'(?:^|\n)(\$.+) = "(.+)";'
)

with open("batman.cow", "r") as f:
    replacements = {
        k: v.replace("\\e", "\x1b")
        for k, v in ANSI_PATTERN.findall(f.read())
    }
    f.seek(0)
    cow = read_dot_cow(f, replacements)

print(cowsay("hello", cowfile=cow))

@James-Ansley
Copy link
Owner

Just following up on this. I just released a new version of python-cowsay that adds a CLI and also handles the colourful cow files you mentioned. These files should now be rendered with colours by default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants