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:
|
2016-06-02 16:50:10 +02:00
|
|
|
""" Klasse zur Verarbeitung einer Eingabemaske
|
|
|
|
"""
|
|
|
|
|
|
|
|
lines = []
|
|
|
|
answers = []
|
|
|
|
|
|
|
|
def __init__(self):
|
2016-06-10 14:48:28 +02:00
|
|
|
return
|
2016-06-02 16:50:10 +02:00
|
|
|
|
2016-06-10 14:48:28 +02:00
|
|
|
def addLine(self, x, y, length):
|
2016-06-02 16:50:10 +02:00
|
|
|
""" adds a new line for the mask to the object """
|
2016-06-10 14:48:28 +02:00
|
|
|
newLine = {}
|
2016-06-02 16:50:10 +02:00
|
|
|
newLine['x'] = x
|
|
|
|
newLine['y'] = y
|
|
|
|
newLine['length'] = length
|
|
|
|
self.lines.append(newLine)
|
|
|
|
|
|
|
|
def get_answer(self, nr):
|
|
|
|
""" returns the answer for a specific line """
|
|
|
|
if nr >= 0 and nr <= len(self.answers):
|
|
|
|
return self.answers[nr]
|
2016-06-10 14:48:28 +02:00
|
|
|
else:
|
2016-06-02 16:50:10 +02:00
|
|
|
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-06-10 14:48:28 +02:00
|
|
|
self._pos_cur_to_xy(line['x'], line['y'])
|
2016-06-02 16:50:10 +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"))
|
|
|
|
|
|
|
|
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:]
|
2016-06-10 14:48:28 +02:00
|
|
|
self.answers[line] = zeile
|
2016-06-06 16:52:53 +02:00
|
|
|
|
2016-06-02 16:50:10 +02:00
|
|
|
def process_input(self):
|
|
|
|
""" reads the input from the client, fills the mask and gets the input """
|
|
|
|
|
|
|
|
# if there have no input lines been defined, no input can be processed - so exit the function
|
2016-06-10 14:48:28 +02:00
|
|
|
if len(self.lines) == 0:
|
2016-06-02 16:50:10 +02:00
|
|
|
logging.debug("Keine Zeilen in der Maske definiert!")
|
|
|
|
return False
|
|
|
|
|
|
|
|
# position the cursor into the first field
|
2016-06-10 14:48:28 +02:00
|
|
|
self._pos_cur_to_line(self.lines[0])
|
2016-06-02 16:50:10 +02:00
|
|
|
|
|
|
|
posx = 0 # cursor position inside the line - max. position is length-1
|
2016-06-10 14:48:28 +02:00
|
|
|
posy = 0 # the current line - max. number is len(self.lines)-1
|
2016-06-02 16:50:10 +02:00
|
|
|
|
|
|
|
weiter = True
|
|
|
|
# read input and interpret it
|
|
|
|
while weiter==True:
|
|
|
|
# if the modem hangs up, exit the function:
|
|
|
|
#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:
|
|
|
|
# go to the beginning of the next line
|
|
|
|
# or finish the mask
|
2016-06-10 14:48:28 +02:00
|
|
|
if posy == len(self.lines)-1:
|
2016-06-06 16:52:53 +02:00
|
|
|
weiter = False
|
|
|
|
self._send_data()
|
2016-06-02 16:50:10 +02:00
|
|
|
else:
|
|
|
|
# go to the beginning of the next line
|
|
|
|
posy += 1
|
|
|
|
posx = 0
|
2016-06-10 14:48:28 +02:00
|
|
|
self._pos_cur_to_line(self.lines[posy])
|
2016-06-02 16:50:10 +02:00
|
|
|
elif instr == cept.BSP:
|
2016-06-10 14:48:28 +02:00
|
|
|
self.answers[posy] = self.answers[posy][:-1]
|
2016-06-02 16:50:10 +02:00
|
|
|
posx -= 1
|
|
|
|
elif instr == cept.UP:
|
|
|
|
# if the cursor is in the first line, we can't go higher...
|
|
|
|
# so we ignore the keypress
|
|
|
|
echostr = "" # no echo needed, we position the curser ourselves
|
|
|
|
if posy > 0:
|
|
|
|
posy -= 1
|
|
|
|
posx = 0
|
2016-06-10 14:48:28 +02:00
|
|
|
self._pos_cur_to_line(self.lines[posy])
|
2016-06-02 16:50:10 +02:00
|
|
|
elif instr == cept.DOWN:
|
|
|
|
# same here: last line => it can't go lower => ignore
|
|
|
|
echostr = "" # no echo needed, we position the curser ourselves
|
|
|
|
if posy < len(self.lines):
|
|
|
|
posy += 1
|
|
|
|
posx = 0
|
2016-06-10 14:48:28 +02:00
|
|
|
self._pos_cur_to_line(self.lines[posy])
|
2016-06-02 16:50:10 +02:00
|
|
|
elif instr == cept.LEFT:
|
|
|
|
if posx > 0:
|
|
|
|
posx -= 1
|
|
|
|
else:
|
|
|
|
echostr = ""
|
|
|
|
elif instr == cept.RIGHT:
|
2016-06-10 14:48:28 +02:00
|
|
|
if posx < len(self.answers[posy]):
|
2016-06-02 16:50:10 +02:00
|
|
|
posx += 1
|
|
|
|
else:
|
|
|
|
echostr = ""
|
2016-06-06 16:52:53 +02:00
|
|
|
elif instr == cept.HOME:
|
|
|
|
posy = 0
|
|
|
|
posx = 0
|
2016-06-10 14:48:28 +02:00
|
|
|
self._pos_cur_to_line(self.lines[posy])
|
2016-06-06 16:52:53 +02:00
|
|
|
echostr = ""
|
2016-06-02 16:50:10 +02:00
|
|
|
elif instr == cept.SEND: # ist das DCT??
|
2016-06-06 16:52:53 +02:00
|
|
|
weiter = False
|
|
|
|
self._send_data()
|
2016-06-02 16:50:10 +02:00
|
|
|
elif instr.isalnum():
|
|
|
|
# "normal" input
|
|
|
|
# TODO: lineinput == Zeilenende beachten
|
2016-06-10 14:48:28 +02:00
|
|
|
#self._set_str_at_pos(instr, posx, posy)
|
2016-06-02 16:50:10 +02:00
|
|
|
posx += 1
|
|
|
|
else:
|
|
|
|
# ignore the rest and do not echo it :)
|
|
|
|
echostr = ""
|
|
|
|
|
|
|
|
# now send the echo
|
|
|
|
glob.ser.write(bytes(echostr, "latin-1"))
|
2016-05-23 16:01:04 +02:00
|
|
|
|
2016-06-06 16:52:53 +02:00
|
|
|
def _send_data(self):
|
|
|
|
""" strip spaces from the right of the answers and save them """
|
|
|
|
new_answers = []
|
|
|
|
for answer in self.answers:
|
|
|
|
temp = answer.rstrip()
|
|
|
|
new_answers.append(temp)
|
|
|
|
self.answers = new_answers
|
2016-06-10 14:48:28 +02:00
|
|
|
print(self.answers)
|
2016-06-06 16:52:53 +02:00
|
|
|
|
|
|
|
|
2016-05-23 16:01:04 +02:00
|
|
|
|