2019-03-29 21:07:01 +00:00
|
|
|
import collections
|
2019-03-29 20:01:40 +00:00
|
|
|
import os
|
2019-03-29 15:17:05 +00:00
|
|
|
import struct
|
|
|
|
import sys
|
|
|
|
import time
|
2019-03-29 20:01:40 +00:00
|
|
|
import fcntl
|
|
|
|
import select
|
|
|
|
import termios
|
|
|
|
import tty
|
|
|
|
|
|
|
|
def writeout(data):
|
|
|
|
done = False
|
|
|
|
while not done:
|
|
|
|
try:
|
|
|
|
sys.stdout.write(data)
|
|
|
|
done = True
|
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
|
2019-03-29 15:17:05 +00:00
|
|
|
def main(binfile, txtfile):
|
|
|
|
binf = open(binfile, 'r')
|
|
|
|
txtf = open(txtfile, 'r')
|
2019-03-29 21:07:01 +00:00
|
|
|
records = collections.deque([])
|
|
|
|
records.append(get_next_text_record(binf))
|
2019-03-29 20:01:40 +00:00
|
|
|
oldtcattr = termios.tcgetattr(sys.stdin.fileno())
|
|
|
|
tty.setraw(sys.stdin.fileno())
|
|
|
|
currfl = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL)
|
|
|
|
fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, currfl | os.O_NONBLOCK)
|
2019-03-29 21:07:01 +00:00
|
|
|
while True:
|
|
|
|
newdata = showrecords(records, txtf, binf)
|
2019-03-29 20:01:40 +00:00
|
|
|
select.select((sys.stdin,), (), (), 86400)
|
|
|
|
myinput = sys.stdin.read()
|
|
|
|
if myinput == '\x1b[C': # right
|
2019-03-29 21:07:01 +00:00
|
|
|
if newdata:
|
|
|
|
sys.stdout.write(newdata)
|
|
|
|
newdata = ''
|
|
|
|
if not records:
|
|
|
|
records.append(get_next_text_record(binf))
|
2019-03-29 20:01:40 +00:00
|
|
|
elif myinput == '\x1b[D': # left
|
|
|
|
sys.stdout.write('\x1b[2J\x1b[;H')
|
|
|
|
prevoffset = 1
|
2019-03-29 21:07:01 +00:00
|
|
|
restoreoffset = binf.tell() - 16
|
|
|
|
while len(records) < 16 and prevoffset > 0:
|
2019-03-29 20:01:40 +00:00
|
|
|
prevoffset = binf.tell() - 32
|
|
|
|
if prevoffset < 0:
|
|
|
|
prevoffset = 0
|
|
|
|
binf.seek(prevoffset)
|
|
|
|
record = binf.read(16)
|
2019-03-29 21:07:01 +00:00
|
|
|
if not record:
|
|
|
|
break
|
2019-03-29 20:01:40 +00:00
|
|
|
while record[1] != '\x02' and prevoffset > 0:
|
|
|
|
prevoffset = binf.tell() - 32
|
|
|
|
if prevoffset < 0:
|
|
|
|
prevoffset = 0
|
|
|
|
binf.seek(prevoffset)
|
|
|
|
record = binf.read(16)
|
|
|
|
if record[1] == '\x02':
|
2019-03-29 21:07:01 +00:00
|
|
|
records.appendleft(record)
|
|
|
|
else:
|
|
|
|
records.appendleft(get_next_text_record(binf))
|
|
|
|
binf.seek(restoreoffset if restoreoffset > 0 else 0)
|
2019-03-29 20:01:40 +00:00
|
|
|
elif myinput.lower() == 'q':
|
|
|
|
break
|
2019-03-29 21:07:01 +00:00
|
|
|
elif myinput.lower() == 'd':
|
|
|
|
print(repr(records))
|
|
|
|
print(repr(binf.tell()))
|
2019-03-29 20:01:40 +00:00
|
|
|
else:
|
2019-03-29 21:07:01 +00:00
|
|
|
records.append(get_next_text_record(binf))
|
2019-03-29 20:01:40 +00:00
|
|
|
currfl = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL)
|
|
|
|
fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, currfl ^ os.O_NONBLOCK)
|
|
|
|
termios.tcsetattr(sys.stdin.fileno(), termios.TCSANOW, oldtcattr)
|
|
|
|
sys.stdout.write('\x1b[m')
|
|
|
|
|
2019-03-29 21:07:01 +00:00
|
|
|
def get_next_text_record(binf):
|
|
|
|
record = binf.read(16)
|
|
|
|
while record and record[1] != '\x02':
|
|
|
|
record = binf.read(16)
|
|
|
|
return record
|
|
|
|
|
|
|
|
|
|
|
|
def showrecords(records, txtf, binf):
|
|
|
|
extradata = ''
|
|
|
|
while records and records[0] and not extradata:
|
|
|
|
record = records.popleft()
|
2019-03-29 15:17:05 +00:00
|
|
|
recs = struct.unpack('!BBIHIBBH', record)
|
|
|
|
offset = recs[2]
|
|
|
|
size = recs[3]
|
|
|
|
tstamp = recs[4]
|
2019-03-29 20:01:40 +00:00
|
|
|
if recs[1] == 2:
|
|
|
|
tstamp = time.strftime('%m/%d %H:%M:%S ', time.localtime(tstamp))
|
|
|
|
txtf.seek(offset)
|
|
|
|
currdata = txtf.read(size)
|
2019-03-29 21:07:01 +00:00
|
|
|
if not records and not currdata.startswith('\x1b[2J') and '\x1b[2J' in currdata:
|
|
|
|
currdata, extradata = currdata.split('\x1b[2J', 1)
|
|
|
|
extradata = '\x1b[2J' + extradata
|
2019-03-29 20:01:40 +00:00
|
|
|
sys.stdout.write(currdata)
|
|
|
|
sys.stdout.write('\x1b]0;{0}\x07'.format(tstamp))
|
|
|
|
sys.stdout.flush()
|
2019-03-29 21:07:01 +00:00
|
|
|
return extradata
|
2019-03-29 20:01:40 +00:00
|
|
|
|
2019-03-29 15:17:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
binfile = sys.argv[1]
|
|
|
|
txtfile = sys.argv[2]
|
|
|
|
main(binfile, txtfile)
|
|
|
|
|