import config import numpy esc = '%s['%chr(27) reset = '%s0m'%esc cls = '%s2j'%esc BLACK = 0 RED = 1 GREEN = 2 YELLOW = 3 BLUE = 4 MAGENTA = 5 CYAN = 6 WHITE = 7 FBRIGHT = 8 FBLINK = 16 rgcolorBg = range(8) rgcolorFg = range(8) rgcolorFg.extend([col | FBRIGHT for col in range(8)]) K_BACKSPACE = chr(8) K_TAB = chr(9) K_RETURN = chr(10) K_DEL = chr(127) K_LEFT = 256 K_RIGHT = 257 K_UP = 258 K_DOWN = 259 K_HOME = 260 K_END = 261 K_PGUP = 262 K_PGDN = 263 def StrKey(ch): if type(ch) == str: return ord(ch) return str(ch) def Ctrl(ch): ch = ch.upper() assert ch != 'M' and ch != 'J' and ch != 'H' return chr(ord(ch - 'A' + 1)) def FEnter(key): return key == "\n" or key == "\r" # ANSI Character - contains prop and character class Ach(object): def __init__(self, ch, fgcol=WHITE, bgcol=BLACK): self.ch = ch self.fgCol = fgcol self.bgCol = bgcol def __eq__(self, ach): return type(ach) is Ach and self.ch == ach.ch and self.fgCol == ach.fgCol and self.bgCol == ach.bgCol def __ne__(self, ach): return not (self == ach) def MkAch(ch, fgcol=WHITE, bgcol=BLACK): return Ach(ch, fgcol, bgcol) def ChFromAch(ach): return ach.ch achBlank = Ach(' ') achInvd = Ach(chr(255), -1, -1) def AstFromAch(ach, achPrev = None): ast = ach.ch rgsgr = [] if (achPrev == None) or (achPrev.fgCol != ach.fgCol): rgsgr.append(str((ach.fgCol & 7) + 30)) fBright = ach.fgCol & FBRIGHT if (achPrev == None) or (fBright != achPrev.fgCol & FBRIGHT): if fBright: rgsgr.append("1") else: rgsgr.append("2") fBlink = ach.fgCol & FBLINK if (achPrev == None) or (fBlink != achPrev.fgCol & FBLINK): if fBlink: rgsgr.append("5") else: rgsgr.append("25") if (achPrev == None) or (achPrev.bgCol != ach.bgCol): rgsgr.append(str(ach.bgCol + 40)) if len(rgsgr) > 0: ast = esc + ";".join(rgsgr) + "m" + ast return ast dtAch = numpy.dtype(object) mpch_entity = [0, 9786, 9787, 9829, 9830, 9827, 9824, 8226, 9688, 9675, 9689, 9794, 9792, 9834, 9835, 9788, 9658, 9668, 8597, 8252, 182, 167, 9644, 8616, 8593, 8595, 8594, 8592, 8735, 8596, 9650, 9660, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 8962, 199, 252, 233, 226, 228, 224, 229, 231, 234, 235, 232, 239, 238, 236, 196, 197, 201, 230, 198, 244, 246, 242, 251, 249, 255, 214, 220, 162, 163, 165, 8359, 402, 225, 237, 243, 250, 241, 209, 170, 186, 191, 8976, 172, 189, 188, 161, 171, 187, 9617, 9618, 9619, 9474, 9508, 9569, 9570, 9558, 9557, 9571, 9553, 9559, 9565, 9564, 9563, 9488, 9492, 9524, 9516, 9500, 9472, 9532, 9566, 9567, 9562, 9556, 9577, 9574, 9568, 9552, 9580, 9575, 9576, 9572, 9573, 9561, 9560, 9554, 9555, 9579, 9578, 9496, 9484, 9608, 9604, 9612, 9616, 9600, 945, 223, 915, 960, 931, 963, 181, 964, 934, 920, 937, 948, 8734, 966, 949, 8745, 8801, 177, 8805, 8804, 8992, 8993, 247, 8776, 176, 8729, 183, 8730, 8319, 178, 9632, 160] setch_unsafe = set(['&', '<', '>', '"', "'"]) mpcol_hcol = ["#000000", "#AA0000", "#00AA00", "#AA5500", "#0000AA", "#AA00AA", "#00AAAA", "#AAAAAA", "#555555", "#FF5555", "#55FF55", "#FFFF55", "#5555FF", "#FF55FF", "#55FFFF", "#FFFFFF"] def HstFromAch(ach, achPrev = None): stStyle = None if achPrev == None or achPrev.fgCol != ach.fgCol or achPrev.bgCol != ach.bgCol: stStyle = "color:" + mpcol_hcol[ach.fgCol & 15] + ";background-color:" + mpcol_hcol[ach.bgCol & 15] rgst = [] if stStyle != None and achPrev != None: rgst.append("") if stStyle != None: rgst.append("") ch = ord(ach.ch) entity = mpch_entity[ch] if entity == ch and not ach.ch in setch_unsafe: rgst.append(ach.ch) else: rgst.append("&#" + str(mpch_entity[ord(ach.ch)]) + ";") return ''.join(rgst) class Pos(object): def __init__(self, w=config.W, h=config.H, x=1, y=1): self.w = w self.h = h self.xz = x - 1 self.yz = y - 1 def X(self): return self.xz + 1 def Y(self): return self.yz + 1 def Inc(self): if self.xz == self.w - 1: if self.yz < self.h: self.xz = 0 self.yz = self.yz + 1 else: self.xz = self.xz + 1 def Up(self): if self.yz > 0: self.yz = self.yz - 1 def Down(self): if self.yz < self.h - 1: self.yz = self.yz + 1 def Left(self): if self.xz > 0: self.xz = self.xz - 1 def Right(self): if self.xz < self.w - 1: self.xz = self.xz + 1 # ANSI Screen class Ascr(object): def __init__(self, w=config.W, h=config.H, achFill = None): self.mpyzxz_ach = numpy.empty((h,w), dtAch) self.mpyzxz_ach[:] = achFill self.w = w self.h = h def Zpos(self, xz, yz): return Pos(self.w, self.h, xz + 1, yz + 1) def W(self): return self.w def H(self): return self.h def PutAch(self, ach, x, y): if not (x < 1 or y < 1 or x > self.w or y > self.h): #clip self.mpyzxz_ach[y-1][x-1] = ach def GetAch(self, x, y): return self.mpyzxz_ach[y-1][x-1] def PutRgach(self, rgach, x, y): xz = x - 1 if xz < self.w: if xz < 0: rgach = rgach[min(-xz, len(rgach)):] xz = 0 if xz + len(rgach) >= self.w: rgach = rgach[:self.w - xz] self.mpyzxz_ach[y-1][xz:xz + len(rgach)] = rgach def PutSt(self, st, x, y, colFg = WHITE, colBg=BLACK): self.PutRgach([MkAch(ch, colFg, colBg) for ch in st], x, y) def Fill(self, ach, w, h, x = 1, y = 1): if x > self.w or y > self.h: return xz = max(x - 1, 0) yz = max(y - 1, 0) w = min(self.w - xz, w) h = min(self.h - yz, h) for yzT in range(yz, yz + h): self.mpyzxz_ach[yzT][xz:xz + w] = ach def PutAscr(self, ascr, x=1, y=1): for irow, rgachRow in enumerate(ascr.mpyzxz_ach): iachPut = -1 for iach, ach in enumerate(rgachRow): if iachPut < 0 and ach != None: iachPut = iach elif iachPut >= 0 and ach == None: self.PutRgach(rgachRow[iachPut:iach], x + iachPut, y + irow) iachPut = -1 if iachPut >= 0: self.PutRgach(rgachRow[iachPut:], x + iachPut, y + irow) iachPut = -1 def AstDiff(self, ascr): assert self.w == ascr.w and self.h == ascr.h rgast = [] achPrev = None yz = 0 for rgachRowOld, rgachRowNew in zip(self.mpyzxz_ach, ascr.mpyzxz_ach): xz = 0 xzPred = -1 for achOld, achNew in zip(rgachRowOld, rgachRowNew): if xz == self.w - 2: achBeforeEOL = achNew if achOld != achNew: achNew = achNew or achBlank if xz == self.w - 1: # Linewrap avoidance algorithm: # when drawing the last character on a line, draw it in the space occupied by the character before. rgast.append("%s%d;%dH" % (esc, yz + 1, xz)) rgast.append(AstFromAch(achNew, None)) # then, move the cursor back onto the character we just drew, and perform an insert. rgast.append("%sD%s@" % (esc, esc)) # finally, draw the character before the last character in the line. rgast.append(AstFromAch(achBeforeEOL or achBlank, achNew)) else: if xz != xzPred: xzPred = xz rgast.append("%s%d;%dH" % (esc, yz + 1, xz + 1)) achPrev = None rgast.append(AstFromAch(achNew, achPrev)) achPrev = achNew xzPred = xzPred + 1 xz = xz + 1 yz = yz + 1 return "".join(rgast) def Ast(self): return Ascr(self.w, self.h, achInvd).AstDiff(self) #stupid implementation def Hst(self): rgst = ["
"]
        achPrev = None
        for rgachRow in self.mpyzxz_ach:
            for ach in rgachRow:
                rgst.append(HstFromAch(ach or achBlank, achPrev))
                achPrev = ach or achBlank
            rgst.append("
") rgst.append("
") return ''.join(rgst) def FKeyPrintable(key): return type(key) == str and (ord(key) >= 32 and ord(key) <= 126)