<commit_before>#!/usr/bin/python
#
# Urwid terminal emulation widget
#    Copyright (C) 2010  aszlig
#
#    This library is free software; you can redistribute it and/or
#    modify it under the terms of the GNU Lesser General Public
#    License as published by the Free Software Foundation; either
#    version 2.1 of the License, or (at your option) any later version.
#
#    This library is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#    Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public
#    License along with this library; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Urwid web site: http://excess.org/urwid/

import os
import pty
import time
import fcntl
import struct
import signal
import atexit
import termios

import urwid

ESC = chr(27)

KEY_TRANSLATIONS = {
    'enter':     chr(13),
    'backspace': chr(127),
    'tab':       chr(9),
    'esc':       ESC,
    'up':        ESC + '[A',
    'down':      ESC + '[B',
    'right':     ESC + '[C',
    'left':      ESC + '[D',
    'home':      ESC + '[1~',
    'end':       ESC + '[4~',
    'page up':   ESC + '[5~',
    'page down': ESC + '[6~',

    'f1':        ESC + '[[A',
    'f2':        ESC + '[[B',
    'f3':        ESC + '[[C',
    'f4':        ESC + '[[D',
    'f5':        ESC + '[[E',
    'f6':        ESC + '[17~',
    'f7':        ESC + '[18~',
    'f8':        ESC + '[19~',
    'f9':        ESC + '[20~',
    'f10':       ESC + '[21~',
    'f11':       ESC + '[23~',
    'f12':       ESC + '[24~',
}

CSI_COMMANDS = {
    # possible values:
    #     None -> ignore sequence
    #     (<minimum number of args>, callback)
    #     ('alias', <symbol>)
    #
    # while callback is executed as:
    #     callback(<instance of TermCanvas>, arguments, has_question_mark)

    '@': (1, lambda s, (number,), q: s.insert_chars(chars=number)),
    'A': (1, lambda s, (rows,), q: s.move_cursor(0, -rows, relative=True)),
    'B': (1, lambda s, (rows,), q: s.move_cursor(0, rows, relative=True)),
    'C': (1, lambda s, (cols,), q: s.move_cursor(cols, 0, relative=True)),
    'D': (1, lambda s, (cols,), q: s.move_cursor(-cols, 0, relative=True)),
    'E': (1, lambda s, (rows,), q: s.move_cursor(0, rows, relative_y=True)),
    'F': (1, lambda s, (rows,), q: s.move_cursor(0, -rows, relative_y=True)),
    'G': (1, lambda s, (col,), q: s.move_cursor(col - 1, 0, relative_y=True)),
    'H': (2, lambda s, (x, y), q: s.move_cursor(y - 1, x - 1)),
    'J': (1, lambda s, (mode,), q: s.csi_erase_display(mode)),
    'K': (1, lambda s, (mode,), q: s.csi_erase_line(mode)),
    'L': (1, lambda s, (number,), q: s.insert_lines(lines=number)),
    'M': (1, lambda s, (number,), q: s.remove_lines(lines=number)),
    'P': (1, lambda s, (number,), q: s.remove_chars(chars=number)),
    'X': (1, lambda s, (number,), q: s.erase(s.cursor, (s.cursor[0]+number-1,
                                                        s.cursor[1]))),
    'a': ('alias', 'C'),
    'c': (0, lambda s, (t,), q: None if q else s.widget.respond(ESC + '[?6c')),
    'd': (1, lambda s, (row,), q: s.move_cursor(0, row - 1, relative_x=True)),
    'e': ('alias', 'B'),
    'f': ('alias', 'H'),
    'g': None,
    'h': None,
    'l': None,
    'm': (1, lambda s, attrs, q: s.csi_set_attr(attrs)),
    'n': (1, lambda s, (mode,), q: s.csi_status_report(mode)),
    'q': None,
    'r': None,
    's': None,
    'u': None,
    '`': ('alias', 'G'),
}

def wrap_store(function):
    """
    Ensures that the TermCanvas will lose its finalized state
    after being stored to the cache.
    """
    @classmethod
    def _wrap(cls, wcls, canvas):
        if isinstance(canvas, TermCanvas):
            return
        return function(wcls, canvas)

    return _wrap

# XXX: monkey-patching...
urwid.CanvasCache.store = wrap_store(urwid.CanvasCache.store)

class TermCanvas(urwid.Canvas):
    def __init__(self, width, height, widget):
        urwid.Canvas.__init__(self)

        self.width, self.height = width, height
        self.widget = widget

        self.scrollback_buffer = []

        self.escbuf = ''
        self.within_escape = False
        self.parsestate = 0
        self.attrspec = None

        # initialize self.term
        self.clear()

    def _get_cursor(self):
        return self._cursor
    def _set_cursor(self, pos):
        if pos is None:
            self.coords.pop("cursor", None)
            return

        self.coords["cursor"] = pos + (None,)
        self._cursor = pos
    cursor = property(_get_cursor, _set_cursor)

    def empty_line(self, char=' '):
        return [self.empty_char(char)] * self.width

    def empty_char(self, char=' '):
        return (self.attrspec, None, char)

    def addstr(self, data):
        x, y = self.cursor
        for char in data:
            self.addch(char)

    def resize(self, width, height):
        if width > self.width:
            for y in xrange(self.height):
                self.term[y] += [self.empty_char()] * (width - self.width)
        elif width < self.width:
            for y in xrange(self.height):<commit_msg>Replace monkey-patching stuff in vterm with cacheable = False.<commit_after>#!/usr/bin/python
#
# Urwid terminal emulation widget
#    Copyright (C) 2010  aszlig
#
#    This library is free software; you can redistribute it and/or
#    modify it under the terms of the GNU Lesser General Public
#    License as published by the Free Software Foundation; either
#    version 2.1 of the License, or (at your option) any later version.
#
#    This library is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#    Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public
#    License along with this library; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Urwid web site: http://excess.org/urwid/

import os
import pty
import time
import fcntl
import struct
import signal
import atexit
import termios

import urwid

ESC = chr(27)

KEY_TRANSLATIONS = {
    'enter':     chr(13),
    'backspace': chr(127),
    'tab':       chr(9),
    'esc':       ESC,
    'up':        ESC + '[A',
    'down':      ESC + '[B',
    'right':     ESC + '[C',
    'left':      ESC + '[D',
    'home':      ESC + '[1~',
    'end':       ESC + '[4~',
    'page up':   ESC + '[5~',
    'page down': ESC + '[6~',

    'f1':        ESC + '[[A',
    'f2':        ESC + '[[B',
    'f3':        ESC + '[[C',
    'f4':        ESC + '[[D',
    'f5':        ESC + '[[E',
    'f6':        ESC + '[17~',
    'f7':        ESC + '[18~',
    'f8':        ESC + '[19~',
    'f9':        ESC + '[20~',
    'f10':       ESC + '[21~',
    'f11':       ESC + '[23~',
    'f12':       ESC + '[24~',
}

CSI_COMMANDS = {
    # possible values:
    #     None -> ignore sequence
    #     (<minimum number of args>, callback)
    #     ('alias', <symbol>)
    #
    # while callback is executed as:
    #     callback(<instance of TermCanvas>, arguments, has_question_mark)

    '@': (1, lambda s, (number,), q: s.insert_chars(chars=number)),
    'A': (1, lambda s, (rows,), q: s.move_cursor(0, -rows, relative=True)),
    'B': (1, lambda s, (rows,), q: s.move_cursor(0, rows, relative=True)),
    'C': (1, lambda s, (cols,), q: s.move_cursor(cols, 0, relative=True)),
    'D': (1, lambda s, (cols,), q: s.move_cursor(-cols, 0, relative=True)),
    'E': (1, lambda s, (rows,), q: s.move_cursor(0, rows, relative_y=True)),
    'F': (1, lambda s, (rows,), q: s.move_cursor(0, -rows, relative_y=True)),
    'G': (1, lambda s, (col,), q: s.move_cursor(col - 1, 0, relative_y=True)),
    'H': (2, lambda s, (x, y), q: s.move_cursor(y - 1, x - 1)),
    'J': (1, lambda s, (mode,), q: s.csi_erase_display(mode)),
    'K': (1, lambda s, (mode,), q: s.csi_erase_line(mode)),
    'L': (1, lambda s, (number,), q: s.insert_lines(lines=number)),
    'M': (1, lambda s, (number,), q: s.remove_lines(lines=number)),
    'P': (1, lambda s, (number,), q: s.remove_chars(chars=number)),
    'X': (1, lambda s, (number,), q: s.erase(s.cursor, (s.cursor[0]+number-1,
                                                        s.cursor[1]))),
    'a': ('alias', 'C'),
    'c': (0, lambda s, (t,), q: None if q else s.widget.respond(ESC + '[?6c')),
    'd': (1, lambda s, (row,), q: s.move_cursor(0, row - 1, relative_x=True)),
    'e': ('alias', 'B'),
    'f': ('alias', 'H'),
    'g': None,
    'h': None,
    'l': None,
    'm': (1, lambda s, attrs, q: s.csi_set_attr(attrs)),
    'n': (1, lambda s, (mode,), q: s.csi_status_report(mode)),
    'q': None,
    'r': None,
    's': None,
    'u': None,
    '`': ('alias', 'G'),
}

class TermCanvas(urwid.Canvas):
    cacheable = False

    def __init__(self, width, height, widget):
        urwid.Canvas.__init__(self)

        self.width, self.height = width, height
        self.widget = widget

        self.scrollback_buffer = []

        self.escbuf = ''
        self.within_escape = False
        self.parsestate = 0
        self.attrspec = None

        # initialize self.term
        self.clear()

    def empty_line(self, char=' '):
        return [self.empty_char(char)] * self.width

    def empty_char(self, char=' '):
        return (self.attrspec, None, char)

    def addstr(self, data):
        x, y = self.cursor
        for char in data:
            self.addch(char)

    def resize(self, width, height):
        if width > self.width:
            for y in xrange(self.height):
                self.term[y] += [self.empty_char()] * (width - self.width)
        elif width < self.width:
            for y in xrange(self.height):
{'fraction_non_alphanumeric': 0.17380881030866047, 'fraction_numerical': 0.019378683448206972, 'mean_word_length': 2.4089206673476338, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3657595', 'n_tokens_mistral': 3631, 'n_tokens_neox': 3521, 'n_words': 1065}