scripts: Added --no-stats to treemap.py/treemapd3.py

The previous behavior of -N/--no-header still rendering a header when
--title is also provided was confusing. I think this is a better API,
at the minor cost of needing to pass one more flag if you don't want
stats in the header.
This commit is contained in:
Christopher Haster
2025-03-07 02:10:26 -06:00
parent 0d134a2830
commit fb03e27baf
2 changed files with 40 additions and 23 deletions

View File

@@ -712,6 +712,7 @@ def main(csv_paths, *,
width=None,
height=None,
no_header=False,
no_stats=False,
to_scale=None,
aspect_ratio=(1,1),
title=None,
@@ -748,7 +749,10 @@ def main(csv_paths, *,
width_ = shutil.get_terminal_size((80, 5))[0]
if height is None:
height_ = 2 if title is not None or not no_header else 1
height_ = (2
if not no_header
and (title is not None or not no_stats)
else 1)
elif height:
height_ = height
else:
@@ -849,7 +853,10 @@ def main(csv_paths, *,
# create a canvas
canvas = Canvas(
width_,
height_ - (1 if title or not no_header else 0),
height_ - (1
if not no_header
and (title is not None or not no_stats)
else 0),
color=color,
dots=dots,
braille=braille)
@@ -967,23 +974,24 @@ def main(csv_paths, *,
canvas.label(*label__)
# print some summary info
if title:
title_ = punescape(title, tile.attrs)
if not no_header:
stat = tile.stat()
stat_ = 'total %d, avg %d +-%dσ, min %d, max %d' % (
stat['total'],
stat['mean'], stat['stddev'],
stat['min'], stat['max'])
if title and not no_header:
print('%s%*s%s' % (
title_,
max(width_-len(stat_)-len(title_), 0), ' ',
stat_))
elif title:
print(title_)
elif not no_header:
print(stat_)
if title:
title_ = punescape(title, tile.attrs)
if not no_stats:
stat = tile.stat()
stat_ = 'total %d, avg %d +-%dσ, min %d, max %d' % (
stat['total'],
stat['mean'], stat['stddev'],
stat['min'], stat['max'])
if title and not no_stats:
print('%s%*s%s' % (
title_,
max(width_-len(stat_)-len(title_), 0), ' ',
stat_))
elif title:
print(title_)
elif not no_stats:
print(stat_)
# draw canvas
for row in range(canvas.height//canvas.yscale):
@@ -1088,6 +1096,10 @@ if __name__ == "__main__":
'-N', '--no-header',
action='store_true',
help="Don't show the header.")
parser.add_argument(
'--no-stats',
action='store_true',
help="Don't show data stats in the header.")
parser.add_argument(
'--binary',
action='store_true',

View File

@@ -561,6 +561,7 @@ def main(csv_paths, output, *,
width=None,
height=None,
no_header=False,
no_stats=False,
to_scale=None,
aspect_ratio=(1,1),
title=None,
@@ -703,7 +704,7 @@ def main(csv_paths, output, *,
height__ = tile.height
# create space for header
if title is not None or not no_header:
if not no_header and (title is not None or not no_stats):
y__ += mt.ceil(FONT_SIZE * 1.3)
height__ -= min(mt.ceil(FONT_SIZE * 1.3), height__)
@@ -789,16 +790,16 @@ def main(csv_paths, output, *,
background=background_))
# create header
if title is not None or not no_header:
if not no_header and (title is not None or not no_stats):
f.write('<text fill="%(color)s">' % dict(
color='#ffffff' if dark else '#000000'))
if not no_header:
if not no_stats:
stat = tile.stat()
if title:
f.write('<tspan x="3" y="1.1em">')
f.write(punescape(title, tile.attrs))
f.write('</tspan>')
if not no_header:
if not no_stats:
f.write('<tspan x="%(x)d" y="1.1em" '
'text-anchor="end">' % dict(
x=tile.width-3))
@@ -807,7 +808,7 @@ def main(csv_paths, output, *,
stat['mean'], stat['stddev'],
stat['min'], stat['max']))
f.write('</tspan>')
else:
elif not no_stats:
f.write('<tspan x="3" y="1.1em">')
f.write('total %d, avg %d +-%dσ, min %d, max %d' % (
stat['total'],
@@ -955,6 +956,10 @@ if __name__ == "__main__":
'-N', '--no-header',
action='store_true',
help="Don't show the header.")
parser.add_argument(
'--no-stats',
action='store_true',
help="Don't show data stats in the header.")
parser.add_argument(
'--binary',
action='store_true',