bluenote10 / yachalk

🖍️ Terminal string styling done right
MIT License
161 stars 3 forks source link

added windows conhost support #20

Closed Mani4D46 closed 2 months ago

Mani4D46 commented 2 months ago

sorry I forgot to add an description I added conhost.exe support using the ctypes module to load the required DLL and set the correct modes to make it support Virtual Terminal Processing

bluenote10 commented 2 months ago

Is this also taken from the original chalk implementation? Where is the name DISABLE_VT_PROCESSING coming from, is this some kind of standard convention?

In general I think that importing a python package/module should ideally be free of side effects, because

That is why I was even reluctant to offer the chalk singleton that is currently initialized at import time. However, that side effect is purely an internal one, and the detect_color_support is reasonably lightweight.

In contrast, the side effect we are talking about here is an external one, which is much more problematic, because it leads to issues like this https://github.com/bluenote10/yachalk/issues/16.

A clean way to deal with external state mutations is to use a context manager, because the change is local, and can be rolled back automatically un-doing the side effect properly. However, chalk is currently not supposed to be used as a context manager, and it probably shouldn't because it would make it very inconvenient to use, and there is not reason on other systems.

What I could imagine is something like this:


from yachalk import chalk, extended_terminal_support

def main():
    # regular code...
    print(chalk.blue("colored"))

def main_wrapped():
    # The `extended_terminal_support` can be used somewhere on an outer level
    # of an app to enable color on "more terminals", which would currently just mean
    # Windows' conhost. Then the side effect is made explicitly, and not necessarily
    # at import time, and the side effect can also be undone thanks to the context
    # manager
    with extended_terminal_support():
        main()

if __name__ == "__main__":
        main_wrapped()
Mani4D46 commented 2 months ago

ctypes is a built-in module and DISABLE_VT_PROCESSING is not a standard naming convention but I'm pretty sure that there's no other standard name. and about side-effects I will fix it in the next pull request I'm going to close this one, i don't think there's any code related to the windows terminal in the original chalk but rich for example has something similar. many people who never worked with the windows API will usually just do os.system('') which will call some windows apis and then return the EXIT_CODE or something idk

Mani4D46 commented 2 months ago

closing...