|
1 | 1 | import * as colors from "@std/fmt/colors"; |
2 | 2 | import { |
3 | 3 | type ConsoleSize, |
| 4 | + type DeferredItem, |
4 | 5 | maybeConsoleSize, |
5 | 6 | renderTextItems, |
6 | 7 | staticText, |
7 | 8 | type TextItem, |
8 | 9 | } from "@david/console-static-text"; |
9 | 10 | import { isOutputTty } from "../utils.ts"; |
10 | 11 | import { humanDownloadSize } from "./format.ts"; |
11 | | -import { addProgressBar, removeProgressBar, type RenderIntervalProgressBar } from "./container.ts"; |
| 12 | +import { logger, LoggerRefreshItemKind } from "../logger.ts"; |
12 | 13 |
|
13 | | -export { isShowingProgressBars } from "./container.ts"; |
| 14 | +const progressBars: RenderIntervalProgressBar[] = []; |
14 | 15 |
|
15 | 16 | /** Options for showing progress. */ |
16 | 17 | export interface ProgressOptions { |
@@ -250,3 +251,42 @@ export function renderProgressBar(state: RenderState, size: ConsoleSize | undefi |
250 | 251 | return result; |
251 | 252 | } |
252 | 253 | } |
| 254 | + |
| 255 | +export interface RenderIntervalProgressBar { |
| 256 | + render(size: ConsoleSize | undefined): TextItem[]; |
| 257 | +} |
| 258 | + |
| 259 | +function addProgressBar(render: (size: ConsoleSize) => TextItem[]): RenderIntervalProgressBar { |
| 260 | + const pb = { |
| 261 | + render, |
| 262 | + }; |
| 263 | + progressBars.push(pb); |
| 264 | + refresh(); |
| 265 | + return pb; |
| 266 | +} |
| 267 | + |
| 268 | +function removeProgressBar(pb: RenderIntervalProgressBar) { |
| 269 | + const index = progressBars.indexOf(pb); |
| 270 | + if (index === -1) { |
| 271 | + return false; |
| 272 | + } |
| 273 | + progressBars.splice(index, 1); |
| 274 | + refresh(); |
| 275 | + return true; |
| 276 | +} |
| 277 | + |
| 278 | +function refresh() { |
| 279 | + logger.setItems( |
| 280 | + LoggerRefreshItemKind.ProgressBars, |
| 281 | + progressBars.map((p) => { |
| 282 | + const item: DeferredItem = (consoleSize) => { |
| 283 | + return p.render(consoleSize); |
| 284 | + }; |
| 285 | + return item; |
| 286 | + }), |
| 287 | + ); |
| 288 | +} |
| 289 | + |
| 290 | +export function isShowingProgressBars() { |
| 291 | + return isOutputTty && progressBars.length > 0; |
| 292 | +} |
0 commit comments