Added -^/--head to watch.py and other scripts, RingIO tweaks

This lets you view the first n lines of output instead of the last n
lines, as though the output was piped through head.

This is how the standard watch command works, and can be more useful
when most of the information is at the top, such as in our dbg*.py
scripts (watch.py was originally used as a sort of inotifywait-esque
build runner, which is the main reason it's different).

To make this work, RingIO (renamed from LinesIO) now uses terminal
height as a part of its canvas rendering. This has the added benefit of
more rigorously enforcing the canvas boundaries, but risks breaking when
not associated with a terminal. But that raises the question, does
RingIO even make sense without a terminal?

Worst case you can bypass all of this with -z/--cat.
This commit is contained in:
Christopher Haster
2024-06-02 00:07:35 -05:00
parent 9914897e39
commit a231bbac6e
4 changed files with 147 additions and 103 deletions

View File

@@ -167,9 +167,10 @@ else:
else: else:
self.add_watch(path, flags) self.add_watch(path, flags)
class LinesIO: class RingIO:
def __init__(self, maxlen=None): def __init__(self, maxlen=None, head=False):
self.maxlen = maxlen self.maxlen = maxlen
self.head = head
self.lines = co.deque(maxlen=maxlen) self.lines = co.deque(maxlen=maxlen)
self.tail = io.StringIO() self.tail = io.StringIO()
@@ -177,6 +178,9 @@ class LinesIO:
if maxlen == 0: if maxlen == 0:
self.resize(0) self.resize(0)
def __len__(self):
return len(self.lines)
def write(self, s): def write(self, s):
# note using split here ensures the trailing string has no newline # note using split here ensures the trailing string has no newline
lines = s.split('\n') lines = s.split('\n')
@@ -204,35 +208,37 @@ class LinesIO:
if self.maxlen == 0: if self.maxlen == 0:
self.resize(0) self.resize(0)
# copy lines
lines = self.lines.copy()
# pad to fill any existing canvas, but truncate to terminal size
h = shutil.get_terminal_size((80, 5))[1]
lines.extend('' for _ in range(
len(lines),
min(RingIO.canvas_lines, h)))
while len(lines) > h:
if self.head:
lines.pop()
else:
lines.popleft()
# first thing first, give ourself a canvas # first thing first, give ourself a canvas
while LinesIO.canvas_lines < len(self.lines): while RingIO.canvas_lines < len(lines):
sys.stdout.write('\n') sys.stdout.write('\n')
LinesIO.canvas_lines += 1 RingIO.canvas_lines += 1
# clear the bottom of the canvas if we shrink # write lines from top to bottom so later lines overwrite earlier
shrink = LinesIO.canvas_lines - len(self.lines) # lines, note [xA/[xB stop at terminal boundaries
if shrink > 0: for i, line in enumerate(lines):
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 # move cursor, clear line, disable/reenable line wrapping
sys.stdout.write('\r') sys.stdout.write('\r')
if len(self.lines)-1-i > 0: if len(lines)-1-i > 0:
sys.stdout.write('\x1b[%dA' % (len(self.lines)-1-i)) sys.stdout.write('\x1b[%dA' % (len(lines)-1-i))
sys.stdout.write('\x1b[K') sys.stdout.write('\x1b[K')
sys.stdout.write('\x1b[?7l') sys.stdout.write('\x1b[?7l')
sys.stdout.write(line) sys.stdout.write(line)
sys.stdout.write('\x1b[?7h') sys.stdout.write('\x1b[?7h')
if len(self.lines)-1-i > 0: if len(lines)-1-i > 0:
sys.stdout.write('\x1b[%dB' % (len(self.lines)-1-i)) sys.stdout.write('\x1b[%dB' % (len(lines)-1-i))
sys.stdout.flush() sys.stdout.flush()
@@ -845,6 +851,7 @@ def main(csv_paths, *,
legend_below=False, legend_below=False,
subplot={}, subplot={},
subplots=[], subplots=[],
head=False,
cat=False, cat=False,
keep_open=False, keep_open=False,
sleep=None, sleep=None,
@@ -1402,7 +1409,7 @@ def main(csv_paths, *,
if cat: if cat:
draw(sys.stdout) draw(sys.stdout)
else: else:
ring = LinesIO() ring = RingIO(head=head)
draw(ring) draw(ring)
ring.draw() ring.draw()
@@ -1419,13 +1426,8 @@ def main(csv_paths, *,
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass
if cat: if not cat:
draw(sys.stdout) sys.stdout.write('\n')
else:
ring = LinesIO()
draw(ring)
ring.draw()
sys.stdout.write('\n')
else: else:
draw(sys.stdout) draw(sys.stdout)
@@ -1644,6 +1646,10 @@ if __name__ == "__main__":
'--subplot', '--subplot',
type=AppendSubplot.parse, type=AppendSubplot.parse,
help="Add subplot-specific arguments to the main plot.") help="Add subplot-specific arguments to the main plot.")
parser.add_argument(
'-^', '--head',
action='store_true',
help="Show the first n lines.")
parser.add_argument( parser.add_argument(
'-z', '--cat', '-z', '--cat',
action='store_true', action='store_true',

View File

@@ -29,9 +29,10 @@ def openio(path, mode='r', buffering=-1):
else: else:
return open(path, mode, buffering) return open(path, mode, buffering)
class LinesIO: class RingIO:
def __init__(self, maxlen=None): def __init__(self, maxlen=None, head=False):
self.maxlen = maxlen self.maxlen = maxlen
self.head = head
self.lines = co.deque(maxlen=maxlen) self.lines = co.deque(maxlen=maxlen)
self.tail = io.StringIO() self.tail = io.StringIO()
@@ -39,6 +40,9 @@ class LinesIO:
if maxlen == 0: if maxlen == 0:
self.resize(0) self.resize(0)
def __len__(self):
return len(self.lines)
def write(self, s): def write(self, s):
# note using split here ensures the trailing string has no newline # note using split here ensures the trailing string has no newline
lines = s.split('\n') lines = s.split('\n')
@@ -66,43 +70,49 @@ class LinesIO:
if self.maxlen == 0: if self.maxlen == 0:
self.resize(0) self.resize(0)
# copy lines
lines = self.lines.copy()
# pad to fill any existing canvas, but truncate to terminal size
h = shutil.get_terminal_size((80, 5))[1]
lines.extend('' for _ in range(
len(lines),
min(RingIO.canvas_lines, h)))
while len(lines) > h:
if self.head:
lines.pop()
else:
lines.popleft()
# first thing first, give ourself a canvas # first thing first, give ourself a canvas
while LinesIO.canvas_lines < len(self.lines): while RingIO.canvas_lines < len(lines):
sys.stdout.write('\n') sys.stdout.write('\n')
LinesIO.canvas_lines += 1 RingIO.canvas_lines += 1
# clear the bottom of the canvas if we shrink # write lines from top to bottom so later lines overwrite earlier
shrink = LinesIO.canvas_lines - len(self.lines) # lines, note [xA/[xB stop at terminal boundaries
if shrink > 0: for i, line in enumerate(lines):
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 # move cursor, clear line, disable/reenable line wrapping
sys.stdout.write('\r') sys.stdout.write('\r')
if len(self.lines)-1-i > 0: if len(lines)-1-i > 0:
sys.stdout.write('\x1b[%dA' % (len(self.lines)-1-i)) sys.stdout.write('\x1b[%dA' % (len(lines)-1-i))
sys.stdout.write('\x1b[K') sys.stdout.write('\x1b[K')
sys.stdout.write('\x1b[?7l') sys.stdout.write('\x1b[?7l')
sys.stdout.write(line) sys.stdout.write(line)
sys.stdout.write('\x1b[?7h') sys.stdout.write('\x1b[?7h')
if len(self.lines)-1-i > 0: if len(lines)-1-i > 0:
sys.stdout.write('\x1b[%dB' % (len(self.lines)-1-i)) sys.stdout.write('\x1b[%dB' % (len(lines)-1-i))
sys.stdout.flush() sys.stdout.flush()
def main(path='-', *, lines=5, cat=False, sleep=None, keep_open=False): def main(path='-', *,
lines=5,
cat=False,
sleep=None,
keep_open=False):
if cat: if cat:
ring = sys.stdout ring = sys.stdout
else: else:
ring = LinesIO(lines) ring = RingIO(lines)
# if sleep print in background thread to avoid getting stuck in a read call # if sleep print in background thread to avoid getting stuck in a read call
event = th.Event() event = th.Event()

View File

@@ -110,9 +110,10 @@ def rbydaddr(s):
return addr return addr
class LinesIO: class RingIO:
def __init__(self, maxlen=None): def __init__(self, maxlen=None, head=False):
self.maxlen = maxlen self.maxlen = maxlen
self.head = head
self.lines = co.deque(maxlen=maxlen) self.lines = co.deque(maxlen=maxlen)
self.tail = io.StringIO() self.tail = io.StringIO()
@@ -120,6 +121,9 @@ class LinesIO:
if maxlen == 0: if maxlen == 0:
self.resize(0) self.resize(0)
def __len__(self):
return len(self.lines)
def write(self, s): def write(self, s):
# note using split here ensures the trailing string has no newline # note using split here ensures the trailing string has no newline
lines = s.split('\n') lines = s.split('\n')
@@ -147,35 +151,37 @@ class LinesIO:
if self.maxlen == 0: if self.maxlen == 0:
self.resize(0) self.resize(0)
# copy lines
lines = self.lines.copy()
# pad to fill any existing canvas, but truncate to terminal size
h = shutil.get_terminal_size((80, 5))[1]
lines.extend('' for _ in range(
len(lines),
min(RingIO.canvas_lines, h)))
while len(lines) > h:
if self.head:
lines.pop()
else:
lines.popleft()
# first thing first, give ourself a canvas # first thing first, give ourself a canvas
while LinesIO.canvas_lines < len(self.lines): while RingIO.canvas_lines < len(lines):
sys.stdout.write('\n') sys.stdout.write('\n')
LinesIO.canvas_lines += 1 RingIO.canvas_lines += 1
# clear the bottom of the canvas if we shrink # write lines from top to bottom so later lines overwrite earlier
shrink = LinesIO.canvas_lines - len(self.lines) # lines, note [xA/[xB stop at terminal boundaries
if shrink > 0: for i, line in enumerate(lines):
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 # move cursor, clear line, disable/reenable line wrapping
sys.stdout.write('\r') sys.stdout.write('\r')
if len(self.lines)-1-i > 0: if len(lines)-1-i > 0:
sys.stdout.write('\x1b[%dA' % (len(self.lines)-1-i)) sys.stdout.write('\x1b[%dA' % (len(lines)-1-i))
sys.stdout.write('\x1b[K') sys.stdout.write('\x1b[K')
sys.stdout.write('\x1b[?7l') sys.stdout.write('\x1b[?7l')
sys.stdout.write(line) sys.stdout.write(line)
sys.stdout.write('\x1b[?7h') sys.stdout.write('\x1b[?7h')
if len(self.lines)-1-i > 0: if len(lines)-1-i > 0:
sys.stdout.write('\x1b[%dB' % (len(self.lines)-1-i)) sys.stdout.write('\x1b[%dB' % (len(lines)-1-i))
sys.stdout.flush() sys.stdout.flush()
@@ -662,6 +668,7 @@ def main(path='-', *,
width=None, width=None,
height=None, height=None,
lines=None, lines=None,
head=False,
cat=False, cat=False,
hilbert=False, hilbert=False,
lebesgue=False, lebesgue=False,
@@ -991,7 +998,7 @@ def main(path='-', *,
if cat: if cat:
ring = sys.stdout ring = sys.stdout
else: else:
ring = LinesIO(lines + (1 if not no_header else 0)) ring = RingIO(lines + (1 if not no_header else 0), head)
# if sleep print in background thread to avoid getting stuck in a read call # if sleep print in background thread to avoid getting stuck in a read call
event = th.Event() event = th.Event()
@@ -1159,6 +1166,10 @@ if __name__ == "__main__":
const=0, const=0,
help="Show this many lines of history. 0 uses the terminal height. " help="Show this many lines of history. 0 uses the terminal height. "
"Defaults to 5.") "Defaults to 5.")
parser.add_argument(
'-^', '--head',
action='store_true',
help="Show the first n lines.")
parser.add_argument( parser.add_argument(
'-z', '--cat', '-z', '--cat',
action='store_true', action='store_true',

View File

@@ -67,9 +67,10 @@ else:
else: else:
self.add_watch(path, flags) self.add_watch(path, flags)
class LinesIO: class RingIO:
def __init__(self, maxlen=None): def __init__(self, maxlen=None, head=False):
self.maxlen = maxlen self.maxlen = maxlen
self.head = head
self.lines = co.deque(maxlen=maxlen) self.lines = co.deque(maxlen=maxlen)
self.tail = io.StringIO() self.tail = io.StringIO()
@@ -77,6 +78,9 @@ class LinesIO:
if maxlen == 0: if maxlen == 0:
self.resize(0) self.resize(0)
def __len__(self):
return len(self.lines)
def write(self, s): def write(self, s):
# note using split here ensures the trailing string has no newline # note using split here ensures the trailing string has no newline
lines = s.split('\n') lines = s.split('\n')
@@ -104,40 +108,43 @@ class LinesIO:
if self.maxlen == 0: if self.maxlen == 0:
self.resize(0) self.resize(0)
# copy lines
lines = self.lines.copy()
# pad to fill any existing canvas, but truncate to terminal size
h = shutil.get_terminal_size((80, 5))[1]
lines.extend('' for _ in range(
len(lines),
min(RingIO.canvas_lines, h)))
while len(lines) > h:
if self.head:
lines.pop()
else:
lines.popleft()
# first thing first, give ourself a canvas # first thing first, give ourself a canvas
while LinesIO.canvas_lines < len(self.lines): while RingIO.canvas_lines < len(lines):
sys.stdout.write('\n') sys.stdout.write('\n')
LinesIO.canvas_lines += 1 RingIO.canvas_lines += 1
# clear the bottom of the canvas if we shrink # write lines from top to bottom so later lines overwrite earlier
shrink = LinesIO.canvas_lines - len(self.lines) # lines, note [xA/[xB stop at terminal boundaries
if shrink > 0: for i, line in enumerate(lines):
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 # move cursor, clear line, disable/reenable line wrapping
sys.stdout.write('\r') sys.stdout.write('\r')
if len(self.lines)-1-i > 0: if len(lines)-1-i > 0:
sys.stdout.write('\x1b[%dA' % (len(self.lines)-1-i)) sys.stdout.write('\x1b[%dA' % (len(lines)-1-i))
sys.stdout.write('\x1b[K') sys.stdout.write('\x1b[K')
sys.stdout.write('\x1b[?7l') sys.stdout.write('\x1b[?7l')
sys.stdout.write(line) sys.stdout.write(line)
sys.stdout.write('\x1b[?7h') sys.stdout.write('\x1b[?7h')
if len(self.lines)-1-i > 0: if len(lines)-1-i > 0:
sys.stdout.write('\x1b[%dB' % (len(self.lines)-1-i)) sys.stdout.write('\x1b[%dB' % (len(lines)-1-i))
sys.stdout.flush() sys.stdout.flush()
def main(command, *, def main(command, *,
lines=0, lines=0,
head=False,
cat=False, cat=False,
sleep=None, sleep=None,
keep_open=False, keep_open=False,
@@ -145,6 +152,11 @@ def main(command, *,
buffer=False, buffer=False,
ignore_errors=False, ignore_errors=False,
exit_on_error=False): exit_on_error=False):
if not command:
print('usage: %s [options] command' % sys.argv[0],
file=sys.stderr)
sys.exit(-1)
# if we have keep_open_paths, assume user wanted keep_open # if we have keep_open_paths, assume user wanted keep_open
if keep_open_paths and not keep_open: if keep_open_paths and not keep_open:
keep_open = True keep_open = True
@@ -171,7 +183,7 @@ def main(command, *,
if cat: if cat:
ring = sys.stdout ring = sys.stdout
else: else:
ring = LinesIO(lines) ring = RingIO(lines, head)
try: try:
# register inotify before running the command, this avoids # register inotify before running the command, this avoids
@@ -206,9 +218,10 @@ def main(command, *,
if not line: if not line:
break break
ring.write(line) if cat or not head or len(ring) < h:
if not cat and not buffer and not ignore_errors: ring.write(line)
ring.draw() if not cat and not buffer and not ignore_errors:
ring.draw()
mpty.close() mpty.close()
proc.wait() proc.wait()
@@ -262,6 +275,10 @@ if __name__ == "__main__":
const=0, const=0,
help="Show this many lines of history. 0 uses the terminal height. " help="Show this many lines of history. 0 uses the terminal height. "
"Defaults to 0.") "Defaults to 0.")
parser.add_argument(
'-^', '--head',
action='store_true',
help="Show the first n lines.")
parser.add_argument( parser.add_argument(
'-z', '--cat', '-z', '--cat',
action='store_true', action='store_true',