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

ANSI processing workaround for Windows command prompt #2628

Closed
wants to merge 1 commit into from
Closed

ANSI processing workaround for Windows command prompt #2628

wants to merge 1 commit into from

Conversation

God-damnit-all
Copy link
Contributor

For some reason, this little line of code in Python enables virtual terminal processing for Windows command prompt, allowing it to display ANSI color.

Details can be found in this comment on Stack Overflow: https://stackoverflow.com/questions/287871/how-do-i-print-colored-text-to-the-terminal/293633#comment124648740_54955094

@mikf
Copy link
Owner

mikf commented May 27, 2022

I've also found this:

import ctypes

kernel32 = ctypes.windll.kernel32
kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)

I would prefer using the Windows API if possible. Could you test if this actually works? I only have access to Win7 at the moment and neither method does anything there.

I'd also like to put this functionality behind an option, especially if it were to call os.system("") on each program start:

diff --git a/gallery_dl/__init__.py b/gallery_dl/__init__.py
index d66e285c..104df063 100644
--- a/gallery_dl/__init__.py
+++ b/gallery_dl/__init__.py
@@ -154,6 +154,12 @@ def main():
                 else:
                     signal.signal(signal_num, signal.SIG_IGN)
 
+        # ANSI on Windows
+        if util.WINDOWS and config.get(("output",), "ansi"):
+            import ctypes
+            kernel32 = ctypes.windll.kernel32
+            kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)
+
         # extractor modules
         modules = config.get(("extractor",), "modules")
         if modules is not None:

@rautamiekka
Copy link
Contributor

I only have access to Win7 at the moment

If you can run an x86-32 or x64 VM and get the installer ISO some way (M$ has an official tool to download an official Win10 ISO, and Win10 doesn't have a nearly as restricted trial period as before cuz it never runs out nor limits in such ways), that would work.

@God-damnit-all
Copy link
Contributor Author

I've also found this:

import ctypes

kernel32 = ctypes.windll.kernel32
kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)

I would prefer using the Windows API if possible. Could you test if this actually works? I only have access to Win7 at the moment and neither method does anything there.

I'd also like to put this functionality behind an option, especially if it were to call os.system("") on each program start:

diff --git a/gallery_dl/__init__.py b/gallery_dl/__init__.py
index d66e285c..104df063 100644
--- a/gallery_dl/__init__.py
+++ b/gallery_dl/__init__.py
@@ -154,6 +154,12 @@ def main():
                 else:
                     signal.signal(signal_num, signal.SIG_IGN)
 
+        # ANSI on Windows
+        if util.WINDOWS and config.get(("output",), "ansi"):
+            import ctypes
+            kernel32 = ctypes.windll.kernel32
+            kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)
+
         # extractor modules
         modules = config.get(("extractor",), "modules")
         if modules is not None:

I'll give it a try, but people reported mix success with it so I went with the one everyone seemed to be recommending.

My first instinct was to put it behind an option like you suggested, but it seemed like the code for checking the options came after code that began writing to the console, and I was concerned that could have a negative effect.

@God-damnit-all
Copy link
Contributor Author

Good news, the code seems to work. Though, instead of having an ansi option, it might be better to determine if ANSI is even going to be used to begin with. That would probably have to include checking the output settings defined by #2529 to see if they contain any ANSI escape codes.

Now that you know the code works, this PR is probably unnecessary since you likely have a more elegant implementation in mind than I can provide.

mikf added a commit that referenced this pull request May 29, 2022
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

Successfully merging this pull request may close these issues.

3 participants