@zzck.dev/tui
Version:
TS Text-based user inteface & commandline parser
128 lines (127 loc) • 5.04 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 });
exports.TUI = exports.TUICommandNotFoundError = exports.TUIError = void 0;
const action_1 = require("./action");
const action_call_1 = require("./action-call");
const exit = "exit";
const ask = '$ ';
class TUIError extends Error {
constructor(msg) {
super(msg);
this.name = "TUIError";
}
}
exports.TUIError = TUIError;
class TUICommandNotFoundError extends TUIError {
constructor(command) {
super(`Command "${command}" not found`);
this.name = "TUICommandNotFoundError";
}
}
exports.TUICommandNotFoundError = TUICommandNotFoundError;
class TUI {
constructor(terminal, partialActions) {
this._stopRequested = false;
this._terminal = terminal;
this._actions = partialActions.map(partialAction => new action_1.Action(partialAction));
const _userExitAction = partialActions.find((action) => {
if (action.command == exit)
return action;
});
this._actions.push(new action_1.Action({
command: exit,
description: `Calls user defined "exit" if defined and exits. User defined "exit" should not accept any args.`,
callback: this.Exit.bind(this)
}));
}
Exit() {
return __awaiter(this, void 0, void 0, function* () {
this._stopRequested = true;
if (this._userExitAction)
yield this._userExitAction.callback();
yield this._terminal.close();
});
}
RunActionCall(action, actionCall) {
return __awaiter(this, void 0, void 0, function* () {
return yield action.callback(...actionCall.args);
});
}
RunActionCallPrintResult(action, actionCall) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.RunActionCall(action, actionCall);
if (res) {
yield this._terminal.write(JSON.stringify(res, null, 2));
yield this._terminal.write("\r\n");
}
});
}
ActionFromActionCall(actionCall) {
return __awaiter(this, void 0, void 0, function* () {
const action = this._actions.find((action) => {
if (action.command == actionCall.command)
return action;
});
if (!action)
throw new TUICommandNotFoundError(actionCall.command);
return action;
});
}
RunIteration(actionCall) {
return __awaiter(this, void 0, void 0, function* () {
try {
const action = yield this.ActionFromActionCall(actionCall);
yield this.RunActionCallPrintResult(action, actionCall);
}
catch (error) {
if (error instanceof TUICommandNotFoundError) {
yield this._terminal.write(error.message);
yield this._terminal.write("\r\n");
yield this.Help();
}
else if (error instanceof TUIError) {
yield this._terminal.write(error.message);
yield this._terminal.write("\r\n");
}
else
throw error;
}
});
}
Help() {
return __awaiter(this, void 0, void 0, function* () {
for (const { command, description } of this._actions) {
yield this._terminal.write(` "${command}"\r\n`);
yield this._terminal.write(` Description: ${description}\r\n`);
}
});
}
Run() {
return __awaiter(this, void 0, void 0, function* () {
while (!this._stopRequested) {
const input = yield this._terminal.question(ask);
if (input.length == 0)
continue;
const actionCall = new action_call_1.ActionCall(input);
yield this.RunIteration(actionCall);
}
});
}
RunActionCalls(actionCallsProvider) {
return __awaiter(this, void 0, void 0, function* () {
for (const actionCall of actionCallsProvider.actioncalls) {
yield this.RunIteration(actionCall);
}
});
}
}
exports.TUI = TUI;