Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jarun committed Apr 11, 2019
1 parent eb19c24 commit 3a076b4
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions ddgr
Original file line number Diff line number Diff line change
Expand Up @@ -1878,6 +1878,31 @@ def python_version():
return '%d.%d.%d' % sys.version_info[:3]


def set_win_console_mode():
# VT100 control sequences are supported on Windows 10 Anniversary Update and later.
# https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences
# https://docs.microsoft.com/en-us/windows/console/setconsolemode
if platform.release() == '10':
STD_OUTPUT_HANDLE = -11
STD_ERROR_HANDLE = -12
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004
try:
from ctypes import windll, wintypes, byref
kernel32 = windll.kernel32
for nhandle in (STD_OUTPUT_HANDLE, STD_ERROR_HANDLE):
handle = kernel32.GetStdHandle(nhandle)
old_mode = wintypes.DWORD()
if not kernel32.GetConsoleMode(handle, byref(old_mode)):
raise RuntimeError('GetConsoleMode failed')
new_mode = old_mode.value | ENABLE_VIRTUAL_TERMINAL_PROCESSING
if not kernel32.SetConsoleMode(handle, new_mode):
raise RuntimeError('SetConsoleMode failed')
# Note: No need to restore at exit. SetConsoleMode seems to
# be limited to the calling process.
except Exception:
pass


# Query autocompleter

# This function is largely experimental and could raise any exception;
Expand Down Expand Up @@ -2004,26 +2029,7 @@ def main():

# Try to enable ANSI color support in cmd or PowerShell on Windows 10
if sys.platform == 'win32' and sys.stdout.isatty() and colorize:
# VT100 control sequences are supported on Windows 10 Anniversary Update and later.
# https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences
# https://docs.microsoft.com/en-us/windows/console/setconsolemode
if platform.release() == '10':
STD_OUTPUT_HANDLE = -11
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004
try:
from ctypes import windll, wintypes, byref
kernel32 = windll.kernel32
stdout_handle = kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
old_mode = wintypes.DWORD()
if not kernel32.GetConsoleMode(stdout_handle, byref(old_mode)):
raise RuntimeError('GetConsoleMode failed')
new_mode = old_mode.value | ENABLE_VIRTUAL_TERMINAL_PROCESSING
if not kernel32.SetConsoleMode(stdout_handle, new_mode):
raise RuntimeError('SetConsoleMode failed')
# Note: No need to restore at exit. SetConsoleMode seems to
# be limited to the calling process.
except Exception:
pass
set_win_console_mode()

if opts.url_handler is not None:
open_url.url_handler = opts.url_handler
Expand Down

0 comments on commit 3a076b4

Please sign in to comment.