diff --git a/bin/reading_cost.py b/bin/reading_cost.py index 552c5834..9725d6a5 100644 --- a/bin/reading_cost.py +++ b/bin/reading_cost.py @@ -17,9 +17,8 @@ order the chunks finished. figures of chunks that finished after it. It then compares the mean tokens of the history's last 5 figures with the 5 before them, in the -order their chunks finished, and prints -"Rising:" or "Not rising:" with both means. Rising, it lists the files the last 5 chunks read most. -With fewer than 10 figures, it says so. +order their chunks finished, and prints "Rising:" or "Not rising:" with both means. Rising, it +lists the files the last 5 chunks read most. With fewer than 10 figures, it says so. Exit 1, saying why, when git cannot start or finds no checkout, the archive or the history cannot be read or written, or a figure in the history cannot be read back. @@ -70,31 +69,39 @@ class Checkout: def main(argv: list[str]) -> int: reset = argv[:1] == ["--reset"] given = argv[1:] if reset else argv - helped = given[:1] in (["-h"], ["--help"]) - if helped or len(given) > 1: + if given[:1] in (["-h"], ["--help"]) or len(given) > 1: + helped = len(given) == 1 print(__doc__.strip(), file=sys.stdout if helped else sys.stderr) return 0 if helped else 1 - home = pathlib.Path.home() checkout = located() if isinstance(checkout, Failure): return failed(checkout) history = pathlib.Path(given[0]) if given else pathlib.Path(checkout.top, "docs", "comprehension-history.md") + updated = update(history, checkout, reset) + if isinstance(updated, Failure): + return failed(updated) + appended, readings = updated + print(f"Appended {appended} reading-cost figures{' and a reset' if reset else ''} to {history}.") + print(trend(readings)) + return 0 + + +def update(history: pathlib.Path, checkout: Checkout, reset: bool) -> tuple[int, list[Reading]] | Failure: + """How many figures it appended, and the readings the trend counts.""" text = read_history(history) if isinstance(text, Failure): - return failed(text) - figures = new_figures(text, checkout, home) + return text + figures = new_figures(text, checkout, pathlib.Path.home()) if isinstance(figures, Failure): - return failed(figures) + return figures marks = [*figures, f"## {RESET}{utc(time.time())}\n"] if reset else figures text = merged(text, marks) if marks else text readings = readings_of(text) if isinstance(readings, Failure): - return failed(readings) + return readings if marks and (unwritten := write_history(history, text)): - return failed(unwritten) - print(f"Appended {len(figures)} reading-cost figures{' and a reset' if reset else ''} to {history}.") - print(trend(readings)) - return 0 + return unwritten + return len(figures), readings def new_figures(history: str, checkout: Checkout, home: pathlib.Path) -> list[str] | Failure: