From ceed614b9a5d14bf0bd1087c23c92a244fef1de8 Mon Sep 17 00:00:00 2001 From: chrys Date: Sun, 27 May 2018 14:31:00 +0200 Subject: [PATCH] very basic initial hilight tracking --- .../core/attributeManager.py | 41 ++++++++++ src/fenrirscreenreader/core/screenManager.py | 3 +- src/fenrirscreenreader/utils/screen_utils.py | 82 ------------------- 3 files changed, 43 insertions(+), 83 deletions(-) diff --git a/src/fenrirscreenreader/core/attributeManager.py b/src/fenrirscreenreader/core/attributeManager.py index 2fdcbfc7..f9957897 100644 --- a/src/fenrirscreenreader/core/attributeManager.py +++ b/src/fenrirscreenreader/core/attributeManager.py @@ -5,6 +5,7 @@ # By Chrys, Storm Dragon, and contributers. from fenrirscreenreader.core import debug +from collections import Counter class attributeManager(): def __init__(self): @@ -155,3 +156,43 @@ class attributeManager(): attributeFormatString = attributeFormatString.replace('fenrirFont', _('default')) return attributeFormatString + def trackHighlights(self, oldAttr, newAttr, text, lenght): + result = '' + currCursor = None + if oldAttr == newAttr: + return result, currCursor + if len(newAttr) == 0: + return result, currCursor + if len(oldAttr) != len(newAttr): + return result, currCursor + + textLines = text.split('\n') + + if len(textLines) - 1 != len(newAttr): + return result, currCursor + + background = [] + try: + bgStat = Counter(newAttr).most_common(3) + #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)) + for line in range(len(newAttr)): + if oldAttr[line] != newAttr[line]: + for column in range(len(newAttr[line])): + if oldAttr[line][column] != newAttr[line][column]: + if not self.isDefaultAttribute(newAttr[line][column]): + if not currCursor: + currCursor = {} + currCursor['x'] = column + currCursor['y'] = line + result += textLines[line][column] + result += ' ' + return result, currCursor diff --git a/src/fenrirscreenreader/core/screenManager.py b/src/fenrirscreenreader/core/screenManager.py index fce2d402..4afd6e9d 100644 --- a/src/fenrirscreenreader/core/screenManager.py +++ b/src/fenrirscreenreader/core/screenManager.py @@ -159,8 +159,9 @@ class screenManager(): try: if self.env['screen']['oldContentAttrib'] != self.env['screen']['newContentAttrib']: if self.env['runtime']['settingsManager'].getSettingAsBool('focus', 'highlight'): - self.env['screen']['newAttribDelta'], self.env['screen']['newCursorAttrib'] = screen_utils.trackHighlights(self.env['screen']['oldContentAttrib'], self.env['screen']['newContentAttrib'], self.env['screen']['newContentText'], self.env['screen']['columns']) + self.env['screen']['newAttribDelta'], self.env['screen']['newCursorAttrib'] = self.env['runtime']['attributeManager'].trackHighlights(self.env['screen']['oldContentAttrib'], self.env['screen']['newContentAttrib'], self.env['screen']['newContentText'], self.env['screen']['columns']) except Exception as e: + print(e) self.env['runtime']['debug'].writeDebugOut('screenManager:update:highlight: ' + str(e),debug.debugLevel.ERROR) def isSuspendingScreen(self, screen = None): diff --git a/src/fenrirscreenreader/utils/screen_utils.py b/src/fenrirscreenreader/utils/screen_utils.py index 20db85e8..95075574 100644 --- a/src/fenrirscreenreader/utils/screen_utils.py +++ b/src/fenrirscreenreader/utils/screen_utils.py @@ -5,7 +5,6 @@ # By Chrys, Storm Dragon, and contributers. from fenrirscreenreader.core import debug -from collections import Counter import getpass, time, re, string, select, os def removeNonprintable(text): @@ -80,84 +79,3 @@ def getShell(): if isValidShell('/bin/bash'): return '/bin/bash' return '/bin/sh' -def trackHighlights(oldAttr, newAttr, text, lenght): - result = '' - currCursor = None - if oldAttr == newAttr: - return result, currCursor - if len(newAttr) == 0: - return result, currCursor - if len(oldAttr) != len(newAttr): - return result, currCursor - - old = splitEvery(oldAttr,lenght) - new = splitEvery(newAttr,lenght) - textLines = text.split('\n') - background = [] - - if len(textLines) - 1 != len(new): - return result, currCursor - try: - bgStat = Counter(newAttr).most_common(3) - 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: - background.append((7,7,0,0,0,0)) - for line in range(len(new)): - if old[line] != new[line]: - for column in range(len(new[line])): - print(new[line][column]) - if old[line][column] != new[line][column]: - if not new[line][column] in background: - if not currCursor: - currCursor = {} - currCursor['x'] = column - currCursor['y'] = line - result += textLines[line][column] - result += ' ' - return result, currCursor - -''' -t = 'hallo\nwelt!' -old = ((1,1,0,0),(1,1,0,0),(1,1,0,0),(1,1,0,0),(1,1,0,0),(1,1,0,0),(1,1,0,0),(1,1,0,0),(1,1,0,0),(1,1,0,0)) -new = ((0,1,1,1),(1,1,1,1),(1,1,1,1),(1,1,1,1),(1,1,1,1),(1,1,0,0),(1,1,0,0),(1,1,0,0),(1,1,0,0),(1,1,0,0)) - -trackHighlights(old,new,t,5) -''' - -class headLineManipulation: - def __init__(self): - self.regExSingle = re.compile(r'(([^\w\s])\2{5,})') - self.regExDouble = re.compile(r'([^\w\s]{2,}){5,}') - def replaceHeadLines(self, text): - result = '' - newText = '' - lastPos = 0 - for match in self.regExDouble.finditer(text): - span = match.span() - newText += text[lastPos:span[0]] - numberOfChars = len(text[span[0]:span[1]]) - name = text[span[0]:span[1]][:2] - if name.strip(name[0]) == '': - newText += ' ' + str(numberOfChars) + ' ' + name[0] + ' ' - else: - newText += ' ' + str(int(numberOfChars / 2)) + ' ' + name + ' ' - lastPos = span[1] - newText += ' ' + text[lastPos:] - lastPos = 0 - for match in self.regExSingle.finditer(newText): - span = match.span() - result += text[lastPos:span[0]] - numberOfChars = len(newText[span[0]:span[1]]) - name = newText[span[0]:span[1]][:2] - if name.strip(name[0]) == '': - result += ' ' + str(numberOfChars) + ' ' + name[0] + ' ' - else: - result += ' ' + str(int(numberOfChars / 2)) + ' ' + name + ' ' - lastPos = span[1] - result += ' ' + newText[lastPos:] - return result -