fenrir/src/fenrirscreenreader/core/barrierManager.py

80 lines
2.7 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
2018-07-07 09:35:07 -04:00
def handleLineBarrier(self, text, xCursor, yCursor, output=True, doInterrupt=True):
isBarrier = False
try:
isBarrier, sayLine = self.getBarrierText(text, xCursor, yCursor)
except Exception as e:
return False, ''
2018-06-17 18:15:35 -04:00
self.updateBarrierChange(isBarrier)
2018-07-07 09:35:07 -04:00
if self.isBarrierChange():
if output:
if isBarrier:
self.env['runtime']['outputManager'].playSoundIcon(soundIcon='BarrierStart', interrupt=doInterrupt)
else:
self.env['runtime']['outputManager'].playSoundIcon(soundIcon='BarrierEnd', interrupt=doInterrupt)
if not isBarrier:
sayLine = ''
2019-04-27 10:11:30 -04:00
return isBarrier, sayLine
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
2018-07-07 09:35:07 -04:00
leftBarriers = self.env['runtime']['settingsManager'].getSetting('barrier', 'leftBarriers')
rightBarriers = self.env['runtime']['settingsManager'].getSetting('barrier', 'rightBarriers')
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
2018-07-07 09:35:07 -04:00
2018-07-04 10:23:38 -04:00
for b in leftBarriers:
if line[:offset + 1].count(b) > line[offset:].count(b):
offset = xCursor - 1
2018-07-07 11:21:22 -04:00
start = line[:offset].rfind(b)
2018-07-04 10:23:38 -04:00
if start != -1:
2019-04-27 10:08:19 -04:00
start += 1
2018-07-04 10:23:38 -04:00
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:
2018-07-07 11:21:22 -04:00
end = line[start:].find(b)
2018-07-04 10:23:38 -04:00
if end != -1:
2018-07-07 11:21:22 -04:00
end = start + end
2018-07-04 10:23:38 -04:00
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
2018-06-17 18:15:35 -04:00
return True, line[start:end]