removing shit node stuff
This commit is contained in:
parent
8f9dd3e6d6
commit
aa893d800a
|
@ -1,2 +0,0 @@
|
|||
declare module 'oled-font-5x7' {
|
||||
}
|
|
@ -1,36 +0,0 @@
|
|||
declare module 'oled-i2c-bus' {
|
||||
import { I2CBus } from 'i2c-bus';
|
||||
|
||||
class Oled {
|
||||
constructor(i2c: I2CBus, opts: OLED.Options);
|
||||
clearDisplay(): void;
|
||||
dimDisplay(dimming: boolean): void;
|
||||
invertDisplay(invert: boolean): void;
|
||||
turnOffDisplay(): void;
|
||||
turnOnDisplay(): void;
|
||||
drawPixel(pixels: OLED.Point[]): void;
|
||||
drawLine(x0: number, y0: number, x1: number, y1: number, color: number): void;
|
||||
fillRect(x0: number, y0: number, x1: number, y1: number, color: number): void;
|
||||
drawBitmap(bitmap: any): void;
|
||||
drawRGBAImage(image: any, x: number, y: number): void;
|
||||
startScroll(direction: 'left'|'right', startRow: number, endRow: number): void;
|
||||
stopScroll(): void;
|
||||
setCursor(x: number, y: number): void;
|
||||
writeString(font: any, size: number, text: string, color: number, wrap: boolean, sync?: boolean): void;
|
||||
update(): void;
|
||||
}
|
||||
|
||||
namespace OLED {
|
||||
interface Options {
|
||||
height?: number;
|
||||
width?: number;
|
||||
address?: number;
|
||||
linespacing?: number;
|
||||
letterspacing?: number;
|
||||
}
|
||||
|
||||
type Point = [number, number, number];
|
||||
}
|
||||
|
||||
export = Oled;
|
||||
}
|
|
@ -1,52 +0,0 @@
|
|||
"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.Chess = void 0;
|
||||
var Chess = /** @class */ (function () {
|
||||
function Chess() {
|
||||
}
|
||||
Chess.prototype.runAsync = function () {
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
return __generator(this, function (_a) {
|
||||
return [2 /*return*/];
|
||||
});
|
||||
});
|
||||
};
|
||||
return Chess;
|
||||
}());
|
||||
exports.Chess = Chess;
|
|
@ -1,63 +0,0 @@
|
|||
"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.ConsolePrinter = void 0;
|
||||
var ConsolePrinter = /** @class */ (function () {
|
||||
function ConsolePrinter() {
|
||||
}
|
||||
ConsolePrinter.prototype.displayContent = function (content) {
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
return __generator(this, function (_a) {
|
||||
console.log(content);
|
||||
console.log("Press a key...");
|
||||
return [2 /*return*/, new Promise(function (resolve) {
|
||||
var listener = function (_letter, _key) {
|
||||
process.stdin.removeListener('keypress', listener);
|
||||
resolve();
|
||||
};
|
||||
process.stdin.on('keypress', listener);
|
||||
})];
|
||||
});
|
||||
});
|
||||
};
|
||||
ConsolePrinter.prototype.clear = function (_) {
|
||||
console.clear();
|
||||
};
|
||||
return ConsolePrinter;
|
||||
}());
|
||||
exports.ConsolePrinter = ConsolePrinter;
|
|
@ -1,69 +0,0 @@
|
|||
"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.Display = void 0;
|
||||
var console_1 = require("./console");
|
||||
var oled_1 = require("./oled");
|
||||
var Display = /** @class */ (function () {
|
||||
function Display(useConsole) {
|
||||
this._display = useConsole
|
||||
? new console_1.ConsolePrinter()
|
||||
: new oled_1.OledPrinter();
|
||||
}
|
||||
Display.prototype.displayContent = function (content, wrap) {
|
||||
if (wrap === void 0) { wrap = true; }
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0:
|
||||
this._display.clear(false);
|
||||
return [4 /*yield*/, this._display.displayContent(content, wrap)];
|
||||
case 1:
|
||||
_a.sent();
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
Display.prototype.clear = function (turnOff) {
|
||||
if (turnOff === void 0) { turnOff = false; }
|
||||
this._display.clear(turnOff);
|
||||
};
|
||||
return Display;
|
||||
}());
|
||||
exports.Display = Display;
|
|
@ -1,2 +0,0 @@
|
|||
"use strict";
|
||||
exports.__esModule = true;
|
|
@ -1,54 +0,0 @@
|
|||
"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.OledPrinter = void 0;
|
||||
var OledPrinter = /** @class */ (function () {
|
||||
function OledPrinter() {
|
||||
}
|
||||
OledPrinter.prototype.displayContent = function (_content, _wrap) {
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
return __generator(this, function (_a) {
|
||||
return [2 /*return*/];
|
||||
});
|
||||
});
|
||||
};
|
||||
OledPrinter.prototype.clear = function (_turnOff) {
|
||||
};
|
||||
return OledPrinter;
|
||||
}());
|
||||
exports.OledPrinter = OledPrinter;
|
165
bin/main.js
165
bin/main.js
|
@ -1,165 +0,0 @@
|
|||
"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;
|
||||
var menu_1 = require("./menu/menu");
|
||||
var interface_1 = require("./menu/interface");
|
||||
var display_1 = require("./display/display");
|
||||
var chess_1 = require("./chess/chess");
|
||||
var child_process_1 = require("child_process");
|
||||
var Main = /** @class */ (function () {
|
||||
function Main() {
|
||||
this._menuConfig = [
|
||||
{
|
||||
display: "df -h",
|
||||
type: interface_1.MenuType.ExecCommand,
|
||||
command: { exe: "df", args: ['-h'] }
|
||||
},
|
||||
{
|
||||
display: "Games",
|
||||
type: interface_1.MenuType.SubMenu,
|
||||
subMenu: [
|
||||
{
|
||||
display: "Chess",
|
||||
type: interface_1.MenuType.ExecModule,
|
||||
module: new chess_1.Chess().runAsync
|
||||
},
|
||||
]
|
||||
},
|
||||
{
|
||||
display: "Reboot",
|
||||
type: interface_1.MenuType.Reboot
|
||||
},
|
||||
{
|
||||
display: "Shutdown",
|
||||
type: interface_1.MenuType.Shutdown
|
||||
},
|
||||
];
|
||||
this._console = process.env['CONSOLE'] === '1';
|
||||
this._menu = new menu_1.Menu(this._menuConfig, this._console);
|
||||
this._display = new display_1.Display(this._console);
|
||||
}
|
||||
Main.prototype.runCommand = function (cmd) {
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
var _this = this;
|
||||
return __generator(this, function (_a) {
|
||||
return [2 /*return*/, new Promise(function (resolve) {
|
||||
var _a;
|
||||
_this._display.displayContent("Executing command:\n> ".concat(cmd.exe, " ").concat((_a = cmd.args) === null || _a === void 0 ? void 0 : _a.join(' ')));
|
||||
var output = '';
|
||||
var child = (0, child_process_1.spawn)(cmd.exe, cmd.args);
|
||||
child.stdout.on('data', function (data) {
|
||||
output += data;
|
||||
});
|
||||
child.on('exit', function () { return __awaiter(_this, void 0, void 0, function () {
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0: return [4 /*yield*/, this._display.displayContent(output, cmd.wrap)];
|
||||
case 1:
|
||||
_a.sent();
|
||||
resolve();
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
}); });
|
||||
})];
|
||||
});
|
||||
});
|
||||
};
|
||||
Main.prototype.runAsync = function () {
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
var selected, _a;
|
||||
return __generator(this, function (_b) {
|
||||
switch (_b.label) {
|
||||
case 0:
|
||||
if (!true) return [3 /*break*/, 11];
|
||||
return [4 /*yield*/, this._menu.getSelection()];
|
||||
case 1:
|
||||
selected = _b.sent();
|
||||
_a = selected.type;
|
||||
switch (_a) {
|
||||
case interface_1.MenuType.Shutdown: return [3 /*break*/, 2];
|
||||
case interface_1.MenuType.Reboot: return [3 /*break*/, 3];
|
||||
case interface_1.MenuType.ExecCommand: return [3 /*break*/, 4];
|
||||
case interface_1.MenuType.ExecModule: return [3 /*break*/, 7];
|
||||
}
|
||||
return [3 /*break*/, 10];
|
||||
case 2:
|
||||
this._display.clear(true);
|
||||
(0, child_process_1.exec)('sudo shutdown now');
|
||||
process.exit();
|
||||
_b.label = 3;
|
||||
case 3:
|
||||
this._display.clear();
|
||||
(0, child_process_1.exec)('sudo reboot');
|
||||
process.exit();
|
||||
_b.label = 4;
|
||||
case 4:
|
||||
if (!selected.command) return [3 /*break*/, 6];
|
||||
return [4 /*yield*/, this.runCommand(selected.command)];
|
||||
case 5:
|
||||
_b.sent();
|
||||
_b.label = 6;
|
||||
case 6: return [3 /*break*/, 10];
|
||||
case 7:
|
||||
if (!selected.module) return [3 /*break*/, 9];
|
||||
return [4 /*yield*/, selected.module()];
|
||||
case 8:
|
||||
_b.sent();
|
||||
_b.label = 9;
|
||||
case 9: return [3 /*break*/, 10];
|
||||
case 10: return [3 /*break*/, 0];
|
||||
case 11: 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();
|
|
@ -1,157 +0,0 @@
|
|||
"use strict";
|
||||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
var desc = Object.getOwnPropertyDescriptor(m, k);
|
||||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
||||
desc = { enumerable: true, get: function() { return m[k]; } };
|
||||
}
|
||||
Object.defineProperty(o, k2, desc);
|
||||
}) : (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
o[k2] = m[k];
|
||||
}));
|
||||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
||||
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
||||
}) : function(o, v) {
|
||||
o["default"] = v;
|
||||
});
|
||||
var __importStar = (this && this.__importStar) || function (mod) {
|
||||
if (mod && mod.__esModule) return mod;
|
||||
var result = {};
|
||||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
||||
__setModuleDefault(result, mod);
|
||||
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.ConsolePrinter = void 0;
|
||||
var interface_1 = require("./interface");
|
||||
var readline = __importStar(require("readline"));
|
||||
var ConsolePrinter = /** @class */ (function () {
|
||||
function ConsolePrinter(config) {
|
||||
this._path = [];
|
||||
this._selectedIndex = 0;
|
||||
this._config = config;
|
||||
this._curMenu = 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 () {
|
||||
console.clear();
|
||||
for (var i = 0; i < this._curMenu.length; i++) {
|
||||
var line = (i === this._selectedIndex
|
||||
? "> "
|
||||
: " ") + this._curMenu[i].display;
|
||||
console.log(line);
|
||||
}
|
||||
};
|
||||
ConsolePrinter.prototype.getSelection = function () {
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
var _this = this;
|
||||
return __generator(this, function (_a) {
|
||||
return [2 /*return*/, new Promise(function (resolve) { return __awaiter(_this, void 0, void 0, function () {
|
||||
var key, i;
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0:
|
||||
if (!true) return [3 /*break*/, 2];
|
||||
// print menu
|
||||
this.printMenu();
|
||||
return [4 /*yield*/, this.keypress()];
|
||||
case 1:
|
||||
key = _a.sent();
|
||||
if (key.ctrl && key.name === 'c') {
|
||||
process.exit();
|
||||
}
|
||||
else if (key.name === 'up') {
|
||||
this._selectedIndex--;
|
||||
if (this._selectedIndex < 0) {
|
||||
this._selectedIndex = 0;
|
||||
}
|
||||
}
|
||||
else if (key.name === 'down') {
|
||||
this._selectedIndex++;
|
||||
if (this._selectedIndex > this._curMenu.length - 1) {
|
||||
this._selectedIndex = this._curMenu.length - 1;
|
||||
}
|
||||
}
|
||||
else if (key.name === 'left') {
|
||||
if (this._path.length > 0) {
|
||||
this._path.pop();
|
||||
this._selectedIndex = 0;
|
||||
}
|
||||
}
|
||||
else if (key.name === 'right' || key.name === 'return') {
|
||||
this._path.push(this._selectedIndex);
|
||||
}
|
||||
this._curMenu = this._config;
|
||||
for (i = 0; i < this._path.length; i++) {
|
||||
if (this._curMenu[this._path[i]].type === interface_1.MenuType.SubMenu) {
|
||||
this._curMenu = this._curMenu[this._path[i]].subMenu;
|
||||
this._selectedIndex = 0;
|
||||
}
|
||||
else {
|
||||
resolve(this._curMenu[this._path[i]]);
|
||||
this._path.pop();
|
||||
return [2 /*return*/];
|
||||
}
|
||||
}
|
||||
return [3 /*break*/, 0];
|
||||
case 2: return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
}); })];
|
||||
});
|
||||
});
|
||||
};
|
||||
return ConsolePrinter;
|
||||
}());
|
||||
exports.ConsolePrinter = ConsolePrinter;
|
|
@ -1,11 +0,0 @@
|
|||
"use strict";
|
||||
exports.__esModule = true;
|
||||
exports.MenuType = void 0;
|
||||
var MenuType;
|
||||
(function (MenuType) {
|
||||
MenuType[MenuType["ExecCommand"] = 0] = "ExecCommand";
|
||||
MenuType[MenuType["ExecModule"] = 1] = "ExecModule";
|
||||
MenuType[MenuType["Reboot"] = 2] = "Reboot";
|
||||
MenuType[MenuType["Shutdown"] = 3] = "Shutdown";
|
||||
MenuType[MenuType["SubMenu"] = 4] = "SubMenu";
|
||||
})(MenuType = exports.MenuType || (exports.MenuType = {}));
|
|
@ -1,58 +0,0 @@
|
|||
"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.Menu = void 0;
|
||||
var console_1 = require("./console");
|
||||
var oled_1 = require("./oled");
|
||||
var Menu = /** @class */ (function () {
|
||||
function Menu(config, isConsole) {
|
||||
if (isConsole === void 0) { isConsole = false; }
|
||||
this._printer = isConsole
|
||||
? new console_1.ConsolePrinter(config)
|
||||
: new oled_1.OledPrinter(config);
|
||||
}
|
||||
Menu.prototype.getSelection = function () {
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
return __generator(this, function (_a) {
|
||||
return [2 /*return*/, this._printer.getSelection()];
|
||||
});
|
||||
});
|
||||
};
|
||||
return Menu;
|
||||
}());
|
||||
exports.Menu = Menu;
|
|
@ -1,90 +0,0 @@
|
|||
"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 };
|
||||
}
|
||||
};
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
exports.__esModule = true;
|
||||
exports.OledPrinter = void 0;
|
||||
var i2c_bus_1 = require("i2c-bus");
|
||||
var oled_i2c_bus_1 = __importDefault(require("oled-i2c-bus"));
|
||||
var oled_font_5x7_1 = __importDefault(require("oled-font-5x7"));
|
||||
/*const i2cbus = openSync(1);
|
||||
const opts = {
|
||||
width: 128,
|
||||
height: 64,
|
||||
address: 0x3C,
|
||||
};
|
||||
|
||||
const oled = new Oled(i2cbus, opts);
|
||||
|
||||
oled.clearDisplay();
|
||||
oled.setCursor(1, 1);
|
||||
oled.writeString(font, 1, "This is a string I am writing to the screen", 1, true);
|
||||
oled.turnOffDisplay();*/
|
||||
var OledPrinter = /** @class */ (function () {
|
||||
function OledPrinter(config) {
|
||||
this._config = config;
|
||||
// this._curMenu = config;
|
||||
var i2cbus = (0, i2c_bus_1.openSync)(1);
|
||||
var opts = {
|
||||
width: 128,
|
||||
height: 64,
|
||||
address: 0x3C
|
||||
};
|
||||
this._oled = new oled_i2c_bus_1["default"](i2cbus, opts);
|
||||
this._oled.clearDisplay();
|
||||
}
|
||||
OledPrinter.prototype.printMenu = function () {
|
||||
this._oled.writeString(oled_font_5x7_1["default"], 1, "how many lines does this display hold let's write a whole bunch of stuff with wrapping on and then count the number of lines", 1, true);
|
||||
};
|
||||
OledPrinter.prototype.getSelection = function () {
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
var _this = this;
|
||||
return __generator(this, function (_a) {
|
||||
return [2 /*return*/, new Promise(function (resolve) {
|
||||
_this.printMenu();
|
||||
while (true) {
|
||||
}
|
||||
resolve(_this._config[0]);
|
||||
})];
|
||||
});
|
||||
});
|
||||
};
|
||||
return OledPrinter;
|
||||
}());
|
||||
exports.OledPrinter = OledPrinter;
|
File diff suppressed because it is too large
Load Diff
20
package.json
20
package.json
|
@ -1,20 +0,0 @@
|
|||
{
|
||||
"name": "z3r0haxx",
|
||||
"version": "1.0.0",
|
||||
"scripts": {
|
||||
"clean": "rm -rf bin/*",
|
||||
"build": "npm run clean && tsc --project tsconfig.json",
|
||||
"start": "ts-node src/main.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"i2c-bus": "^5.2.2",
|
||||
"oled-font-5x7": "^1.0.3",
|
||||
"oled-i2c-bus": "^1.0.12"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/i2c-bus": "^5.1.0",
|
||||
"ts-node": "^10.7.0",
|
||||
"typescript": "^4.6.3",
|
||||
"typescript-language-server": "^0.9.7"
|
||||
}
|
||||
}
|
|
@ -1,4 +0,0 @@
|
|||
export class Chess {
|
||||
public async runAsync(): Promise<void> {
|
||||
}
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
import { DisplayPrinter } from './interface';
|
||||
|
||||
export class ConsolePrinter implements DisplayPrinter {
|
||||
public async displayContent(content: string): Promise<void> {
|
||||
console.log(content);
|
||||
console.log("Press a key...");
|
||||
return new Promise((resolve) => {
|
||||
const listener = (_letter: string, _key: any): void => {
|
||||
process.stdin.removeListener('keypress', listener);
|
||||
resolve();
|
||||
}
|
||||
process.stdin.on('keypress', listener);
|
||||
});
|
||||
}
|
||||
|
||||
public clear(_: boolean): void {
|
||||
console.clear();
|
||||
}
|
||||
}
|
|
@ -1,22 +0,0 @@
|
|||
import { DisplayPrinter } from './interface';
|
||||
import { ConsolePrinter } from './console';
|
||||
import { OledPrinter } from './oled';
|
||||
|
||||
export class Display {
|
||||
private _display: DisplayPrinter;
|
||||
|
||||
constructor(useConsole: boolean) {
|
||||
this._display = useConsole
|
||||
? new ConsolePrinter()
|
||||
: new OledPrinter();
|
||||
}
|
||||
|
||||
public async displayContent(content: string, wrap: boolean = true): Promise<void> {
|
||||
this._display.clear(false);
|
||||
await this._display.displayContent(content, wrap);
|
||||
}
|
||||
|
||||
public clear(turnOff: boolean = false): void {
|
||||
this._display.clear(turnOff);
|
||||
}
|
||||
}
|
|
@ -1,4 +0,0 @@
|
|||
export interface DisplayPrinter {
|
||||
displayContent(content: string, wrap: boolean): Promise<void>;
|
||||
clear(turnOff: boolean): void;
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
import { DisplayPrinter } from './interface';
|
||||
|
||||
export class OledPrinter implements DisplayPrinter {
|
||||
public async displayContent(_content: string, _wrap: boolean): Promise<void> {
|
||||
}
|
||||
|
||||
public clear(_turnOff: boolean): void {
|
||||
}
|
||||
}
|
93
src/main.ts
93
src/main.ts
|
@ -1,93 +0,0 @@
|
|||
import { Menu } from './menu/menu';
|
||||
import { MenuCommand, MenuType } from './menu/interface';
|
||||
import { Display } from './display/display';
|
||||
|
||||
import { Chess } from './chess/chess';
|
||||
|
||||
import { exec, spawn } from 'child_process';
|
||||
|
||||
class Main {
|
||||
private readonly _menuConfig = [
|
||||
{
|
||||
display: "df -h",
|
||||
type: MenuType.ExecCommand,
|
||||
command: {exe: "df", args: ['-h']},
|
||||
},
|
||||
{
|
||||
display: "Games",
|
||||
type: MenuType.SubMenu,
|
||||
subMenu: [
|
||||
{
|
||||
display: "Chess",
|
||||
type: MenuType.ExecModule,
|
||||
module: new Chess().runAsync,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
display: "Reboot",
|
||||
type: MenuType.Reboot,
|
||||
},
|
||||
{
|
||||
display: "Shutdown",
|
||||
type: MenuType.Shutdown,
|
||||
},
|
||||
];
|
||||
|
||||
private readonly _menu: Menu;
|
||||
private readonly _display: Display;
|
||||
private readonly _console: boolean = process.env['CONSOLE'] === '1';
|
||||
|
||||
constructor() {
|
||||
this._menu = new Menu(this._menuConfig, this._console);
|
||||
this._display = new Display(this._console);
|
||||
}
|
||||
|
||||
private async runCommand(cmd: MenuCommand): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
this._display.displayContent(
|
||||
`Executing command:\n> ${cmd.exe} ${cmd.args?.join(' ')}`);
|
||||
let output = '';
|
||||
const child = spawn(cmd.exe, cmd.args);
|
||||
child.stdout.on('data', (data) => {
|
||||
output += data;
|
||||
});
|
||||
child.on('exit', async ()=> {
|
||||
await this._display.displayContent(output, cmd.wrap);
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public async runAsync(): Promise<void> {
|
||||
while (true) {
|
||||
const selected = await this._menu.getSelection();
|
||||
switch(selected.type) {
|
||||
case MenuType.Shutdown:
|
||||
this._display.clear(true);
|
||||
exec('sudo shutdown now');
|
||||
process.exit();
|
||||
case MenuType.Reboot:
|
||||
this._display.clear();
|
||||
exec('sudo reboot');
|
||||
process.exit();
|
||||
case MenuType.ExecCommand:
|
||||
if (selected.command) {
|
||||
await this.runCommand(selected.command);
|
||||
}
|
||||
break;
|
||||
case MenuType.ExecModule:
|
||||
if (selected.module) {
|
||||
await selected.module();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function run() {
|
||||
await new Main().runAsync();
|
||||
}
|
||||
|
||||
run();
|
|
@ -1,82 +0,0 @@
|
|||
import { MenuConfig, MenuPrinter, MenuType } from './interface';
|
||||
import * as readline from 'readline';
|
||||
|
||||
type Key = {name: string, ctrl: boolean};
|
||||
|
||||
export class ConsolePrinter implements MenuPrinter {
|
||||
private _path: number[] = [];
|
||||
private _selectedIndex: number = 0;
|
||||
private _config: MenuConfig[];
|
||||
private _curMenu: MenuConfig[];
|
||||
|
||||
constructor(config: MenuConfig[]) {
|
||||
this._config = config;
|
||||
this._curMenu = 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(): void {
|
||||
console.clear();
|
||||
for (let i = 0; i < this._curMenu.length; i++) {
|
||||
const line = (i === this._selectedIndex
|
||||
? "> "
|
||||
: " ") + this._curMenu[i].display;
|
||||
console.log(line);
|
||||
}
|
||||
}
|
||||
|
||||
public async getSelection(): Promise<MenuConfig> {
|
||||
return new Promise(async (resolve) => {
|
||||
while (true) {
|
||||
// print menu
|
||||
this.printMenu();
|
||||
|
||||
// get user input
|
||||
const key = await this.keypress();
|
||||
|
||||
if (key.ctrl && key.name === 'c') {
|
||||
process.exit();
|
||||
} else if (key.name === 'up') {
|
||||
this._selectedIndex--;
|
||||
if (this._selectedIndex < 0) {
|
||||
this._selectedIndex = 0;
|
||||
}
|
||||
} else if (key.name ==='down') {
|
||||
this._selectedIndex++
|
||||
if (this._selectedIndex > this._curMenu.length - 1) {
|
||||
this._selectedIndex = this._curMenu.length - 1;
|
||||
}
|
||||
} else if (key.name === 'left') {
|
||||
if (this._path.length > 0) {
|
||||
this._path.pop();
|
||||
this._selectedIndex = 0;
|
||||
}
|
||||
} else if (key.name === 'right' || key.name === 'return') {
|
||||
this._path.push(this._selectedIndex);
|
||||
}
|
||||
this._curMenu = this._config;
|
||||
for (let i = 0; i < this._path.length; i++) {
|
||||
if (this._curMenu[this._path[i]].type === MenuType.SubMenu) {
|
||||
this._curMenu = this._curMenu[this._path[i]].subMenu!;
|
||||
this._selectedIndex = 0;
|
||||
} else {
|
||||
resolve(this._curMenu[this._path[i]]);
|
||||
this._path.pop();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
export enum MenuType {
|
||||
ExecCommand,
|
||||
ExecModule,
|
||||
Reboot,
|
||||
Shutdown,
|
||||
SubMenu,
|
||||
}
|
||||
|
||||
export interface MenuCommand {
|
||||
exe: string,
|
||||
args?: string[],
|
||||
wrap?: boolean,
|
||||
}
|
||||
|
||||
export interface MenuConfig {
|
||||
display: string,
|
||||
type: MenuType,
|
||||
command?: MenuCommand,
|
||||
subMenu?: MenuConfig[],
|
||||
module?: () => Promise<void>,
|
||||
}
|
||||
|
||||
export interface MenuPrinter {
|
||||
getSelection(): Promise<MenuConfig>;
|
||||
}
|
|
@ -1,17 +0,0 @@
|
|||
import { MenuConfig, MenuPrinter } from './interface';
|
||||
import { ConsolePrinter } from './console';
|
||||
import { OledPrinter } from './oled';
|
||||
|
||||
export class Menu {
|
||||
private _printer: MenuPrinter;
|
||||
|
||||
constructor(config: MenuConfig[], isConsole: boolean = false) {
|
||||
this._printer = isConsole
|
||||
? new ConsolePrinter(config)
|
||||
: new OledPrinter(config);
|
||||
}
|
||||
|
||||
public async getSelection(): Promise<MenuConfig> {
|
||||
return this._printer.getSelection();
|
||||
}
|
||||
}
|
|
@ -1,52 +0,0 @@
|
|||
import { openSync } from 'i2c-bus';
|
||||
import Oled from 'oled-i2c-bus';
|
||||
import font from 'oled-font-5x7';
|
||||
import { MenuConfig, MenuPrinter } from './interface';
|
||||
|
||||
/*const i2cbus = openSync(1);
|
||||
const opts = {
|
||||
width: 128,
|
||||
height: 64,
|
||||
address: 0x3C,
|
||||
};
|
||||
|
||||
const oled = new Oled(i2cbus, opts);
|
||||
|
||||
oled.clearDisplay();
|
||||
oled.setCursor(1, 1);
|
||||
oled.writeString(font, 1, "This is a string I am writing to the screen", 1, true);
|
||||
oled.turnOffDisplay();*/
|
||||
|
||||
export class OledPrinter implements MenuPrinter {
|
||||
// private _path: number[] = [];
|
||||
// private _selectedIndex: number = 0;
|
||||
private _config: MenuConfig[];
|
||||
// private _curMenu: MenuConfig[];
|
||||
private _oled: Oled;
|
||||
|
||||
constructor(config: MenuConfig[]) {
|
||||
this._config = config;
|
||||
// this._curMenu = config;
|
||||
const i2cbus = openSync(1);
|
||||
const opts = {
|
||||
width: 128,
|
||||
height: 64,
|
||||
address: 0x3C,
|
||||
};
|
||||
this._oled = new Oled(i2cbus, opts);
|
||||
this._oled.clearDisplay();
|
||||
}
|
||||
|
||||
private printMenu(): void {
|
||||
this._oled.writeString(font, 1, "how many lines does this display hold let's write a whole bunch of stuff with wrapping on and then count the number of lines", 1, true);
|
||||
}
|
||||
|
||||
public async getSelection(): Promise<MenuConfig> {
|
||||
return new Promise((resolve) => {
|
||||
this.printMenu();
|
||||
while (true) {
|
||||
}
|
||||
resolve(this._config[0]);
|
||||
});
|
||||
}
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"allowJs": false,
|
||||
"checkJs": false,
|
||||
"strict": true,
|
||||
"noImplicitAny": true,
|
||||
"strictNullChecks": true,
|
||||
"strictFunctionTypes": true,
|
||||
"noImplicitThis": true,
|
||||
"alwaysStrict": true,
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"noImplicitReturns": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"esModuleInterop": true,
|
||||
"paths": {
|
||||
"*": ["./@types/*"]
|
||||
},
|
||||
"typeRoots": [
|
||||
"./node_modules/@types",
|
||||
"./@types"
|
||||
],
|
||||
"outDir": "./bin"
|
||||
},
|
||||
"include": ["src/**/*"]
|
||||
}
|
Reference in New Issue