fenrir/src/fenrirscreenreader/core/barrierManager.py

102 lines
3.9 KiB
Python
Raw Normal View History

2018-06-17 18:15:35 -04:00
#!/bin/python
# -*- coding: utf-8 -*-
# Fenrir TTY screen reader
# By Chrys, Storm Dragon, and contributers.
from fenrirscreenreader.core import debug
import re, string
class barrierManager():
def __init__(self):
self.currIsBarrier = False
self.prefIsBarrier = False
def initialize(self, environment):
self.env = environment
def shutdown(self):
pass
def updateBarrierChange(self, isBarrier):
self.prefIsBarrier = self.currIsBarrier
self.currIsBarrier = isBarrier
def resetBarrierChange(self):
self.currIsBarrier = False
self.prefIsBarrier = False
def isBarrierChange(self):
return self.currIsBarrier != self.prefIsBarrier
def handleLineBarrier(self, line, xCursor, output=True, doInterrupt=True):
isBarrier, sayLine = self.getBarrierText(line, xCursor)
self.updateBarrierChange(isBarrier)
#if self.isBarrierChange():
if isBarrier:
if output:
2018-06-18 14:51:18 -04:00
self.env['runtime']['outputManager'].playSoundIcon(soundIcon='BarrierFound', interrupt=doInterrupt)
2018-06-17 18:15:35 -04:00
return sayLine
2018-07-04 10:23:38 -04:00
def hasBorder(self, text, xCursor, yCursor, validBorder):
2018-06-17 18:15:35 -04:00
# check for corners here
2018-07-04 10:23:38 -04:00
lastLineNo = len(text) - 1
if yCursor <= 0:
if not (text[0][xCursor] in validBorder):
return False
if len(text) > 1:
if not (text[1][xCursor] in validBorder):
return False
if len(text) > 2:
if not (text[2][xCursor] in validBorder):
return False
elif yCursor >= lastLineNo:
if not (text[lastLineNo][xCursor] in validBorder):
return False
if len(text) > 1:
if not (text[lastLineNo - 1][xCursor] in validBorder):
return False
if len(text) > 2:
if not (text[lastLineNo - 2][xCursor] in validBorder):
return False
else:
if not (text[yCursor][xCursor] in validBorder):
return False
if not (text[yCursor - 1][xCursor] in validBorder):
return False
if not (text[yCursor + 1][xCursor] in validBorder):
return False
2018-06-17 18:15:35 -04:00
return True
2018-07-04 10:23:38 -04:00
def getBarrierText(self, text, xCursor, yCursor):
line = text[yCursor]
if not self.env['runtime']['settingsManager'].getSettingAsBool('barrier', 'enabled'):
return False, line
offset = xCursor
leftBarriers = self.env['runtime']['settingsManager'].getSetting('barrier', 'barrier')
rightBarriers = self.env['runtime']['settingsManager'].getSetting('barrier', 'grabDevices')
2018-06-17 18:15:35 -04:00
# is the cursor at the begin or end of an entry:
#print(line[:offset + 1].count('│'),line[offset:].count('│'))
2018-07-04 10:23:38 -04:00
# start
for b in leftBarriers:
if line[:offset + 1].count(b) > line[offset:].count(b):
offset = xCursor - 1
start = line[:offset + 1].rfind(b) + 1
if start != -1:
if not self.hasBorder(text, xCursor, yCursor):
start = -1
break
2018-06-17 18:15:35 -04:00
if start == -1:
2018-07-04 10:23:38 -04:00
return False, line
# end
for b in rightBarriers:
end = line[offset + 1:].find(b)
if end != -1:
if not self.hasBorder(text, xCursor, yCursor):
end = -1
break
2018-06-17 18:15:35 -04:00
if end == -1:
2018-07-04 10:23:38 -04:00
return False, line
if start == end:
2018-06-17 18:15:35 -04:00
return False, line
2018-07-04 10:23:38 -04:00
end += offset + 1
2018-06-17 18:15:35 -04:00
return True, line[start:end]