Added teepipe.py and watch.py

This commit is contained in:
Christopher Haster
2022-10-16 14:27:14 -05:00
parent 3a33c3795b
commit b2a2cc9a19
16 changed files with 509 additions and 47 deletions
+53 -21
View File
@@ -17,10 +17,10 @@ import math as m
import os
import re
import shutil
import threading as th
import time
CHARS = 'rpe.'
COLORS = ['42', '45', '44', '']
@@ -42,6 +42,7 @@ CHARS_BRAILLE = (
def openio(path, mode='r', buffering=-1):
# allow '-' for stdin/stdout
if path == '-':
if mode == 'r':
return os.fdopen(os.dup(sys.stdin.fileno()), mode, buffering)
@@ -81,32 +82,44 @@ class LinesIO:
if maxlen != self.lines.maxlen:
self.lines = co.deque(self.lines, maxlen=maxlen)
last_lines = 1
canvas_lines = 1
def draw(self):
# did terminal size change?
if self.maxlen == 0:
self.resize(0)
# first thing first, give ourself a canvas
while LinesIO.last_lines < len(self.lines):
while LinesIO.canvas_lines < len(self.lines):
sys.stdout.write('\n')
LinesIO.last_lines += 1
LinesIO.canvas_lines += 1
for j, line in enumerate(self.lines):
# clear the bottom of the canvas if we shrink
shrink = LinesIO.canvas_lines - len(self.lines)
if shrink > 0:
for i in range(shrink):
sys.stdout.write('\r')
if shrink-1-i > 0:
sys.stdout.write('\x1b[%dA' % (shrink-1-i))
sys.stdout.write('\x1b[K')
if shrink-1-i > 0:
sys.stdout.write('\x1b[%dB' % (shrink-1-i))
sys.stdout.write('\x1b[%dA' % shrink)
LinesIO.canvas_lines = len(self.lines)
for i, line in enumerate(self.lines):
# move cursor, clear line, disable/reenable line wrapping
sys.stdout.write('\r')
if len(self.lines)-1-j > 0:
sys.stdout.write('\x1b[%dA' % (len(self.lines)-1-j))
if len(self.lines)-1-i > 0:
sys.stdout.write('\x1b[%dA' % (len(self.lines)-1-i))
sys.stdout.write('\x1b[K')
sys.stdout.write('\x1b[?7l')
sys.stdout.write(line)
sys.stdout.write('\x1b[?7h')
if len(self.lines)-1-j > 0:
sys.stdout.write('\x1b[%dB' % (len(self.lines)-1-j))
if len(self.lines)-1-i > 0:
sys.stdout.write('\x1b[%dB' % (len(self.lines)-1-i))
sys.stdout.flush()
# space filling Hilbert-curve
#
# note we memoize the last curve since this is a bit expensive
@@ -801,23 +814,39 @@ def main(path='-', *,
else:
ring = LinesIO(lines)
ptime = time.time()
# if sleep print in background thread to avoid getting stuck in a read call
event = th.Event()
lock = th.Lock()
if sleep:
done = False
def background():
while not done:
event.wait()
event.clear()
with lock:
draw(ring)
if not cat:
ring.draw()
time.sleep(sleep or 0.01)
th.Thread(target=background, daemon=True).start()
try:
while True:
with openio(path) as f:
changed = 0
for line in f:
changed += parse(line)
with lock:
changed += parse(line)
# need to redraw?
if (changed
and (not coalesce or changed >= coalesce)
and (not sleep or time.time()-ptime >= sleep)):
draw(ring)
if not cat:
ring.draw()
changed = 0
ptime = time.time()
# need to redraw?
if changed and (not coalesce or changed >= coalesce):
if sleep:
event.set()
else:
draw(ring)
if not cat:
ring.draw()
changed = 0
if not keep_open:
break
@@ -829,6 +858,9 @@ def main(path='-', *,
except KeyboardInterrupt:
pass
if sleep:
done = True
lock.acquire() # avoids https://bugs.python.org/issue42717
if not cat:
sys.stdout.write('\n')