async menu handling
This commit is contained in:
parent
cd5622c98e
commit
3331243872
83
bin/main.js
83
bin/main.js
|
@ -1,8 +1,46 @@
|
||||||
"use strict";
|
"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());
|
||||||
|
});
|
||||||
|
};
|
||||||
|
var __generator = (this && this.__generator) || function (thisArg, body) {
|
||||||
|
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
||||||
|
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
||||||
|
function verb(n) { return function (v) { return step([n, v]); }; }
|
||||||
|
function step(op) {
|
||||||
|
if (f) throw new TypeError("Generator is already executing.");
|
||||||
|
while (_) try {
|
||||||
|
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
||||||
|
if (y = 0, t) op = [op[0] & 2, t.value];
|
||||||
|
switch (op[0]) {
|
||||||
|
case 0: case 1: t = op; break;
|
||||||
|
case 4: _.label++; return { value: op[1], done: false };
|
||||||
|
case 5: _.label++; y = op[1]; op = [0]; continue;
|
||||||
|
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
||||||
|
default:
|
||||||
|
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
||||||
|
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
||||||
|
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
||||||
|
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
||||||
|
if (t[2]) _.ops.pop();
|
||||||
|
_.trys.pop(); continue;
|
||||||
|
}
|
||||||
|
op = body.call(thisArg, _);
|
||||||
|
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
||||||
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
||||||
|
}
|
||||||
|
};
|
||||||
exports.__esModule = true;
|
exports.__esModule = true;
|
||||||
var menu_1 = require("./menu/menu");
|
var menu_1 = require("./menu/menu");
|
||||||
var interface_1 = require("./menu/interface");
|
var interface_1 = require("./menu/interface");
|
||||||
var menu = new menu_1.Menu([
|
var Main = /** @class */ (function () {
|
||||||
|
function Main() {
|
||||||
|
this._menu = [
|
||||||
{
|
{
|
||||||
display: "df -h",
|
display: "df -h",
|
||||||
type: interface_1.MenuType.ExecCommand,
|
type: interface_1.MenuType.ExecCommand,
|
||||||
|
@ -27,5 +65,44 @@ var menu = new menu_1.Menu([
|
||||||
display: "Shutdown",
|
display: "Shutdown",
|
||||||
type: interface_1.MenuType.Shutdown
|
type: interface_1.MenuType.Shutdown
|
||||||
},
|
},
|
||||||
], true);
|
];
|
||||||
menu.getSelection();
|
}
|
||||||
|
Main.prototype.runAsync = function () {
|
||||||
|
return __awaiter(this, void 0, void 0, function () {
|
||||||
|
var selected;
|
||||||
|
return __generator(this, function (_a) {
|
||||||
|
switch (_a.label) {
|
||||||
|
case 0:
|
||||||
|
if (!true) return [3 /*break*/, 2];
|
||||||
|
return [4 /*yield*/, new menu_1.Menu(this._menu, process.env['CONSOLE'] === '1').getSelection()];
|
||||||
|
case 1:
|
||||||
|
selected = _a.sent();
|
||||||
|
switch (selected.type) {
|
||||||
|
case interface_1.MenuType.Shutdown:
|
||||||
|
process.exit();
|
||||||
|
case interface_1.MenuType.Reboot:
|
||||||
|
process.exit();
|
||||||
|
case interface_1.MenuType.ExecCommand:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return [3 /*break*/, 0];
|
||||||
|
case 2: return [2 /*return*/];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
return Main;
|
||||||
|
}());
|
||||||
|
function run() {
|
||||||
|
return __awaiter(this, void 0, void 0, function () {
|
||||||
|
return __generator(this, function (_a) {
|
||||||
|
switch (_a.label) {
|
||||||
|
case 0: return [4 /*yield*/, new Main().runAsync()];
|
||||||
|
case 1:
|
||||||
|
_a.sent();
|
||||||
|
return [2 /*return*/];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
run();
|
||||||
|
|
|
@ -22,6 +22,42 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
||||||
__setModuleDefault(result, mod);
|
__setModuleDefault(result, mod);
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
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());
|
||||||
|
});
|
||||||
|
};
|
||||||
|
var __generator = (this && this.__generator) || function (thisArg, body) {
|
||||||
|
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
||||||
|
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
||||||
|
function verb(n) { return function (v) { return step([n, v]); }; }
|
||||||
|
function step(op) {
|
||||||
|
if (f) throw new TypeError("Generator is already executing.");
|
||||||
|
while (_) try {
|
||||||
|
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
||||||
|
if (y = 0, t) op = [op[0] & 2, t.value];
|
||||||
|
switch (op[0]) {
|
||||||
|
case 0: case 1: t = op; break;
|
||||||
|
case 4: _.label++; return { value: op[1], done: false };
|
||||||
|
case 5: _.label++; y = op[1]; op = [0]; continue;
|
||||||
|
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
||||||
|
default:
|
||||||
|
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
||||||
|
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
||||||
|
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
||||||
|
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
||||||
|
if (t[2]) _.ops.pop();
|
||||||
|
_.trys.pop(); continue;
|
||||||
|
}
|
||||||
|
op = body.call(thisArg, _);
|
||||||
|
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
||||||
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
||||||
|
}
|
||||||
|
};
|
||||||
exports.__esModule = true;
|
exports.__esModule = true;
|
||||||
exports.ConsolePrinter = void 0;
|
exports.ConsolePrinter = void 0;
|
||||||
var interface_1 = require("./interface");
|
var interface_1 = require("./interface");
|
||||||
|
@ -31,7 +67,22 @@ var ConsolePrinter = /** @class */ (function () {
|
||||||
this._path = [];
|
this._path = [];
|
||||||
this._selectedIndex = 0;
|
this._selectedIndex = 0;
|
||||||
this._config = config;
|
this._config = config;
|
||||||
|
readline.emitKeypressEvents(process.stdin);
|
||||||
|
process.stdin.setRawMode(true);
|
||||||
}
|
}
|
||||||
|
ConsolePrinter.prototype.keypress = function () {
|
||||||
|
return __awaiter(this, void 0, void 0, function () {
|
||||||
|
return __generator(this, function (_a) {
|
||||||
|
return [2 /*return*/, new Promise(function (resolve) {
|
||||||
|
var listener = function (_letter, key) {
|
||||||
|
process.stdin.removeListener('keypress', listener);
|
||||||
|
resolve(key);
|
||||||
|
};
|
||||||
|
process.stdin.on('keypress', listener);
|
||||||
|
})];
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
ConsolePrinter.prototype.printMenu = function (curMenu) {
|
ConsolePrinter.prototype.printMenu = function (curMenu) {
|
||||||
console.clear();
|
console.clear();
|
||||||
for (var i = 0; i < curMenu.length; i++) {
|
for (var i = 0; i < curMenu.length; i++) {
|
||||||
|
@ -42,49 +93,65 @@ var ConsolePrinter = /** @class */ (function () {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
ConsolePrinter.prototype.getSelection = function () {
|
ConsolePrinter.prototype.getSelection = function () {
|
||||||
|
return __awaiter(this, void 0, void 0, function () {
|
||||||
var _this = this;
|
var _this = this;
|
||||||
var curMenu = this._config;
|
return __generator(this, function (_a) {
|
||||||
|
return [2 /*return*/, new Promise(function (resolve) { return __awaiter(_this, void 0, void 0, function () {
|
||||||
|
var curMenu, key, i;
|
||||||
|
return __generator(this, function (_a) {
|
||||||
|
switch (_a.label) {
|
||||||
|
case 0:
|
||||||
|
curMenu = this._config;
|
||||||
|
_a.label = 1;
|
||||||
|
case 1:
|
||||||
|
if (!true) return [3 /*break*/, 3];
|
||||||
|
// print menu
|
||||||
this.printMenu(curMenu);
|
this.printMenu(curMenu);
|
||||||
// get user input
|
return [4 /*yield*/, this.keypress()];
|
||||||
readline.emitKeypressEvents(process.stdin);
|
case 2:
|
||||||
process.stdin.setRawMode(true);
|
key = _a.sent();
|
||||||
process.stdin.on('keypress', function (_letter, key) {
|
|
||||||
if (key.ctrl && key.name === 'c') {
|
if (key.ctrl && key.name === 'c') {
|
||||||
process.exit();
|
process.exit();
|
||||||
}
|
}
|
||||||
else if (key.name === 'up') {
|
else if (key.name === 'up') {
|
||||||
_this._selectedIndex--;
|
this._selectedIndex--;
|
||||||
if (_this._selectedIndex < 0) {
|
if (this._selectedIndex < 0) {
|
||||||
_this._selectedIndex = 0;
|
this._selectedIndex = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (key.name === 'down') {
|
else if (key.name === 'down') {
|
||||||
_this._selectedIndex++;
|
this._selectedIndex++;
|
||||||
if (_this._selectedIndex > curMenu.length - 1) {
|
if (this._selectedIndex > curMenu.length - 1) {
|
||||||
_this._selectedIndex = curMenu.length - 1;
|
this._selectedIndex = curMenu.length - 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (key.name === 'left') {
|
else if (key.name === 'left') {
|
||||||
if (_this._path.length > 0) {
|
if (this._path.length > 0) {
|
||||||
_this._path.pop();
|
this._path.pop();
|
||||||
|
this._selectedIndex = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (key.name === 'right' || key.name === 'enter') {
|
else if (key.name === 'right' || key.name === 'return') {
|
||||||
if (curMenu[_this._selectedIndex].type === interface_1.MenuType.SubMenu) {
|
this._path.push(this._selectedIndex);
|
||||||
_this._path.push(_this._selectedIndex);
|
this._selectedIndex = 0;
|
||||||
_this._selectedIndex = 0;
|
}
|
||||||
|
curMenu = this._config;
|
||||||
|
for (i = 0; i < this._path.length; i++) {
|
||||||
|
if (curMenu[this._path[i]].type === interface_1.MenuType.SubMenu) {
|
||||||
|
curMenu = curMenu[this._path[i]].subMenu;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
process.exit();
|
resolve(curMenu[this._path[i]]);
|
||||||
|
return [2 /*return*/];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
curMenu = _this._config;
|
return [3 /*break*/, 1];
|
||||||
for (var i = 0; i < _this._path.length; i++) {
|
case 3: return [2 /*return*/];
|
||||||
curMenu = curMenu[_this._path[i]].subMenu;
|
|
||||||
}
|
}
|
||||||
_this.printMenu(curMenu);
|
|
||||||
});
|
});
|
||||||
return this._config[this._selectedIndex];
|
}); })];
|
||||||
|
});
|
||||||
|
});
|
||||||
};
|
};
|
||||||
return ConsolePrinter;
|
return ConsolePrinter;
|
||||||
}());
|
}());
|
||||||
|
|
|
@ -1,4 +1,40 @@
|
||||||
"use strict";
|
"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());
|
||||||
|
});
|
||||||
|
};
|
||||||
|
var __generator = (this && this.__generator) || function (thisArg, body) {
|
||||||
|
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
||||||
|
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
||||||
|
function verb(n) { return function (v) { return step([n, v]); }; }
|
||||||
|
function step(op) {
|
||||||
|
if (f) throw new TypeError("Generator is already executing.");
|
||||||
|
while (_) try {
|
||||||
|
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
||||||
|
if (y = 0, t) op = [op[0] & 2, t.value];
|
||||||
|
switch (op[0]) {
|
||||||
|
case 0: case 1: t = op; break;
|
||||||
|
case 4: _.label++; return { value: op[1], done: false };
|
||||||
|
case 5: _.label++; y = op[1]; op = [0]; continue;
|
||||||
|
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
||||||
|
default:
|
||||||
|
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
||||||
|
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
||||||
|
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
||||||
|
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
||||||
|
if (t[2]) _.ops.pop();
|
||||||
|
_.trys.pop(); continue;
|
||||||
|
}
|
||||||
|
op = body.call(thisArg, _);
|
||||||
|
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
||||||
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
||||||
|
}
|
||||||
|
};
|
||||||
exports.__esModule = true;
|
exports.__esModule = true;
|
||||||
exports.Menu = void 0;
|
exports.Menu = void 0;
|
||||||
var console_1 = require("./console");
|
var console_1 = require("./console");
|
||||||
|
@ -11,7 +47,11 @@ var Menu = /** @class */ (function () {
|
||||||
: new oled_1.OledPrinter(config);
|
: new oled_1.OledPrinter(config);
|
||||||
}
|
}
|
||||||
Menu.prototype.getSelection = function () {
|
Menu.prototype.getSelection = function () {
|
||||||
return this._printer.getSelection();
|
return __awaiter(this, void 0, void 0, function () {
|
||||||
|
return __generator(this, function (_a) {
|
||||||
|
return [2 /*return*/, this._printer.getSelection()];
|
||||||
|
});
|
||||||
|
});
|
||||||
};
|
};
|
||||||
return Menu;
|
return Menu;
|
||||||
}());
|
}());
|
||||||
|
|
|
@ -1,4 +1,40 @@
|
||||||
"use strict";
|
"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());
|
||||||
|
});
|
||||||
|
};
|
||||||
|
var __generator = (this && this.__generator) || function (thisArg, body) {
|
||||||
|
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
||||||
|
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
||||||
|
function verb(n) { return function (v) { return step([n, v]); }; }
|
||||||
|
function step(op) {
|
||||||
|
if (f) throw new TypeError("Generator is already executing.");
|
||||||
|
while (_) try {
|
||||||
|
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
||||||
|
if (y = 0, t) op = [op[0] & 2, t.value];
|
||||||
|
switch (op[0]) {
|
||||||
|
case 0: case 1: t = op; break;
|
||||||
|
case 4: _.label++; return { value: op[1], done: false };
|
||||||
|
case 5: _.label++; y = op[1]; op = [0]; continue;
|
||||||
|
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
||||||
|
default:
|
||||||
|
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
||||||
|
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
||||||
|
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
||||||
|
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
||||||
|
if (t[2]) _.ops.pop();
|
||||||
|
_.trys.pop(); continue;
|
||||||
|
}
|
||||||
|
op = body.call(thisArg, _);
|
||||||
|
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
||||||
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
||||||
|
}
|
||||||
|
};
|
||||||
exports.__esModule = true;
|
exports.__esModule = true;
|
||||||
exports.OledPrinter = void 0;
|
exports.OledPrinter = void 0;
|
||||||
/*const i2cbus = openSync(1);
|
/*const i2cbus = openSync(1);
|
||||||
|
@ -19,7 +55,11 @@ var OledPrinter = /** @class */ (function () {
|
||||||
this._config = config;
|
this._config = config;
|
||||||
}
|
}
|
||||||
OledPrinter.prototype.getSelection = function () {
|
OledPrinter.prototype.getSelection = function () {
|
||||||
return this._config[0];
|
return __awaiter(this, void 0, void 0, function () {
|
||||||
|
return __generator(this, function (_a) {
|
||||||
|
return [2 /*return*/, this._config[0]];
|
||||||
|
});
|
||||||
|
});
|
||||||
};
|
};
|
||||||
return OledPrinter;
|
return OledPrinter;
|
||||||
}());
|
}());
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
{
|
{
|
||||||
|
"name": "z3r0haxx",
|
||||||
|
"version": "1.0.0",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"clean": "rm -rf bin/*",
|
"clean": "rm -rf bin/*",
|
||||||
"build": "npm run clean && tsc --project tsconfig.json",
|
"build": "npm run clean && tsc --project tsconfig.json",
|
||||||
|
|
26
src/main.ts
26
src/main.ts
|
@ -1,7 +1,8 @@
|
||||||
import { Menu } from './menu/menu';
|
import { Menu } from './menu/menu';
|
||||||
import { MenuType } from './menu/interface';
|
import { MenuType } from './menu/interface';
|
||||||
|
|
||||||
const menu = new Menu([
|
class Main {
|
||||||
|
private readonly _menu = [
|
||||||
{
|
{
|
||||||
display: "df -h",
|
display: "df -h",
|
||||||
type: MenuType.ExecCommand,
|
type: MenuType.ExecCommand,
|
||||||
|
@ -26,6 +27,25 @@ const menu = new Menu([
|
||||||
display: "Shutdown",
|
display: "Shutdown",
|
||||||
type: MenuType.Shutdown,
|
type: MenuType.Shutdown,
|
||||||
},
|
},
|
||||||
], true);
|
];
|
||||||
|
|
||||||
menu.getSelection();
|
public async runAsync(): Promise<void> {
|
||||||
|
while (true) {
|
||||||
|
const selected = await new Menu(this._menu, process.env['CONSOLE'] === '1').getSelection();
|
||||||
|
switch(selected.type) {
|
||||||
|
case MenuType.Shutdown:
|
||||||
|
process.exit();
|
||||||
|
case MenuType.Reboot:
|
||||||
|
process.exit();
|
||||||
|
case MenuType.ExecCommand:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function run() {
|
||||||
|
await new Main().runAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
run();
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
import { MenuConfig, MenuPrinter, MenuType } from './interface';
|
import { MenuConfig, MenuPrinter, MenuType } from './interface';
|
||||||
import * as readline from 'readline';
|
import * as readline from 'readline';
|
||||||
|
|
||||||
|
type Key = {name: string, ctrl: boolean};
|
||||||
|
|
||||||
export class ConsolePrinter implements MenuPrinter {
|
export class ConsolePrinter implements MenuPrinter {
|
||||||
private _path: number[] = [];
|
private _path: number[] = [];
|
||||||
private _selectedIndex: number = 0;
|
private _selectedIndex: number = 0;
|
||||||
|
@ -8,6 +10,18 @@ export class ConsolePrinter implements MenuPrinter {
|
||||||
|
|
||||||
constructor(config: MenuConfig[]) {
|
constructor(config: MenuConfig[]) {
|
||||||
this._config = config;
|
this._config = config;
|
||||||
|
readline.emitKeypressEvents(process.stdin);
|
||||||
|
process.stdin.setRawMode(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async keypress(): Promise<Key> {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
const listener = (_letter: string, key: Key): void => {
|
||||||
|
process.stdin.removeListener('keypress', listener);
|
||||||
|
resolve(key);
|
||||||
|
}
|
||||||
|
process.stdin.on('keypress', listener);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private printMenu(curMenu: MenuConfig[]): void {
|
private printMenu(curMenu: MenuConfig[]): void {
|
||||||
|
@ -20,14 +34,16 @@ export class ConsolePrinter implements MenuPrinter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public getSelection(): MenuConfig {
|
public async getSelection(): Promise<MenuConfig> {
|
||||||
|
return new Promise(async (resolve) => {
|
||||||
let curMenu = this._config;
|
let curMenu = this._config;
|
||||||
|
while (true) {
|
||||||
|
// print menu
|
||||||
this.printMenu(curMenu);
|
this.printMenu(curMenu);
|
||||||
|
|
||||||
// get user input
|
// get user input
|
||||||
readline.emitKeypressEvents(process.stdin);
|
const key = await this.keypress();
|
||||||
process.stdin.setRawMode(true);
|
|
||||||
process.stdin.on('keypress', (_letter, key: any) => {
|
|
||||||
if (key.ctrl && key.name === 'c') {
|
if (key.ctrl && key.name === 'c') {
|
||||||
process.exit();
|
process.exit();
|
||||||
} else if (key.name === 'up') {
|
} else if (key.name === 'up') {
|
||||||
|
@ -43,21 +59,22 @@ export class ConsolePrinter implements MenuPrinter {
|
||||||
} else if (key.name === 'left') {
|
} else if (key.name === 'left') {
|
||||||
if (this._path.length > 0) {
|
if (this._path.length > 0) {
|
||||||
this._path.pop();
|
this._path.pop();
|
||||||
|
this._selectedIndex = 0;
|
||||||
}
|
}
|
||||||
} else if (key.name === 'right' || key.name === 'enter') {
|
} else if (key.name === 'right' || key.name === 'return') {
|
||||||
if (curMenu[this._selectedIndex].type === MenuType.SubMenu) {
|
|
||||||
this._path.push(this._selectedIndex);
|
this._path.push(this._selectedIndex);
|
||||||
this._selectedIndex = 0;
|
this._selectedIndex = 0;
|
||||||
} else {
|
|
||||||
process.exit()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
curMenu = this._config;
|
curMenu = this._config;
|
||||||
for (let i = 0; i < this._path.length; i++) {
|
for (let i = 0; i < this._path.length; i++) {
|
||||||
|
if (curMenu[this._path[i]].type === MenuType.SubMenu) {
|
||||||
curMenu = curMenu[this._path[i]].subMenu!;
|
curMenu = curMenu[this._path[i]].subMenu!;
|
||||||
|
} else {
|
||||||
|
resolve(curMenu[this._path[i]]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
this.printMenu(curMenu);
|
|
||||||
});
|
});
|
||||||
return this._config[this._selectedIndex];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,5 +13,5 @@ export interface MenuConfig {
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface MenuPrinter {
|
export interface MenuPrinter {
|
||||||
getSelection(): MenuConfig;
|
getSelection(): Promise<MenuConfig>;
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ export class Menu {
|
||||||
: new OledPrinter(config);
|
: new OledPrinter(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
public getSelection(): MenuConfig {
|
public async getSelection(): Promise<MenuConfig> {
|
||||||
return this._printer.getSelection();
|
return this._printer.getSelection();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ export class OledPrinter implements MenuPrinter {
|
||||||
this._config = config;
|
this._config = config;
|
||||||
}
|
}
|
||||||
|
|
||||||
public getSelection(): MenuConfig {
|
public async getSelection(): Promise<MenuConfig> {
|
||||||
return this._config[0];
|
return this._config[0];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue