From 23bc25eb64383b20aa751d1bb875b5d451520675 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 15 Oct 2016 18:16:49 +0530 Subject: [PATCH] Get rid of numpy --- kitty/boss.py | 2 -- kitty/data_types.py | 50 +++++++++++++++++++++++++++++++-------------- kitty/term.py | 5 ++--- 3 files changed, 37 insertions(+), 20 deletions(-) diff --git a/kitty/boss.py b/kitty/boss.py index a94d8ba30..6544a8ea6 100644 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -28,7 +28,5 @@ class Boss(QObject): if previous == cells_per_line: return old = self.linebuf.copy() - print(list(old)) self.linebuf.clear() self.linebuf.extend(rewrap_lines(old, cells_per_line)) - print(list(self.linebuf)) diff --git a/kitty/data_types.py b/kitty/data_types.py index 6a0847cda..8e90f2fe3 100644 --- a/kitty/data_types.py +++ b/kitty/data_types.py @@ -2,12 +2,24 @@ # vim:fileencoding=utf-8 # License: GPL v3 Copyright: 2016, Kovid Goyal +import array from typing import Tuple, Dict, Union, Iterator, Sequence +from itertools import repeat -from numpy import zeros, dtype from PyQt5.QtGui import QColor -color_type = dtype([('type', 'u1'), ('r', 'u1'), ('g', 'u1'), ('b', 'u1')]) +code = 'I' if array.array('I').itemsize >= 4 else 'L' + + +def get_zeroes(sz: int) -> Tuple[array.array]: + if get_zeroes.current_size != sz: + get_zeroes.current_size = sz + get_zeroes.ans = ( + array.array('B', repeat(0, sz)), + array.array(code, repeat(0, sz)), + ) + return get_zeroes.ans +get_zeroes.current_size = None class Line: @@ -16,16 +28,17 @@ class Line: continued = False def __init__(self, sz: int): - self.char = zeros(sz, 'U1') - self.fg = zeros(sz, color_type) - self.bg = zeros(sz, color_type) - self.bold = zeros(sz, bool) - self.italic = zeros(sz, bool) - self.reverse = zeros(sz, bool) - self.strikethrough = zeros(sz, bool) - self.decoration = zeros(sz, 'u1') - self.decoration_fg = zeros(sz, color_type) - self.width = zeros(sz, 'u1') + z1, z4 = get_zeroes(sz) + self.char = z4[:] + self.fg = z4[:] + self.bg = z4[:] + self.bold = z1[:] + self.italic = z1[:] + self.reverse = z1[:] + self.strikethrough = z1[:] + self.decoration = z1[:] + self.decoration_fg = z4[:] + self.width = z1[:] def __len__(self): return len(self.char) @@ -42,15 +55,22 @@ class Line: to.decoration_fg[dest] = self.decoration_fg[src] to.width[dest] = self.width[src] + def __str__(self) -> str: + return ''.join(map(ord, self.char)).rstrip('\0') + def __repr__(self) -> str: - return repr(''.join(self.char)) + return repr(str(self)) -def as_color(entry: Tuple[int, int, int, int], color_table: Dict[int, QColor]) -> Union[QColor, None]: - t, r, g, b = entry +def as_color(entry: int, color_table: Dict[int, QColor]) -> Union[QColor, None]: + t = entry & 0xff if t == 1: + r = (entry >> 8) & 0xff return color_table.get(r) if t == 2: + r = (entry >> 8) & 0xff + g = (entry >> 16) & 0xff + b = (entry >> 24) & 0xff return QColor(r, g, b) diff --git a/kitty/term.py b/kitty/term.py index 1b5eb69fa..08fddeeba 100644 --- a/kitty/term.py +++ b/kitty/term.py @@ -105,14 +105,13 @@ class TerminalWidget(QWidget): def paint_cell(self, painter: QPainter, line: Line, col: int, y: int) -> None: x = self.cell_positions[col] - r = QRect(x, y, self.cell_width, self.cell_height) - t, r, g, b = line.fg[col] fg = as_color(line.fg[col], self.ansi_fg) if fg is not None: painter.setPen(QPen(fg)) bg = as_color(line.bg[col], self.ansi_bg) if bg is not None: + r = QRect(x, y, self.cell_width, self.cell_height) painter.fillRect(r, bg) char = line.char[col] if char: - painter.drawText(x, y + self.baseline_offset, char) + painter.drawText(x, y + self.baseline_offset, chr(char))