| ... | @@ -1,10 +1,15 @@ | ... | @@ -1,10 +1,15 @@ |
| 1 | // This file is under heavy construction. | 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 | // A progress tree based on my past uses with clover console v3 and the Zig | 8 | // A progress tree based on my past uses with clover console v3 and the Zig |
| 4 | // Progress API. Pass `Progress` to functions which should track progress, | 9 | // Progress API. Pass `Progress` to functions which should track progress, |
| 5 | // calling `.start()` to create child items. Or, multiple top-level progress | 10 | // calling `.start()` to create child items. Or, multiple top-level progress |
| 6 | // bars can be created with the singleton's `start()` function. | 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 | self satisfies Ref; // prove you can use the namespace import as a `Progress` | 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,12 +17,7 @@ self satisfies Ref; // prove you can use the namespace import as a `Progress` |
| 12 | * when given an estimate, a progress bar is rendered. | 17 | * when given an estimate, a progress bar is rendered. |
| 13 | */ | 18 | */ |
| 14 | export function start(text: string, opts?: StartOptions): Node { | 19 | export function start(text: string, opts?: StartOptions): Node { |
| 15 | const [state, node] = newNode(text, opts); | 20 | return global.start(text, opts); |
| 16 | globalState.push(state); | | |
| 17 | state.signal = topLevelSignaler; | | |
| 18 | if (rerender) rerender(); | | |
| 19 | else log.startWidget(globalWidget); | | |
| 20 | return node; | | |
| 21 | } | 21 | } |
| 22 | | 22 | |
| 23 | export interface Ref { | 23 | export interface Ref { |
| ... | @@ -58,10 +58,6 @@ export interface Node extends Ref { | ... | @@ -58,10 +58,6 @@ export interface Node extends Ref { |
| 58 | | 58 | |
| 59 | /** end this Node or RootNode. ending collapses children */ | 59 | /** end this Node or RootNode. ending collapses children */ |
| 60 | end(): void; | 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 | [Symbol.dispose](): void; | 62 | [Symbol.dispose](): void; |
| 67 | } | 63 | } |
| ... | @@ -70,30 +66,18 @@ export interface StartOptions { | ... | @@ -70,30 +66,18 @@ export interface StartOptions { |
| 70 | estimate?: number | undefined | null; | 66 | estimate?: number | undefined | null; |
| 71 | } | 67 | } |
| 72 | | 68 | |
| 73 | /** a node that satisfies the api */ | 69 | /** a valid `Node` without any output */ |
| 74 | const nullNode: Node = | 70 | const nullNode: Node = /* @__PURE__ */ |
| 75 | (function start(text: string, { estimate }: StartOptions = {}): Node { | 71 | (function start(text: string, opts?: StartOptions): Node { |
| 76 | return { | 72 | const node = newNode(text, opts)[1]; |
| 77 | start, | 73 | node.start = start; |
| 78 | text, | 74 | node.log = log.scoped(""); |
| 79 | value: 0, | 75 | return node; |
| 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 | }; | | |
| 92 | })("root"); | 76 | })("root"); |
| 93 | | 77 | |
| 94 | export { nullNode as null }; | 78 | export { nullNode as null }; |
| 95 | | 79 | |
| 96 | type SortNode = | 80 | export type SortNode = |
| 97 | & Pick< | 81 | & Pick< |
| 98 | Node, | 82 | Node, |
| 99 | | "text" | 83 | | "text" |
| ... | @@ -105,16 +89,23 @@ type SortNode = | ... | @@ -105,16 +89,23 @@ type SortNode = |
| 105 | > | 89 | > |
| 106 | & { children: SortNode[] }; | 90 | & { children: SortNode[] }; |
| 107 | | 91 | |
| 108 | type Signal = "draw" | "end" | "endFail" | "pendingFail"; | 92 | export interface Internal extends SortNode { |
| 109 | interface State extends SortNode { | | |
| 110 | logs: string[]; | 93 | logs: string[]; |
| 111 | children: State[]; | 94 | children: Internal[]; |
| 112 | parent: State | null; | 95 | parent: Internal | null; |
| 113 | signal(signal: Signal): void; | 96 | signal(msg: Signal): void; |
| 114 | } | 97 | } |
| 115 | | 98 | |
| 116 | function newNode(text: string, { estimate }: StartOptions = {}): [State, Node] { | 99 | type Signal = |
| 117 | const state: State = { | 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 | text, | 109 | text, |
| 119 | value: 0, | 110 | value: 0, |
| 120 | estimate: estimate ?? 0, | 111 | estimate: estimate ?? 0, |
| ... | @@ -128,13 +119,20 @@ function newNode(text: string, { estimate }: StartOptions = {}): [State, Node] { | ... | @@ -128,13 +119,20 @@ function newNode(text: string, { estimate }: StartOptions = {}): [State, Node] { |
| 128 | throw new Error("no signaler"); | 119 | throw new Error("no signaler"); |
| 129 | }, | 120 | }, |
| 130 | }; | 121 | }; |
| | 122 | let detached = false; |
| 131 | function end() { | 123 | function end() { |
| | 124 | if (detached) return; |
| 132 | if (!state.parent) return; | 125 | if (!state.parent) return; |
| 133 | const i = state.parent.children.indexOf(state); | 126 | const i = state.parent.children.indexOf(state); |
| 134 | if (i === -1) return; | 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 | state.parent.children.splice(i, 1); | 133 | state.parent.children.splice(i, 1); |
| 137 | state.parent = null; | 134 | state.parent = null; |
| | 135 | detached = true; |
| 138 | } | 136 | } |
| 139 | function mutate() { | 137 | function mutate() { |
| 140 | const { parent } = state; | 138 | const { parent } = state; |
| ... | @@ -144,6 +142,7 @@ function newNode(text: string, { estimate }: StartOptions = {}): [State, Node] { | ... | @@ -144,6 +142,7 @@ function newNode(text: string, { estimate }: StartOptions = {}): [State, Node] { |
| 144 | const scope = headless.logger({ | 142 | const scope = headless.logger({ |
| 145 | log: { | 143 | log: { |
| 146 | writeLine(line) { | 144 | writeLine(line) { |
| | 145 | if (detached) return state.signal({ type: "line", line }); |
| 147 | state.logs.push(...line.split("\n")); | 146 | state.logs.push(...line.split("\n")); |
| 148 | state.signal("draw"); | 147 | state.signal("draw"); |
| 149 | }, | 148 | }, |
| ... | @@ -226,6 +225,27 @@ function newNode(text: string, { estimate }: StartOptions = {}): [State, Node] { | ... | @@ -226,6 +225,27 @@ function newNode(text: string, { estimate }: StartOptions = {}): [State, Node] { |
| 226 | return [state, binding]; | 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 | const spinnerFps = 12.5; | 249 | const spinnerFps = 12.5; |
| 230 | | 250 | |
| 231 | const box = { | 251 | const box = { |
| ... | @@ -238,31 +258,7 @@ const fullBar = "█"; | ... | @@ -238,31 +258,7 @@ const fullBar = "█"; |
| 238 | const spinner = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"] | 258 | const spinner = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"] |
| 239 | .map((frame) => ansi.style(ansi.fgBlue, frame)); | 259 | .map((frame) => ansi.style(ansi.fgBlue, frame)); |
| 240 | | 260 | |
| 241 | let rerender: (() => void) | null = null; | 261 | function renderMainLine(state: Internal, now: number, depth: number) { |
| 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) { | | |
| 266 | if (state.estimate && state.showEstimate) { | 262 | if (state.estimate && state.showEstimate) { |
| 267 | return ansi.style( | 263 | return ansi.style( |
| 268 | ansi.bgBrightBlack + ansi.fgBlue, | 264 | ansi.bgBrightBlack + ansi.fgBlue, |
| ... | @@ -278,14 +274,14 @@ function renderMainLine(state: State, now: number, depth: number) { | ... | @@ -278,14 +274,14 @@ function renderMainLine(state: State, now: number, depth: number) { |
| 278 | (state.value > 0 ? `[${state.value}] ` : "") + state.text; | 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 | let maxHeight = 25; // TODO: | 278 | let maxHeight = 25; // TODO: |
| 283 | let truncated = 0; | 279 | let truncated = 0; |
| 284 | let out = ""; | 280 | let out = ""; |
| 285 | const { children } = state; | 281 | const { children } = state; |
| 286 | if (children.length === 0) return ""; | 282 | if (children.length === 0) return ""; |
| 287 | for (let i = 0, { length } = children; i < length; i += 1) { | 283 | for (let i = 0, { length } = children; i < length; i += 1) { |
| 288 | const child = children[i]; | 284 | const child = UNWRAP(children[i]); |
| 289 | if (child.passive && child.children.length === 0) continue; | 285 | if (child.passive && child.children.length === 0) continue; |
| 290 | let item = ""; | 286 | let item = ""; |
| 291 | const left = depth.map((x) => x ? box.line : " ").join(""); | 287 | const left = depth.map((x) => x ? box.line : " ").join(""); |
| ... | @@ -333,14 +329,55 @@ export function getUnicodeBar(progress: number, width: number) { | ... | @@ -333,14 +329,55 @@ export function getUnicodeBar(progress: number, width: number) { |
| 333 | return fill + partChar + empty; | 329 | return fill + partChar + empty; |
| 334 | } | 330 | } |
| 335 | | 331 | |
| 336 | function topLevelSignaler(this: State, signal: Signal) { | 332 | /** Run a progress tree without a dependency on a TTY */ |
| 337 | if (signal === "draw") { | 333 | export class Headless { |
| 338 | rerender?.(); | 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"; | 376 | const global = /** @__PURE__ */ new Headless(log); |
| 343 | import * as log from "../log.ts"; | 377 | |
| 344 | import * as headless from "../log/headless.ts"; | 378 | import * as process from "node:process"; |
| 345 | import * as ansi from "./ansi.ts"; | 379 | import * as log from "lib/log.ts"; |
| 346 | import * as string from "../string.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"; |