<commit_before># -*- encoding: utf-8
"""
Curses user interface.
"""

import os
import logging

from .prompt import Prompt, PromptBool, PromptFile
from .key_mappings import key_map

# Curses can't be imported yet but we'll
# predefine it to avoid confusing flake8
curses = None


class InputEvent:
    """Represents a keyboard or mouse event."""
    def __init__(self):
        self.type = None  # 'key' or 'mouse'
        self.key_name = None
        self.key_code = None
        self.is_typeable = False
        self.curses_key_name = None
        self.mouse_code = None
        self.mouse_pos = (0, 0)
        self.logger = logging.getLogger("{0}.InputEvent".format(__name__))

    def parse_key_code(self, key_code):
        """Parse a key code (or character) from curses."""
        self.type = "key"
        self.key_code = key_code
        self.key_name = self._key_name(key_code)
        self.curses_key_name = self._curses_key_name(key_code)

    def set_key_name(self, name):
        """Manually set the event key name."""
        self.type = "key"
        self.key_name = name

    def parse_mouse_state(self, state):
        """Parse curses mouse events."""
        self.type = "mouse"
        self.mouse_code = state[4]
        self.mouse_pos = (state[1], state[2])

    def _key_name(self, key_code):
        """Return a normalized key name for key_code."""
        if isinstance(key_code, int):
            if key_code in key_map.keys():
                return key_map[key_code]
        curs_key_name = self._curses_key_name(key_code)
        if curs_key_name:
            if curs_key_name in key_map.keys():
                return key_map[curs_key_name]
            return curs_key_name
        else:
            char = None
            if key_code in key_map.keys():
                return key_map[key_code]
            try:
                char = chr(key_code)
            except:
                return False
            if char is not None:
                self.is_typeable = True
                return char
        return False

    def _curses_key_name(self, key):
        """Return the curses key name for keys received from get_wch (and getch)."""
        # Handle multibyte get_wch input in Python 3.3
        if isinstance(key, str):
            return str(curses.keyname(ord(key)).decode("utf-8"))<commit_msg>Corrected stupid mistake in previous commit (only insert typeable keys)<commit_after># -*- encoding: utf-8
"""
Curses user interface.
"""

import os
import logging

from .prompt import Prompt, PromptBool, PromptFile
from .key_mappings import key_map

# Curses can't be imported yet but we'll
# predefine it to avoid confusing flake8
curses = None


class InputEvent:
    """Represents a keyboard or mouse event."""
    def __init__(self):
        self.type = None  # 'key' or 'mouse'
        self.key_name = None
        self.key_code = None
        self.is_typeable = False
        self.curses_key_name = None
        self.mouse_code = None
        self.mouse_pos = (0, 0)
        self.logger = logging.getLogger("{0}.InputEvent".format(__name__))

    def parse_key_code(self, key_code):
        """Parse a key code (or character) from curses."""
        self.type = "key"
        self.key_code = key_code
        self.key_name = self._key_name(key_code)
        self.curses_key_name = self._curses_key_name(key_code)

    def set_key_name(self, name):
        """Manually set the event key name."""
        self.type = "key"
        self.key_name = name

    def parse_mouse_state(self, state):
        """Parse curses mouse events."""
        self.type = "mouse"
        self.mouse_code = state[4]
        self.mouse_pos = (state[1], state[2])

    def _key_name(self, key_code):
        """Return a normalized key name for key_code."""
        if isinstance(key_code, int):
            if key_code in key_map.keys():
                return key_map[key_code]
        curs_key_name = self._curses_key_name(key_code)
        if curs_key_name:
            if curs_key_name in key_map.keys():
                return key_map[curs_key_name]
            self.is_typeable = True  # We'll assume the key is typeable if it's not found in the key map
            return curs_key_name
        else:
            char = None
            if key_code in key_map.keys():
                return key_map[key_code]
            try:
                char = chr(key_code)
            except:
                pass
            if char is not None:
                self.is_typeable = True
                return char
        return False

    def _curses_key_name(self, key):
        """Return the curses key name for keys received from get_wch (and getch)."""
        # Handle multibyte get_wch input in Python 3.3
        if isinstance(key, str):
            return str(curses.keyname(ord(key)).decode("utf-8"))
{'fraction_non_alphanumeric': 0.09467578205925005, 'fraction_numerical': 0.0045576962916925624, 'mean_word_length': 2.104823151125402, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '56274', 'n_tokens_mistral': 1497, 'n_tokens_neox': 1452, 'n_words': 413}