| ... | ... | @@ -1,10 +1,15 @@ |
| 1 | 1 | // This file is under heavy construction. |
| 2 | // - I am happy with the overall design |
| 3 | // - Will rename things |
| 4 | // - Will be re-working the entire module to have a headless |
| 5 | // core, but still leaving the top level `start` function. |
| 6 | // - There are an overwhelming amount of bugs |
| 2 | 7 | // |
| 3 | 8 | // A progress tree based on my past uses with clover console v3 and the Zig |
| 4 | 9 | // Progress API. Pass `Progress` to functions which should track progress, |
| 5 | 10 | // calling `.start()` to create child items. Or, multiple top-level progress |
| 6 | 11 | // bars can be created with the singleton's `start()` function. |
| 7 | | import * as self from "./progress.ts"; |
| 12 | import * as self from "lib/log/progress.ts"; |
| 8 | 13 | self satisfies Ref; // prove you can use the namespace import as a `Progress` |
| 9 | 14 | |
| 10 | 15 | /** |
| ... | ... | @@ -12,12 +17,7 @@ self satisfies Ref; // prove you can use the namespace import as a `Progress` |
| 12 | 17 | * when given an estimate, a progress bar is rendered. |
| 13 | 18 | */ |
| 14 | 19 | export function start(text: string, opts?: StartOptions): Node { |
| 15 | | const [state, node] = newNode(text, opts); |
| 16 | | globalState.push(state); |
| 17 | | state.signal = topLevelSignaler; |
| 18 | | if (rerender) rerender(); |
| 19 | | else log.startWidget(globalWidget); |
| 20 | | return node; |
| 20 | return global.start(text, opts); |
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | export interface Ref { |
| ... | ... | @@ -58,10 +58,6 @@ export interface Node extends Ref { |
| 58 | 58 | |
| 59 | 59 | /** end this Node or RootNode. ending collapses children */ |
| 60 | 60 | end(): void; |
| 61 | | // /** signal that there will be a failure without ending the node */ |
| 62 | | // pendingFail(): void; |
| 63 | | // /** end this node with a failure */ |
| 64 | | // endFail(): void; |
| 65 | 61 | |
| 66 | 62 | [Symbol.dispose](): void; |
| 67 | 63 | } |
| ... | ... | @@ -70,30 +66,18 @@ export interface StartOptions { |
| 70 | 66 | estimate?: number | undefined | null; |
| 71 | 67 | } |
| 72 | 68 | |
| 73 | | /** a node that satisfies the api */ |
| 74 | | const nullNode: Node = |
| 75 | | (function start(text: string, { estimate }: StartOptions = {}): Node { |
| 76 | | return { |
| 77 | | start, |
| 78 | | text, |
| 79 | | value: 0, |
| 80 | | estimate: estimate ?? 0, |
| 81 | | showEstimate: true, |
| 82 | | passive: false, |
| 83 | | maxHeight: null, |
| 84 | | sortChildren: null, |
| 85 | | inc() { |
| 86 | | this.value += 1; |
| 87 | | }, |
| 88 | | log: log.scoped(""), |
| 89 | | end() {}, |
| 90 | | [Symbol.dispose]() {}, |
| 91 | | }; |
| 69 | /** a valid `Node` without any output */ |
| 70 | const nullNode: Node = /* @__PURE__ */ |
| 71 | (function start(text: string, opts?: StartOptions): Node { |
| 72 | const node = newNode(text, opts)[1]; |
| 73 | node.start = start; |
| 74 | node.log = log.scoped(""); |
| 75 | return node; |
| 92 | 76 | })("root"); |
| 93 | 77 | |
| 94 | 78 | export { nullNode as null }; |
| 95 | 79 | |
| 96 | | type SortNode = |
| 80 | export type SortNode = |
| 97 | 81 | & Pick< |
| 98 | 82 | Node, |
| 99 | 83 | | "text" |
| ... | ... | @@ -105,16 +89,23 @@ type SortNode = |
| 105 | 89 | > |
| 106 | 90 | & { children: SortNode[] }; |
| 107 | 91 | |
| 108 | | type Signal = "draw" | "end" | "endFail" | "pendingFail"; |
| 109 | | interface State extends SortNode { |
| 92 | export interface Internal extends SortNode { |
| 110 | 93 | logs: string[]; |
| 111 | | children: State[]; |
| 112 | | parent: State | null; |
| 113 | | signal(signal: Signal): void; |
| 94 | children: Internal[]; |
| 95 | parent: Internal | null; |
| 96 | signal(msg: Signal): void; |
| 114 | 97 | } |
| 115 | 98 | |
| 116 | | function newNode(text: string, { estimate }: StartOptions = {}): [State, Node] { |
| 117 | | const state: State = { |
| 99 | type Signal = |
| 100 | | "draw" |
| 101 | | { type: "end"; title: string; logs: string[] } |
| 102 | | { type: "line"; line: string }; |
| 103 | |
| 104 | function newNode( |
| 105 | text: string, |
| 106 | { estimate }: StartOptions = {}, |
| 107 | ): [Internal, Node] { |
| 108 | const state: Internal = { |
| 118 | 109 | text, |
| 119 | 110 | value: 0, |
| 120 | 111 | estimate: estimate ?? 0, |
| ... | ... | @@ -128,13 +119,20 @@ function newNode(text: string, { estimate }: StartOptions = {}): [State, Node] { |
| 128 | 119 | throw new Error("no signaler"); |
| 129 | 120 | }, |
| 130 | 121 | }; |
| 122 | let detached = false; |
| 131 | 123 | function end() { |
| 124 | if (detached) return; |
| 132 | 125 | if (!state.parent) return; |
| 133 | 126 | const i = state.parent.children.indexOf(state); |
| 134 | 127 | if (i === -1) return; |
| 135 | | state.signal("draw"); |
| 128 | let title = state.text; |
| 129 | let p: Internal | null = state; |
| 130 | while (p = p.parent) title = p.text + " / " + title; |
| 131 | state.signal({ type: "end", title, logs: state.logs }); |
| 132 | state.logs = []; |
| 136 | 133 | state.parent.children.splice(i, 1); |
| 137 | 134 | state.parent = null; |
| 135 | detached = true; |
| 138 | 136 | } |
| 139 | 137 | function mutate() { |
| 140 | 138 | const { parent } = state; |
| ... | ... | @@ -144,6 +142,7 @@ function newNode(text: string, { estimate }: StartOptions = {}): [State, Node] { |
| 144 | 142 | const scope = headless.logger({ |
| 145 | 143 | log: { |
| 146 | 144 | writeLine(line) { |
| 145 | if (detached) return state.signal({ type: "line", line }); |
| 147 | 146 | state.logs.push(...line.split("\n")); |
| 148 | 147 | state.signal("draw"); |
| 149 | 148 | }, |
| ... | ... | @@ -226,6 +225,27 @@ function newNode(text: string, { estimate }: StartOptions = {}): [State, Node] { |
| 226 | 225 | return [state, binding]; |
| 227 | 226 | } |
| 228 | 227 | |
| 228 | function format(now: number, list: Internal[]) { |
| 229 | let out = ""; |
| 230 | for (const top of list) { |
| 231 | if (top.passive && top.children.length === 0) continue; |
| 232 | out += renderMainLine(top, now, 0) + "\n"; |
| 233 | out += renderChildren(top, now, []); |
| 234 | } |
| 235 | return out.trimEnd(); |
| 236 | } |
| 237 | |
| 238 | /** These functions are used to test `lib/log/progress` */ |
| 239 | export const internals = { |
| 240 | node(text: string, props: Partial<Internal>, children: Internal[]): Internal { |
| 241 | const [node] = newNode(text); |
| 242 | Object.assign(node, props); |
| 243 | node.children.push(...children); |
| 244 | return node; |
| 245 | }, |
| 246 | format, |
| 247 | }; |
| 248 | |
| 229 | 249 | const spinnerFps = 12.5; |
| 230 | 250 | |
| 231 | 251 | const box = { |
| ... | ... | @@ -238,31 +258,7 @@ const fullBar = "█"; |
| 238 | 258 | const spinner = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"] |
| 239 | 259 | .map((frame) => ansi.style(ansi.fgBlue, frame)); |
| 240 | 260 | |
| 241 | | let rerender: (() => void) | null = null; |
| 242 | | let globalTimer: ts.Interval | null = null; |
| 243 | | const globalWidget: log.Widget = { |
| 244 | | format(now) { |
| 245 | | let out = ""; |
| 246 | | for (const top of globalState) { |
| 247 | | if (top.passive && top.children.length === 0) continue; |
| 248 | | out += renderMainLine(top, now, 0) + "\n"; |
| 249 | | out += renderChildren(top, now, []); |
| 250 | | } |
| 251 | | return out.trimEnd(); |
| 252 | | }, |
| 253 | | onChange(cb) { |
| 254 | | //const globalTimer = setInterval(cb, 1000 / spinnerFps); |
| 255 | | //if ("unref" in globalTimer) (globalTimer.unref as () => void)(); |
| 256 | | rerender = cb; |
| 257 | | return () => { |
| 258 | | globalTimer != null && clearInterval(globalTimer); |
| 259 | | rerender = null; |
| 260 | | }; |
| 261 | | }, |
| 262 | | }; |
| 263 | | const globalState: State[] = []; |
| 264 | | |
| 265 | | function renderMainLine(state: State, now: number, depth: number) { |
| 261 | function renderMainLine(state: Internal, now: number, depth: number) { |
| 266 | 262 | if (state.estimate && state.showEstimate) { |
| 267 | 263 | return ansi.style( |
| 268 | 264 | ansi.bgBrightBlack + ansi.fgBlue, |
| ... | ... | @@ -278,14 +274,14 @@ function renderMainLine(state: State, now: number, depth: number) { |
| 278 | 274 | (state.value > 0 ? `[${state.value}] ` : "") + state.text; |
| 279 | 275 | } |
| 280 | 276 | |
| 281 | | function renderChildren(state: State, now: number, depth: boolean[]) { |
| 277 | function renderChildren(state: Internal, now: number, depth: boolean[]) { |
| 282 | 278 | let maxHeight = 25; // TODO: |
| 283 | 279 | let truncated = 0; |
| 284 | 280 | let out = ""; |
| 285 | 281 | const { children } = state; |
| 286 | 282 | if (children.length === 0) return ""; |
| 287 | 283 | for (let i = 0, { length } = children; i < length; i += 1) { |
| 288 | | const child = children[i]; |
| 284 | const child = UNWRAP(children[i]); |
| 289 | 285 | if (child.passive && child.children.length === 0) continue; |
| 290 | 286 | let item = ""; |
| 291 | 287 | const left = depth.map((x) => x ? box.line : " ").join(""); |
| ... | ... | @@ -333,14 +329,55 @@ export function getUnicodeBar(progress: number, width: number) { |
| 333 | 329 | return fill + partChar + empty; |
| 334 | 330 | } |
| 335 | 331 | |
| 336 | | function topLevelSignaler(this: State, signal: Signal) { |
| 337 | | if (signal === "draw") { |
| 338 | | rerender?.(); |
| 332 | /** Run a progress tree without a dependency on a TTY */ |
| 333 | export class Headless { |
| 334 | constructor( |
| 335 | /** |
| 336 | * `startWidget` is only ever called from `start` |
| 337 | * `writeLine` is used for logs from completed nodes |
| 338 | */ |
| 339 | public env: Pick<headless.WidgetHost, "writeLine" | "startWidget">, |
| 340 | ) {} |
| 341 | |
| 342 | active: Internal[] = []; |
| 343 | |
| 344 | start(text: string, opts?: StartOptions): Node { |
| 345 | const [state, node] = newNode(text, opts); |
| 346 | this.active.push(state); |
| 347 | state.signal = this.#signal; |
| 348 | const rerender = this.#rerender; |
| 349 | if (rerender) rerender(); |
| 350 | else log.startWidget(this.widget); |
| 351 | return node; |
| 339 | 352 | } |
| 353 | |
| 354 | widget: log.Widget = { |
| 355 | format: (now) => format(now, this.active), |
| 356 | onChange: ( |
| 357 | rerender, |
| 358 | ) => (this.#rerender = rerender, () => this.#rerender = null), |
| 359 | }; |
| 360 | |
| 361 | #rerender?: (() => void) | null; |
| 362 | #signal = (msg: Signal) => { |
| 363 | if (msg === "draw") this.#rerender?.(); |
| 364 | else if (msg.type === "line") this.env.writeLine(msg.line); |
| 365 | else if (msg.type === "end") { |
| 366 | if (msg.logs.length === 0) return; |
| 367 | const { logs } = msg; |
| 368 | const count = `${logs.length} line${logs.length === 1 ? "" : "s"}`; |
| 369 | const header = `[${count} from ${msg.title}]`; |
| 370 | this.env.writeLine(ansi.style(ansi.fgBrightBlack, header)); |
| 371 | this.env.writeLine(msg.logs.join("\n")); |
| 372 | } |
| 373 | }; |
| 340 | 374 | } |
| 341 | 375 | |
| 342 | | import * as ts from "../ts.ts"; |
| 343 | | import * as log from "../log.ts"; |
| 344 | | import * as headless from "../log/headless.ts"; |
| 345 | | import * as ansi from "./ansi.ts"; |
| 346 | | import * as string from "../string.ts"; |
| 376 | const global = /** @__PURE__ */ new Headless(log); |
| 377 | |
| 378 | import * as process from "node:process"; |
| 379 | import * as log from "lib/log.ts"; |
| 380 | import * as headless from "lib/log/headless.ts"; |
| 381 | import * as ansi from "lib/log/ansi.ts"; |
| 382 | import * as string from "lib/string.ts"; |
| 383 | import { UNWRAP } from "lib/assert.ts"; |