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 ability to convert strings without writing to console #311

Open
BD103 opened this issue Aug 3, 2021 · 4 comments
Open

Add ability to convert strings without writing to console #311

BD103 opened this issue Aug 3, 2021 · 4 comments

Comments

@BD103
Copy link

BD103 commented Aug 3, 2021

Creating a convert(string: str) global function would make certain programs easier.

I usually code Colorama into a Python package for Windows support. I find that I have to write an initialize script in __init__.py because it seems unnecessary to init() and deinit() every time. This convert function would fix that.

  • It wouldn't rewrite sys.stdout / err
  • It wouldn't be a part of a class, though it might implement AnsiToWin32
  • It would only take one required argument, string (this leaves room for configuration)

Some examples of this used:

from colorama import convert

def cli():
  print(convert("\033[31mHello, world!\033[0m"))

if __name__ == "__main__":
  cli()
@mara004
Copy link

mara004 commented Sep 3, 2021

Surely sounds like a good idea to me, but that would of course be a completely different, compatibility-breaking approach that would mean more or less a rewrite of colorama. I'm not sure whether a completely new project wouldn't be more suitable for this.

As a design consideration, I'd rather name the method colourise, coloured or similar, since convert is too general and does not hint enough at the fact that it's about colouring/highlighting messages.

@BD103
Copy link
Author

BD103 commented Sep 3, 2021

Yes. I just realized a few days ago that ANSI codes work on Windows 10, though I can't get them to work for the Python Repl. Do you know the escape pattern that would work?

@mara004
Copy link

mara004 commented Sep 3, 2021

ANSI codes working on the classical Windows 10 cmd is new to me. I thought that would require Windows-specific API calls? -- Does colorama work with Vistual Studio REPL?

@mara004
Copy link

mara004 commented Sep 3, 2021

I find that I have to write an initialize script in __init__.py because it seems unnecessary to init() and deinit() every time.

Yeah, putting the colorama.init() call in an __init__.py file is the right approach. Calling init() and deinit() repeatedly would be wasteful and is not how it's intended.

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