<commit_before>from __future__ import unicode_literals

from ctypes import windll, byref, ArgumentError, c_char, c_long, c_ulong, c_uint, pointer
from ctypes.wintypes import DWORD

from prompt_toolkit.renderer import Output
from prompt_toolkit.styles import ANSI_COLOR_NAMES
from prompt_toolkit.win32_types import CONSOLE_SCREEN_BUFFER_INFO, STD_OUTPUT_HANDLE, STD_INPUT_HANDLE, COORD, SMALL_RECT

import os
import six

__all__ = (
    'Win32Output',
)


def _coord_byval(coord):
    """
    Turns a COORD object into a c_long.
    This will cause it to be passed by value instead of by reference. (That is what I think at least.)

    When runing ``ptipython`` is run (only with IPython), we often got the following error::

         Error in 'SetConsoleCursorPosition'.
         ArgumentError("argument 2: <class 'TypeError'>: wrong type",)
     argument 2: <class 'TypeError'>: wrong type

    It was solved by turning ``COORD`` parameters into a ``c_long`` like this.

    More info: http://msdn.microsoft.com/en-us/library/windows/desktop/ms686025(v=vs.85).aspx
    """
    return c_long(coord.Y * 0x10000 | coord.X & 0xFFFF)


#: If True: write the output of the renderer also to the following file. This
#: is very useful for debugging. (e.g.: to see that we don't write more bytes
#: than required.)
_DEBUG_RENDER_OUTPUT = False
_DEBUG_RENDER_OUTPUT_FILENAME = r'prompt-toolkit-windows-output.log'


class NoConsoleScreenBufferError(Exception):
    """
    Raised when the application is not running inside a Windows Console, but
    the user tries to instantiate Win32Output.
    """
    def __init__(self):
        # Are we running in 'xterm' on Windows, like git-bash for instance?
        xterm = 'xterm' in os.environ.get('TERM')

        if xterm:
            message = ('Found %s, while expecting a Windows console. '
                       'Maybe try to run this program using "winpty" '
                       'or run it in cmd.exe instead. Or otherwise, '
                       'in case of Cygwin, use the Python executable '
                       'that is compiled for Cygwin.' % os.environ['TERM'])
        else:
            message = 'No Windows console found. Are you running cmd.exe?'
        super(NoConsoleScreenBufferError, self).__init__(message)


class Win32Output(Output):
    """
    I/O abstraction for rendering to Windows consoles.
    (cmd.exe and similar.)
    """
    def __init__(self, stdout, use_complete_width=False):
        self.use_complete_width = use_complete_width

        self._buffer = []
        self.stdout = stdout
        self.hconsole = windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)

        self._in_alternate_screen = False
<commit_msg>Handle bug in Windows when the TERM environment variable is not defined.<commit_after>from __future__ import unicode_literals

from ctypes import windll, byref, ArgumentError, c_char, c_long, c_ulong, c_uint, pointer
from ctypes.wintypes import DWORD

from prompt_toolkit.renderer import Output
from prompt_toolkit.styles import ANSI_COLOR_NAMES
from prompt_toolkit.win32_types import CONSOLE_SCREEN_BUFFER_INFO, STD_OUTPUT_HANDLE, STD_INPUT_HANDLE, COORD, SMALL_RECT

import os
import six

__all__ = (
    'Win32Output',
)


def _coord_byval(coord):
    """
    Turns a COORD object into a c_long.
    This will cause it to be passed by value instead of by reference. (That is what I think at least.)

    When runing ``ptipython`` is run (only with IPython), we often got the following error::

         Error in 'SetConsoleCursorPosition'.
         ArgumentError("argument 2: <class 'TypeError'>: wrong type",)
     argument 2: <class 'TypeError'>: wrong type

    It was solved by turning ``COORD`` parameters into a ``c_long`` like this.

    More info: http://msdn.microsoft.com/en-us/library/windows/desktop/ms686025(v=vs.85).aspx
    """
    return c_long(coord.Y * 0x10000 | coord.X & 0xFFFF)


#: If True: write the output of the renderer also to the following file. This
#: is very useful for debugging. (e.g.: to see that we don't write more bytes
#: than required.)
_DEBUG_RENDER_OUTPUT = False
_DEBUG_RENDER_OUTPUT_FILENAME = r'prompt-toolkit-windows-output.log'


class NoConsoleScreenBufferError(Exception):
    """
    Raised when the application is not running inside a Windows Console, but
    the user tries to instantiate Win32Output.
    """
    def __init__(self):
        # Are we running in 'xterm' on Windows, like git-bash for instance?
        xterm = 'xterm' in os.environ.get('TERM', '')

        if xterm:
            message = ('Found %s, while expecting a Windows console. '
                       'Maybe try to run this program using "winpty" '
                       'or run it in cmd.exe instead. Or otherwise, '
                       'in case of Cygwin, use the Python executable '
                       'that is compiled for Cygwin.' % os.environ['TERM'])
        else:
            message = 'No Windows console found. Are you running cmd.exe?'
        super(NoConsoleScreenBufferError, self).__init__(message)


class Win32Output(Output):
    """
    I/O abstraction for rendering to Windows consoles.
    (cmd.exe and similar.)
    """
    def __init__(self, stdout, use_complete_width=False):
        self.use_complete_width = use_complete_width

        self._buffer = []
        self.stdout = stdout
        self.hconsole = windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)

        self._in_alternate_screen = False

{'fraction_non_alphanumeric': 0.08612790910756826, 'fraction_numerical': 0.009895547003848268, 'mean_word_length': 3.042962962962963, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5094006', 'n_tokens_mistral': 1693, 'n_tokens_neox': 1587, 'n_words': 545}