scripts: Adopted -n/--lines in most ascii art scripts

The notable exception being plot.py, where line-level history doesn't
really make sense.

These scripts all default to height=1, and -n/--lines can be useful for
viewing changes over time.

In theory you could achieve something similar to this with tailpipe.py,
but you would lose the header info, which is useful.

---

Note, as a point of simplicity, we do _not_ show sub-char history like
we used to in tracebd.py. That was way too complicated for what it was
worth.
This commit is contained in:
Christopher Haster
2025-04-10 17:12:56 -05:00
parent 8e3760c5b8
commit fc5bfdae14
5 changed files with 113 additions and 29 deletions

View File

@ -1357,8 +1357,11 @@ def main_(f, paths, *,
def main(paths, *,
width=None,
height=None,
no_header=None,
keep_open=False,
lines=None,
head=False,
cat=False,
sleep=False,
@ -1366,23 +1369,47 @@ def main(paths, *,
# keep-open?
if keep_open:
try:
# keep track of history if lines specified
if lines is not None:
ring = RingIO(lines+1
if not no_header and lines > 0
else lines)
while True:
# register inotify before running the command, this avoids
# modification race conditions
if Inotify:
inotify = Inotify(paths)
# cat? write directly to stdout
if cat:
main_(sys.stdout, paths,
width=width,
# make space for shell prompt
height=height if height is not False else -1,
height=-1 if height is ... else height,
no_header=no_header,
**args)
# not cat? write to a bounded ring
else:
ring = RingIO(head=head)
main_(ring, paths,
height=height if height is not False else 0,
ring_ = RingIO(head=head)
main_(ring_, paths,
width=width,
height=0 if height is ... else height,
no_header=no_header,
**args)
ring.draw()
# no history? draw immediately
if lines is None:
ring_.draw()
# history? merge with previous lines
else:
# write header separately?
if not no_header:
if not ring.lines:
ring.lines.append('')
ring.lines.extend(it.islice(ring_.lines, 1, None))
ring.lines[0] = ring_.lines[0]
else:
ring.lines.extend(ring_.lines)
ring.draw()
# try to inotifywait
if Inotify:
@ -1402,8 +1429,10 @@ def main(paths, *,
# single-pass?
else:
main_(sys.stdout, paths,
width=width,
# make space for shell prompt
height=height if height is not False else -1,
height=-1 if height is ... else height,
no_header=no_header,
**args)
@ -1518,7 +1547,7 @@ if __name__ == "__main__":
'-H', '--height',
nargs='?',
type=lambda x: int(x, 0),
const=False,
const=..., # handles shell prompt spacing, which is a bit subtle
help="Height in rows. <=0 uses the terminal height. Defaults "
"to 1.")
parser.add_argument(
@ -1630,6 +1659,13 @@ if __name__ == "__main__":
'-k', '--keep-open',
action='store_true',
help="Continue to open and redraw the CSV files in a loop.")
parser.add_argument(
'-n', '--lines',
nargs='?',
type=lambda x: int(x, 0),
const=0,
help="Show this many lines of history. <=0 uses the terminal "
"height. Defaults to 1.")
parser.add_argument(
'-^', '--head',
action='store_true',