kui-shell
Version:
This is the monorepo for Kui, the hybrid command-line/GUI electron-based Kubernetes tool
141 lines • 5.25 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const ora = require("ora");
const stream_1 = require("stream");
const capabilities_1 = require("@kui-shell/core/api/capabilities");
const defaultColor = 'processing-text';
class OraStream extends stream_1.Writable {
constructor() {
super({ highWaterMark: 0 });
this.killLine = true;
this.color = defaultColor;
}
init(text, { createOutputStream }) {
return __awaiter(this, void 0, void 0, function* () {
this.stdout = yield createOutputStream();
this.spinner = ora({
text,
isEnabled: true,
spinner: 'growHorizontal',
stream: this
}).start();
return this;
});
}
set text(str) {
this.spinner.text = str;
}
next(str, successStr) {
return __awaiter(this, void 0, void 0, function* () {
yield this.succeed(successStr);
yield this.start(str);
});
}
clear() {
return new Promise(resolve => {
this.cb = resolve;
this.spinner.clear();
});
}
start(str) {
return new Promise(resolve => {
this.killLine = false;
this.text = str;
this.cb = resolve;
this.spinner.start();
}).then(() => {
this.killLine = true;
});
}
stop(withBlank = false) {
return __awaiter(this, void 0, void 0, function* () {
this.spinner.clear();
if (withBlank) {
yield this.next('');
this.spinner.stopAndPersist({ symbol: '' });
}
else {
yield this.succeed();
}
if (capabilities_1.isHeadless()) {
yield this.stdout('\n');
}
});
}
blank() {
return this.next('');
}
fail(message) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise(resolve => {
this.cb = resolve;
this.color = 'red-text';
this.spinner.fail(message);
}).then(() => {
this.color = defaultColor;
});
});
}
succeed(message) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise(resolve => {
this.cb = resolve;
this.color = 'green-text';
this.spinner.succeed(message);
}).then(() => {
this.color = defaultColor;
});
});
}
_write(chunk, enc, next) {
return __awaiter(this, void 0, void 0, function* () {
if (capabilities_1.isHeadless()) {
const str = chunk.toString();
yield this.stdout(str.substring(0, str.indexOf('\n')), this.killLine);
}
else {
const str = chunk.toString();
const splitIdx = str[0] === '\u001b' ? str.indexOf('m', 1) + 2 : 1;
const spinnerPart = str[splitIdx - 1];
const restPart = str[splitIdx] === '\u001b' ? str.slice(splitIdx + 5) : str.slice(splitIdx);
const line = document.createElement('pre');
const spinnerDom = document.createElement('div');
const restDom = document.createElement('div');
line.appendChild(spinnerDom);
line.appendChild(restDom);
line.classList.add('flex-layout');
restDom.classList.add('do-not-overflow');
spinnerDom.classList.add(this.color);
spinnerDom.innerText = spinnerPart;
if (!/✔/.test(spinnerPart) && !/'✖'/.test(spinnerPart)) {
restDom.classList.add('lighter-text');
line.classList.add('flex-align-top');
}
else {
spinnerDom.style.fontSize = '125%';
spinnerDom.style.lineHeight = '1.2rem';
spinnerDom.style.width = '1rem';
spinnerDom.style.textAlign = 'center';
}
restDom.innerText = restPart;
yield this.stdout(line, this.killLine);
}
if (this.cb) {
this.cb();
this.cb = undefined;
}
next();
});
}
}
exports.default = OraStream;
//# sourceMappingURL=ora.js.map