scripts: plot.py: Tweaked -H/--height to allow negative carve-outs

This finally solves the how-do-I-make-space-for-shell-prompts problem:

- plot.py -H0  => use full terminal height
- plot.py -H-1 => use height-1, making space for shell prompts
- plot.py -H   => automatic based on other flags

While also allowing other carveouts in case your prompt takes up more
than 1 line.

Unfortunately this does make -H (no arg) subtly different from -H0, but
sometimes you can't have everything.
This commit is contained in:
Christopher Haster
2025-03-15 03:11:12 -05:00
parent 5e8344a1c6
commit 6c36f76ffd

View File

@ -1105,7 +1105,6 @@ def main_(f, csv_paths, *,
legend_below=False, legend_below=False,
subplot={}, subplot={},
subplots=[], subplots=[],
keep_open=False,
**args): **args):
# give f an writeln function # give f an writeln function
def writeln(s=''): def writeln(s=''):
@ -1339,22 +1338,18 @@ def main_(f, csv_paths, *,
# figure out our canvas size # figure out our canvas size
if width is None: if width is None:
width_ = min(80, shutil.get_terminal_size((80, None))[0]) width_ = min(80, shutil.get_terminal_size((80, 5))[0])
elif width: elif width > 0:
width_ = width width_ = width
else: else:
width_ = shutil.get_terminal_size((80, None))[0] width_ = max(0, shutil.get_terminal_size((80, 5))[0] + width)
if height is None: if height is None:
height_ = 17 + len(title_) + len(xlabel_) height_ = 17 + len(title_) + len(xlabel_)
elif height: elif height > 0:
height_ = height height_ = height
else: else:
height_ = shutil.get_terminal_size((None, height_ = max(0, shutil.get_terminal_size((80, 5))[1] + height)
17 + len(title_) + len(xlabel_)))[1]
# make space for shell prompt
if not keep_open:
height_ -= 1
# carve out space for the xlabel # carve out space for the xlabel
height_ -= len(xlabel_) height_ -= len(xlabel_)
@ -1725,13 +1720,12 @@ def main_(f, csv_paths, *,
def main(csv_paths, *, def main(csv_paths, *,
height=None,
keep_open=False, keep_open=False,
head=False, head=False,
cat=False, cat=False,
sleep=False, sleep=False,
**args): **args):
# note main_ still wants keep-open for header padding
# keep-open? # keep-open?
if keep_open: if keep_open:
try: try:
@ -1743,12 +1737,13 @@ def main(csv_paths, *,
if cat: if cat:
main_(sys.stdout, csv_paths, main_(sys.stdout, csv_paths,
keep_open=False, # make space for shell prompt
height=height if height is not False else -1,
**args) **args)
else: else:
ring = RingIO(head=head) ring = RingIO(head=head)
main_(ring, csv_paths, main_(ring, csv_paths,
keep_open=True, height=height if height is not False else 0,
**args) **args)
ring.draw() ring.draw()
@ -1770,7 +1765,8 @@ def main(csv_paths, *,
# single-pass? # single-pass?
else: else:
main_(sys.stdout, csv_paths, main_(sys.stdout, csv_paths,
keep_open=False, # make space for shell prompt
height=height if height is not False else -1,
**args) **args)
@ -1886,14 +1882,15 @@ if __name__ == "__main__":
nargs='?', nargs='?',
type=lambda x: int(x, 0), type=lambda x: int(x, 0),
const=0, const=0,
help="Width in columns. 0 uses the terminal width. Defaults to " help="Width in columns. <=0 uses the terminal width. Defaults "
"min(terminal, 80).") "to min(terminal, 80).")
parser.add_argument( parser.add_argument(
'-H', '--height', '-H', '--height',
nargs='?', nargs='?',
type=lambda x: int(x, 0), type=lambda x: int(x, 0),
const=0, const=False,
help="Height in rows. 0 uses the terminal height. Defaults to 17.") help="Height in rows. <=0 uses the terminal height. Defaults "
"to 17.")
parser.add_argument( parser.add_argument(
'-X', '--xlim', '-X', '--xlim',
type=lambda x: tuple( type=lambda x: tuple(