progress.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /**
  2. * Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { Task } from '../task'
  7. interface Progress {
  8. root: Progress.Node,
  9. canAbort: boolean,
  10. requestAbort: (reason?: string) => void
  11. }
  12. namespace Progress {
  13. export interface Node {
  14. readonly progress: Task.Progress,
  15. readonly children: ReadonlyArray<Node>
  16. }
  17. export interface Observer { (progress: Progress): void }
  18. function _format(root: Progress.Node, prefix = ''): string {
  19. const p = root.progress;
  20. if (!root.children.length) {
  21. if (p.isIndeterminate) return `${prefix}${p.taskName}: ${p.message}`;
  22. return `${prefix}${p.taskName}: [${p.current}/${p.max}] ${p.message}`;
  23. }
  24. const newPrefix = prefix + ' |_ ';
  25. const subTree = root.children.map(c => _format(c, newPrefix));
  26. if (p.isIndeterminate) return `${prefix}${p.taskName}: ${p.message}\n${subTree.join('\n')}`;
  27. return `${prefix}${p.taskName}: [${p.current}/${p.max}] ${p.message}\n${subTree.join('\n')}`;
  28. }
  29. export function format(p: Progress) { return _format(p.root); }
  30. }
  31. export { Progress }