Get rid of numpy

This commit is contained in:
Kovid Goyal
2016-10-15 18:16:49 +05:30
parent c12a3ff826
commit 23bc25eb64
3 changed files with 37 additions and 20 deletions

View File

@@ -28,7 +28,5 @@ class Boss(QObject):
if previous == cells_per_line: if previous == cells_per_line:
return return
old = self.linebuf.copy() old = self.linebuf.copy()
print(list(old))
self.linebuf.clear() self.linebuf.clear()
self.linebuf.extend(rewrap_lines(old, cells_per_line)) self.linebuf.extend(rewrap_lines(old, cells_per_line))
print(list(self.linebuf))

View File

@@ -2,12 +2,24 @@
# vim:fileencoding=utf-8 # vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net> # License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
import array
from typing import Tuple, Dict, Union, Iterator, Sequence from typing import Tuple, Dict, Union, Iterator, Sequence
from itertools import repeat
from numpy import zeros, dtype
from PyQt5.QtGui import QColor 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: class Line:
@@ -16,16 +28,17 @@ class Line:
continued = False continued = False
def __init__(self, sz: int): def __init__(self, sz: int):
self.char = zeros(sz, 'U1') z1, z4 = get_zeroes(sz)
self.fg = zeros(sz, color_type) self.char = z4[:]
self.bg = zeros(sz, color_type) self.fg = z4[:]
self.bold = zeros(sz, bool) self.bg = z4[:]
self.italic = zeros(sz, bool) self.bold = z1[:]
self.reverse = zeros(sz, bool) self.italic = z1[:]
self.strikethrough = zeros(sz, bool) self.reverse = z1[:]
self.decoration = zeros(sz, 'u1') self.strikethrough = z1[:]
self.decoration_fg = zeros(sz, color_type) self.decoration = z1[:]
self.width = zeros(sz, 'u1') self.decoration_fg = z4[:]
self.width = z1[:]
def __len__(self): def __len__(self):
return len(self.char) return len(self.char)
@@ -42,15 +55,22 @@ class Line:
to.decoration_fg[dest] = self.decoration_fg[src] to.decoration_fg[dest] = self.decoration_fg[src]
to.width[dest] = self.width[src] to.width[dest] = self.width[src]
def __str__(self) -> str:
return ''.join(map(ord, self.char)).rstrip('\0')
def __repr__(self) -> str: 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]: def as_color(entry: int, color_table: Dict[int, QColor]) -> Union[QColor, None]:
t, r, g, b = entry t = entry & 0xff
if t == 1: if t == 1:
r = (entry >> 8) & 0xff
return color_table.get(r) return color_table.get(r)
if t == 2: if t == 2:
r = (entry >> 8) & 0xff
g = (entry >> 16) & 0xff
b = (entry >> 24) & 0xff
return QColor(r, g, b) return QColor(r, g, b)

View File

@@ -105,14 +105,13 @@ class TerminalWidget(QWidget):
def paint_cell(self, painter: QPainter, line: Line, col: int, y: int) -> None: def paint_cell(self, painter: QPainter, line: Line, col: int, y: int) -> None:
x = self.cell_positions[col] 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) fg = as_color(line.fg[col], self.ansi_fg)
if fg is not None: if fg is not None:
painter.setPen(QPen(fg)) painter.setPen(QPen(fg))
bg = as_color(line.bg[col], self.ansi_bg) bg = as_color(line.bg[col], self.ansi_bg)
if bg is not None: if bg is not None:
r = QRect(x, y, self.cell_width, self.cell_height)
painter.fillRect(r, bg) painter.fillRect(r, bg)
char = line.char[col] char = line.char[col]
if char: if char:
painter.drawText(x, y + self.baseline_offset, char) painter.drawText(x, y + self.baseline_offset, chr(char))