add progress estimation based on several samples

This commit is contained in:
Matthieu Jolimaitre 2025-08-01 11:10:10 +02:00
parent 669b9ac56a
commit f58632c025
3 changed files with 24 additions and 8 deletions

View file

@ -5,7 +5,7 @@ import { Command } from "https://deno.land/x/cliffy@v1.0.0-rc.4/command/mod.ts"
import { Channel, Constructible, InstanceOf } from "https://git.barnulf.net/mb/barnulf_ts/raw/branch/master/mod.ts"
import { wait } from "https://git.barnulf.net/mb/barnulf_ts/raw/branch/master/src/lib/utils.ts"
const version = "1.1.1"
const version = "1.1.2"
async function main() {
const { do_clear, command, extensions, files, strategy, go_up, show_progress } = await parse_args()
@ -118,7 +118,7 @@ class Runner {
return (status: Deno.CommandStatus) => {
const end_ms = Date.now()
const time_ms = end_ms - start_ms
if (status.success) this.time_estimation_ms = time_ms
if (status.success) this.estimator.add(time_ms)
const secs = time_ms / 1000
if (this.go_up) this.restore_cursor()
if (stop !== undefined) stop.stop = true
@ -135,14 +135,14 @@ class Runner {
console.log("\x1b[u")
}
time_estimation_ms = 0
estimator = new Estimator(Math.max)
private start_progress() {
const stop = { stop: false }
;(async () => {
for await (const step of progress(this.time_estimation_ms)) {
for await (const step of progress(this.estimator.estimate())) {
if (stop.stop) break
const secs = (Math.floor(step.ms / 100) / 10).toString().padStart(3)
const total = Math.floor(this.time_estimation_ms / 1_000).toString().padStart(3)
const total = Math.floor(this.estimator.estimate() / 1_000).toString().padStart(3)
const perc = (Math.floor(step.relative * 1_000) / 10).toString().padStart(5)
console.log("")
log("progress", `${secs} s / ${total} s (${perc} %)`)
@ -234,6 +234,22 @@ function catch_<T, R, Cs extends Catching<any, R>[]>(op: () => T, ...catchings:
}
}
class Estimator {
private readonly measures = Array<number>()
public constructor(
private readonly estimation: (...measures: Array<number>) => number,
) {}
public add(measure: number) {
this.measures.push(measure)
}
public estimate() {
return this.estimation(...this.measures)
}
}
function log(verb: string, value: unknown) {
const yellow = "\x1b[0;33m"
const bold = "\x1b[1m"