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

Improved output handling on Windows #196

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions tldr.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from urllib.request import urlopen, Request
from urllib.error import HTTPError, URLError
from termcolor import colored
import colorama # Required for Windows
import colorama
import shtab

__version__ = "3.2.0"
Expand Down Expand Up @@ -496,7 +496,21 @@ def main() -> None:

options = parser.parse_args()

colorama.init(strip=options.color)
isWindows = True if get_platform() == 'windows' else None
if isWindows:
# Sets the ENABLE_VIRTUAL_TERMINAL_PROCESSING flag using the Windows API
from ctypes import windll, wintypes, byref
kernel32 = windll.kernel32
mode = wintypes.DWORD()

for std_id in (-11, -12): # stdout, stderr
hConsoleHandle = kernel32.GetStdHandle(std_id)
kernel32.GetConsoleMode(hConsoleHandle, byref(mode))
if not mode.value & 0x4:
mode.value |= 0x4
kernel32.SetConsoleMode(hConsoleHandle, mode)

colorama.init(strip=options.color, convert=isWindows)
if options.color is False:
os.environ["FORCE_COLOR"] = "true"

Expand Down
Loading