r3bl-ts-utils
Version:
The `r3bl-ts-utils` package is a set of useful TypeScript functions and classes that can be used in Node.js and browser environments. They are inspired by Kotlin stdlib, and Rust to write code as expressions rather than statements, colorized text, powerfu
101 lines • 4.87 kB
JavaScript
/*
* Copyright (c) 2022 R3BL LLC. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.keyboard_debug_ui = void 0;
const ink_1 = require("ink");
const react_1 = __importStar(require("react"));
const index_1 = require("../../index");
// Unique namespace ensures there will be no unintended collisions w/ other symbols of the codebase.
var keyboard_debug_ui;
(function (keyboard_debug_ui) {
const runHooks = (name) => {
(0, index_1.usePreventProcessExitDuringTesting)(); // For testing using `npm run start-dev-watch`.
const ttySize = (0, index_1.useTTYSize)();
const { localeTimeString: formattedTime } = (0, index_1.useClockWithLocalTimeFormat)(3_000);
const { keyPress, inRawMode } = (0, index_1.useKeyboardBuilder)({
type: name,
args: { type: "map-cached", createShortcutsFn, options: { isActive: true } }
});
return {
name,
ttySize,
formattedTime,
inRawMode,
keyPress
};
};
//#endregion
//#region handleKeyboard.
const createShortcutsFn = () => (0, index_1._also)((0, index_1.createNewShortcutToActionMap)(), map => map
.set("q", index_1.LifecycleHelper.fireExit)
.set("ctrl+q", index_1.LifecycleHelper.fireExit)
.set("escape", index_1.LifecycleHelper.fireExit));
//#endregion
//#region Function component.
const App = ({ arg }) => {
const { keyPress, name, inRawMode, ttySize, formattedTime } = runHooks(arg);
return (react_1.default.createElement(ink_1.Box, { flexDirection: "column" },
react_1.default.createElement(ink_1.Text, null,
"Hello ",
react_1.default.createElement(ink_1.Text, { color: "yellow" }, name)),
react_1.default.createElement(ink_1.Text, null, inRawMode ?
react_1.default.createElement(ink_1.Text, { color: "green" }, "inRawMode") :
react_1.default.createElement(ink_1.Text, { color: "red" }, "!inRawMode")),
react_1.default.createElement(ink_1.Text, null,
"ttySize ",
react_1.default.createElement(ink_1.Text, { color: "blue" }, ttySize.toString())),
react_1.default.createElement(ink_1.Text, null,
"time ",
react_1.default.createElement(ink_1.Text, { color: "magenta" }, formattedTime)),
react_1.default.createElement(ink_1.Text, null, keyPress.isSome() ?
react_1.default.createElement(ink_1.Text, { color: "cyan" }, keyPress.value.toString()) :
react_1.default.createElement(ink_1.Text, { color: "red" }, "!keyPress")),
react_1.default.createElement(UserInput, null)));
};
const UserInput = () => {
const [input, setInput] = (0, react_1.useState)("");
(0, ink_1.useInput)((input, _) => setInput(input));
return react_1.default.createElement(ink_1.Text, { color: "gray" },
"useInput: ",
input);
};
keyboard_debug_ui.main = (arg) => {
(0, index_1.inkCLIAppMainFn)(() => (0, ink_1.render)(react_1.default.createElement(App, { arg: arg })), "Exiting ink", "Problem w/ exiting ink").catch(console.error);
};
//#endregion
})(keyboard_debug_ui = exports.keyboard_debug_ui || (exports.keyboard_debug_ui = {}));
//# sourceMappingURL=debug-ui.js.map
;