1
0
rtx/rtxMask.py

116 lines
2.9 KiB
Python
Raw Normal View History

2016-05-23 16:01:04 +02:00
# -*- coding: UTF-8 -*-
'''
rtx - RetroText
rtxMask: Klasse zur Verarbeitung einer Eingabemaske
by Anna Christina Naß <acn@acn.wtf>
released under GPL
'''
import glob
import logging
import config
import cept
class rtxMask:
""" Klasse zur Verarbeitung einer Eingabemaske
"""
lines = []
answers = []
def __init__(self):
2016-05-31 15:35:32 +02:00
def addLine(x, y, length):
2016-05-23 16:01:04 +02:00
""" adds a new line for the mask to the object """
2016-05-31 15:35:32 +02:00
newLine['x'] = x
newLine['y'] = y
newLine['length'] = length
self.lines.append(newLine)
2016-05-23 16:01:04 +02:00
def get_answer(self, nr):
""" returns the answer for a specific line """
if nr >= 0 and nr <= len(self.answers):
return self.answers[nr]
else
return False
def get_answers(self):
""" returns the array of all answers """
return self.answers
def _pos_cur_to_line(self, line):
""" positions the cursor to the beginning of a line """
2016-05-31 15:35:32 +02:00
zeile=self.lines[line]
self._pos_cur_to_xy(zeile['x'], zeile['y'])
2016-05-23 16:01:04 +02:00
def _pos_cur_to_xy(self, x, y):
""" positions the cursor to a (x,y) position on screen """
toX=0x41 + x
toY=0x41 + y
gotoString = "\x1f" + chr(toX) + chr(toY)
glob.ser.write(bytes(gotoString, "latin-1"))
2016-05-31 15:35:32 +02:00
def _set_str_at_pos(self, char, pos, line):
""" changes the 'char' at a position 'pos' of a specific answer 'line' """
zeile = self.answers[line]
# if the line is shorter than the position, add spaces
while len(zeile) < pos:
zeile += " "
zeile = zeile[:pos-1] + char + zeile[pos:]
answers[line] = zeile
2016-06-01 17:14:09 +02:00
def _cut_off_rest_of_line(self, line, pos):
""" erases the rest of the line from the 'lines' variable and from the screen """
zeile = self.answers[line]
self.answers[line] = zeile[:pos]
if zeile[pos:] != "":
numOfSpaces = len(zeile[pos:])
spaces = " " * numOfSpaces
backSpaces = "\x08" * numOfSpaces
glob.ser.write(bytes(spaces + backSpaces, "latin-1"))
2016-05-23 16:01:04 +02:00
def process_input(self):
""" reads the input from the client, fills the mask and gets the input """
2016-05-31 15:35:32 +02:00
if len(lines) == 0:
logging.debug("Keine Zeilen in der Maske definiert!")
return False
# position the cursor into the first field
self._pos_cur_to_line(lines[1])
2016-06-01 17:14:09 +02:00
posx = 0 # cursor position inside the line - max. position is length-1
posy = 0 # the current line - max. number is len(lines)-1
2016-05-31 15:35:32 +02:00
weiter = True
# read input and interpret it
while weiter==True:
2016-06-01 17:14:09 +02:00
# if the modem hangs up, exit the function:
2016-05-31 15:35:32 +02:00
#if config.MODE == "modem" and glob.ser.getCD() == False:
# weiter=False
in_byte = glob.ser.read(1)
instr = str(in_byte, encoding="latin-1")
echostr = instr
if instr == cept.TER or instr == cept.CR:
2016-06-01 17:14:09 +02:00
if posy == len(lines)-1:
# this was the last line, now send the data
# TODO
weiter = False
2016-05-31 15:35:32 +02:00
elif instr.isalnum():
self._set_str_at_pos(instr, posx, posy)
posx += 1
elif instr == cept.BSP:
answers[posy] = answers[posy][:-1]
posx -= 1
else:
echostr = ""
# lineinput == Zeilenende beachten
glob.ser.write(bytes(echostr, "latin-1"))
2016-05-23 16:01:04 +02:00