fenrir/src/fenrirscreenreader/core/attributeManager.py

235 lines
8.4 KiB
Python
Raw Normal View History

2018-05-24 05:31:21 -04:00
#!/bin/python
# -*- coding: utf-8 -*-
# Fenrir TTY screen reader
# By Chrys, Storm Dragon, and contributers.
from fenrirscreenreader.core import debug
2018-05-27 08:31:00 -04:00
from collections import Counter
2018-05-24 05:31:21 -04:00
class attributeManager():
def __init__(self):
2018-05-28 03:59:18 -04:00
self.currAttributes = None
self.prevAttributes = None
2018-05-28 04:32:13 -04:00
self.currAttribDelta = ''
self.prevAttribDelta = ''
self.currAttribCursor = None
self.prefAttribCursor = None
2018-05-28 03:59:18 -04:00
self.setDefaultAttributes()
2018-05-24 05:31:21 -04:00
def initialize(self, environment):
self.env = environment
def shutdown(self):
pass
2018-05-28 04:00:34 -04:00
def updateAttributeData(self, prevAttributes, currAttributes):
self.prevAttributes = prevAttributes
self.currAttributes = currAttributes
2018-05-28 04:32:13 -04:00
def getAttributeByXY(self, x, y):
if not self.currAttributes:
return None
if len(self.currAttributes) < y:
return None
if len(self.currAttributes[y]) < x:
return None
return self.currAttributes[y][x]
2018-05-25 06:59:10 -04:00
def setDefaultAttributes(self):
self.defaultAttributes = []
self.defaultAttributes.append((
2018-05-25 06:50:24 -04:00
'white', # fg
'black', # bg
False, # bold
False, # italics
False, # underscore
False, # strikethrough
False, # reverse
False, # blink
'default', # fontsize
'default' # fontfamily
)) #end attribute
2018-05-25 06:59:10 -04:00
self.defaultAttributes.append((
2018-05-25 06:50:24 -04:00
'default', # fg
'default', # bg
False, # bold
False, # italics
False, # underscore
False, # strikethrough
False, # reverse
False, # blink
'default', # fontsize
'default' # fontfamily
2018-05-25 06:59:10 -04:00
)) #end attribute
def isDefaultAttribute(self,attribute):
return attribute in self.defaultAttributes
2018-05-24 05:31:21 -04:00
def formatAttributes(self, attribute, attributeFormatString = None):
2018-05-25 06:56:03 -04:00
# "black",
# "red",
# "green",
# "brown",
# "blue",
# "magenta",
# "cyan",
# "white",
# "default" # white.
# _order_
# "fg",
# "bg",
# "bold",
# "italics",
# "underscore",
# "strikethrough",
# "reverse",
# "blink"
# "fontsieze"
# "fontfamily"
2018-05-24 05:31:21 -04:00
if not attributeFormatString:
attributeFormatString = self.env['runtime']['settingsManager'].getSetting('general', 'attributeFormatString')
if not attributeFormatString:
return ''
if attributeFormatString == '':
return ''
2018-05-25 06:56:03 -04:00
if not attribute:
return ''
if len(attribute) != 10:
return ''
# 0 FG color (name)
try:
attributeFormatString = attributeFormatString.replace('fenrirFGColor', _(attribute[0]))
except Exception as e:
attributeFormatString = attributeFormatString.replace('fenrirFGColor', '')
# 1 BG color (name)
try:
attributeFormatString = attributeFormatString.replace('fenrirBGColor', _(attribute[1]))
except Exception as e:
attributeFormatString = attributeFormatString.replace('fenrirBGColor', '')
# 2 bold (True/ False)
try:
if attribute[2]:
attributeFormatString = attributeFormatString.replace('fenrirBold', _('bold'))
except Exception as e:
pass
attributeFormatString = attributeFormatString.replace('fenrirBold', '')
# 3 italics (True/ False)
2018-05-24 05:31:21 -04:00
try:
2018-05-25 06:56:03 -04:00
if attribute[3]:
attributeFormatString = attributeFormatString.replace('fenrirItalics', _('italic'))
2018-05-24 05:31:21 -04:00
except Exception as e:
2018-05-25 06:56:03 -04:00
pass
attributeFormatString = attributeFormatString.replace('fenrirItalics', '')
# 4 underline (True/ False)
2018-05-24 05:31:21 -04:00
try:
2018-05-25 06:56:03 -04:00
if attribute[4]:
attributeFormatString = attributeFormatString.replace('fenrirUnderline', _('underline'))
2018-05-24 05:31:21 -04:00
except Exception as e:
2018-05-25 06:56:03 -04:00
pass
attributeFormatString = attributeFormatString.replace('fenrirUnderline', '')
# 5 strikethrough (True/ False)
2018-05-24 05:31:21 -04:00
try:
2018-05-25 06:56:03 -04:00
if attribute[5]:
attributeFormatString = attributeFormatString.replace('fenrirStrikethrough', _('strikethrough'))
2018-05-24 05:31:21 -04:00
except Exception as e:
2018-05-25 06:56:03 -04:00
pass
attributeFormatString = attributeFormatString.replace('fenrirStrikethrough', '')
# 6 reverse (True/ False)
2018-05-24 05:31:21 -04:00
try:
2018-05-25 06:56:03 -04:00
if attribute[6]:
attributeFormatString = attributeFormatString.replace('fenrirReverse', _('reverse'))
2018-05-24 05:31:21 -04:00
except Exception as e:
2018-05-25 06:56:03 -04:00
pass
attributeFormatString = attributeFormatString.replace('fenrirReverse', '')
# 7 blink (True/ False)
2018-05-24 05:31:21 -04:00
try:
2018-05-25 06:56:03 -04:00
if attribute[7]:
attributeFormatString = attributeFormatString.replace('fenrirBlink', _('blink'))
2018-05-24 05:31:21 -04:00
except Exception as e:
2018-05-25 06:56:03 -04:00
pass
attributeFormatString = attributeFormatString.replace('fenrirBlink', '')
# 8 font size (int/ string)
2018-05-24 05:31:21 -04:00
try:
2018-05-25 06:56:03 -04:00
try:
attributeFormatString = attributeFormatString.replace('fenrirFontSize', int(attribute[8]))
except:
pass
try:
attributeFormatString = attributeFormatString.replace('fenrirFontSize', str(attribute[8]))
except:
pass
2018-05-24 05:31:21 -04:00
except Exception as e:
2018-05-25 06:56:03 -04:00
pass
attributeFormatString = attributeFormatString.replace('fenrirFontSize', _('default'))
# 9 font family (string)
2018-05-24 05:31:21 -04:00
try:
2018-05-25 06:56:03 -04:00
attributeFormatString = attributeFormatString.replace('fenrirFont', attribute[9])
2018-05-24 05:31:21 -04:00
except Exception as e:
2018-05-25 06:56:03 -04:00
pass
attributeFormatString = attributeFormatString.replace('fenrirFont', _('default'))
2018-05-24 20:56:03 -04:00
return attributeFormatString
2018-05-27 16:44:17 -04:00
def trackHighlights(self, oldAttr, newAttr, text):
2018-05-27 08:31:00 -04:00
result = ''
currCursor = None
2018-05-28 03:50:37 -04:00
# screen change
if oldAttr == None:
return result, currCursor
# no change
2018-05-27 16:44:17 -04:00
if oldAttr == newAttr:
return result, currCursor
2018-05-28 03:59:18 -04:00
# error case
if newAttr == None:
return result, currCursor
2018-05-28 03:50:37 -04:00
# special case for pty if not text exists.
2018-05-27 08:31:00 -04:00
if len(newAttr) == 0:
2018-05-28 03:59:18 -04:00
return result, currCursor
2018-05-28 03:50:37 -04:00
2018-05-27 08:31:00 -04:00
textLines = text.split('\n')
2018-05-27 16:41:03 -04:00
if len(textLines) != len(newAttr):
2018-05-27 08:31:00 -04:00
return result, currCursor
#print(len(textLines), len(newAttr))
#background = []
2018-05-27 16:41:03 -04:00
2018-05-27 08:31:00 -04:00
try:
pass
#llAttrib = [line for line in newAttr]
#print(Counter(allAttrib[0]).most_common())
2018-05-27 16:41:03 -04:00
#from collections import Counter
#import random
#tups = [ (1,2), (3,4), (5,6), (1,2), (3,4) ]
#lst = Counter(tups).most_common()
#highest_count = max([i[1] for i in lst])
#values = [i[0] for i in lst if i[1] == highest_count]
#random.shuffle(values)
#print(values[0])
#bgStat = Counter(newAttr).most_common(3)
2018-05-27 08:31:00 -04:00
#for i in bgStat:
# print(i)
#background.append(bgStat[0][0])
# if there is a third color add a secondary background (for dialogs for example)
#if len(bgStat) > 2:
# if bgStat[1][1] > 40:
# background.append(bgStat[1][0])
except Exception as e:
print(e)
#background.append((7,7,0,0,0,0))
2018-05-28 03:50:37 -04:00
for line in range(len(oldAttr)):
2018-05-27 08:31:00 -04:00
if oldAttr[line] != newAttr[line]:
2018-05-28 01:12:35 -04:00
for column in range(len(oldAttr[line])):
2018-05-27 08:31:00 -04:00
if oldAttr[line][column] != newAttr[line][column]:
2018-05-28 01:12:35 -04:00
if not self.isDefaultAttribute(newAttr[line][column]):
if not currCursor:
2018-05-28 03:52:58 -04:00
currCursor = {'x': column, 'y': line}
2018-05-28 01:12:35 -04:00
result += textLines[line][column]
2018-05-27 08:31:00 -04:00
result += ' '
return result, currCursor