1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", { value: true });
|
4 | const tslib_1 = require("tslib");
|
5 | const chalk_1 = tslib_1.__importDefault(require("chalk"));
|
6 | const supportsColor = tslib_1.__importStar(require("supports-color"));
|
7 | const deps_1 = tslib_1.__importDefault(require("../deps"));
|
8 | const base_1 = require("./base");
|
9 | const spinners = require('./spinners');
|
10 | function color(s) {
|
11 | if (!supportsColor)
|
12 | return s;
|
13 | let has256 = supportsColor.stdout.has256 || (process.env.TERM || '').indexOf('256') !== -1;
|
14 | return has256 ? `\u001b[38;5;104m${s}${deps_1.default.ansiStyles.reset.open}` : chalk_1.default.magenta(s);
|
15 | }
|
16 | class SpinnerAction extends base_1.ActionBase {
|
17 | constructor() {
|
18 | super();
|
19 | this.type = 'spinner';
|
20 | this.frames = spinners[process.platform === 'win32' ? 'line' : 'dots2'].frames;
|
21 | this.frameIndex = 0;
|
22 | }
|
23 | _start() {
|
24 | this._reset();
|
25 | if (this.spinner)
|
26 | clearInterval(this.spinner);
|
27 | this._render();
|
28 | let interval = (this.spinner = setInterval(icon => this._render.bind(this)(icon), process.platform === 'win32' ? 500 : 100, 'spinner'));
|
29 | interval.unref();
|
30 | }
|
31 | _stop(status) {
|
32 | if (this.task)
|
33 | this.task.status = status;
|
34 | if (this.spinner)
|
35 | clearInterval(this.spinner);
|
36 | this._render();
|
37 | this.output = undefined;
|
38 | }
|
39 | _pause(icon) {
|
40 | if (this.spinner)
|
41 | clearInterval(this.spinner);
|
42 | this._reset();
|
43 | if (icon)
|
44 | this._render(` ${icon}`);
|
45 | this.output = undefined;
|
46 | }
|
47 | _frame() {
|
48 | let frame = this.frames[this.frameIndex];
|
49 | this.frameIndex = ++this.frameIndex % this.frames.length;
|
50 | return color(frame);
|
51 | }
|
52 | _render(icon) {
|
53 | const task = this.task;
|
54 | if (!task)
|
55 | return;
|
56 | this._reset();
|
57 | this._flushStdout();
|
58 | let frame = icon === 'spinner' ? ` ${this._frame()}` : icon || '';
|
59 | let status = task.status ? ` ${task.status}` : '';
|
60 | this.output = `${task.action}...${frame}${status}\n`;
|
61 | this._write(this.std, this.output);
|
62 | }
|
63 | _reset() {
|
64 | if (!this.output)
|
65 | return;
|
66 | let lines = this._lines(this.output);
|
67 | this._write(this.std, deps_1.default.ansiEscapes.cursorLeft + deps_1.default.ansiEscapes.cursorUp(lines) + deps_1.default.ansiEscapes.eraseDown);
|
68 | this.output = undefined;
|
69 | }
|
70 | _lines(s) {
|
71 | return deps_1.default
|
72 | .stripAnsi(s)
|
73 | .split('\n')
|
74 | .map(l => Math.ceil(l.length / deps_1.default.screen.errtermwidth))
|
75 | .reduce((c, i) => c + i, 0);
|
76 | }
|
77 | }
|
78 | exports.default = SpinnerAction;
|