async menu handling

This commit is contained in:
Rudis Muiznieks 2022-04-20 08:05:21 -05:00
parent cd5622c98e
commit 3331243872
Signed by: rudism
GPG Key ID: CABF2F86EF7884F9
10 changed files with 393 additions and 130 deletions

View File

@ -1,31 +1,108 @@
"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() {
display: "df -h", this._menu = [
type: interface_1.MenuType.ExecCommand,
command: ["df", "-h"]
},
{
display: "Games",
type: interface_1.MenuType.SubMenu,
subMenu: [
{ {
display: "Chess", display: "df -h",
type: interface_1.MenuType.ExecCommand, type: interface_1.MenuType.ExecCommand,
command: ["df", "-h"] command: ["df", "-h"]
}, },
] {
}, display: "Games",
{ type: interface_1.MenuType.SubMenu,
display: "Reboot", subMenu: [
type: interface_1.MenuType.Reboot {
}, display: "Chess",
{ type: interface_1.MenuType.ExecCommand,
display: "Shutdown", command: ["df", "-h"]
type: interface_1.MenuType.Shutdown },
}, ]
], true); },
menu.getSelection(); {
display: "Reboot",
type: interface_1.MenuType.Reboot
},
{
display: "Shutdown",
type: interface_1.MenuType.Shutdown
},
];
}
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();

View File

@ -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 () {
var _this = this; return __awaiter(this, void 0, void 0, function () {
var curMenu = this._config; var _this = this;
this.printMenu(curMenu); return __generator(this, function (_a) {
// get user input return [2 /*return*/, new Promise(function (resolve) { return __awaiter(_this, void 0, void 0, function () {
readline.emitKeypressEvents(process.stdin); var curMenu, key, i;
process.stdin.setRawMode(true); return __generator(this, function (_a) {
process.stdin.on('keypress', function (_letter, key) { switch (_a.label) {
if (key.ctrl && key.name === 'c') { case 0:
process.exit(); curMenu = this._config;
} _a.label = 1;
else if (key.name === 'up') { case 1:
_this._selectedIndex--; if (!true) return [3 /*break*/, 3];
if (_this._selectedIndex < 0) { // print menu
_this._selectedIndex = 0; this.printMenu(curMenu);
} return [4 /*yield*/, this.keypress()];
} case 2:
else if (key.name === 'down') { key = _a.sent();
_this._selectedIndex++; if (key.ctrl && key.name === 'c') {
if (_this._selectedIndex > curMenu.length - 1) { process.exit();
_this._selectedIndex = curMenu.length - 1; }
} else if (key.name === 'up') {
} this._selectedIndex--;
else if (key.name === 'left') { if (this._selectedIndex < 0) {
if (_this._path.length > 0) { this._selectedIndex = 0;
_this._path.pop(); }
} }
} else if (key.name === 'down') {
else if (key.name === 'right' || key.name === 'enter') { this._selectedIndex++;
if (curMenu[_this._selectedIndex].type === interface_1.MenuType.SubMenu) { if (this._selectedIndex > curMenu.length - 1) {
_this._path.push(_this._selectedIndex); this._selectedIndex = curMenu.length - 1;
_this._selectedIndex = 0; }
} }
else { else if (key.name === 'left') {
process.exit(); if (this._path.length > 0) {
} this._path.pop();
} this._selectedIndex = 0;
curMenu = _this._config; }
for (var i = 0; i < _this._path.length; i++) { }
curMenu = curMenu[_this._path[i]].subMenu; else if (key.name === 'right' || key.name === 'return') {
} this._path.push(this._selectedIndex);
_this.printMenu(curMenu); 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 {
resolve(curMenu[this._path[i]]);
return [2 /*return*/];
}
}
return [3 /*break*/, 1];
case 3: return [2 /*return*/];
}
});
}); })];
});
}); });
return this._config[this._selectedIndex];
}; };
return ConsolePrinter; return ConsolePrinter;
}()); }());

View File

@ -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;
}()); }());

View File

@ -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;
}()); }());

View File

@ -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",

View File

@ -1,31 +1,51 @@
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", {
type: MenuType.ExecCommand, display: "df -h",
command: ["df", "-h"], type: MenuType.ExecCommand,
}, command: ["df", "-h"],
{ },
display: "Games", {
type: MenuType.SubMenu, display: "Games",
subMenu: [ type: MenuType.SubMenu,
{ subMenu: [
display: "Chess", {
type: MenuType.ExecCommand, display: "Chess",
command: ["df", "-h"], type: MenuType.ExecCommand,
}, command: ["df", "-h"],
], },
}, ],
{ },
display: "Reboot", {
type: MenuType.Reboot, display: "Reboot",
}, type: MenuType.Reboot,
{ },
display: "Shutdown", {
type: MenuType.Shutdown, display: "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();

View File

@ -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,44 +34,47 @@ export class ConsolePrinter implements MenuPrinter {
} }
} }
public getSelection(): MenuConfig { public async getSelection(): Promise<MenuConfig> {
let curMenu = this._config; return new Promise(async (resolve) => {
this.printMenu(curMenu); let curMenu = this._config;
while (true) {
// print menu
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') { 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 === MenuType.SubMenu) {
this._path.push(this._selectedIndex); this._path.push(this._selectedIndex);
this._selectedIndex = 0; this._selectedIndex = 0;
} else { }
process.exit() curMenu = this._config;
for (let i = 0; i < this._path.length; i++) {
if (curMenu[this._path[i]].type === MenuType.SubMenu) {
curMenu = curMenu[this._path[i]].subMenu!;
} else {
resolve(curMenu[this._path[i]]);
return;
}
} }
} }
curMenu = this._config;
for (let i = 0; i < this._path.length; i++) {
curMenu = curMenu[this._path[i]].subMenu!;
}
this.printMenu(curMenu);
}); });
return this._config[this._selectedIndex];
} }
} }

View File

@ -13,5 +13,5 @@ export interface MenuConfig {
} }
export interface MenuPrinter { export interface MenuPrinter {
getSelection(): MenuConfig; getSelection(): Promise<MenuConfig>;
} }

View File

@ -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();
} }
} }

View File

@ -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];
} }
} }