/* * Copyright (c) 2021-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. * */ import { Box, render, Text, useApp } from "ink" import React, { FC } from "react" import { _also } from "../../lang-utils/kotlin-lang-utils" import { ConfirmInput } from "../../tui-components" import { inkCLIAppMainFn, StateHolder, useStateSafely } from "../../tui-core" import { createNewShortcutToActionMap, ShortcutToActionMap, UseKeyboardReturnValue, useKeyboardWithMapCached, UseKeyboardWrapper } from "../../tui-node-keyboard" // Types & data classes. export type HookOutput = { answer: StateHolder, useKeyboard: UseKeyboardReturnValue, greeting: string } export type HookInput = { name: string } type InternalProps = { ctx: HookOutput } // Hooks. const runHooks = (input: HookInput): HookOutput => { const { name } = input const app = useApp() const [ answer, setAnswer ] = useStateSafely("").asArray() const createShortcuts = (): ShortcutToActionMap => { return _also( createNewShortcutToActionMap(), map => map .set("q", app.exit) .set("ctrl+q", app.exit) ) } return { greeting: `Hello ${name}`, answer: new StateHolder(answer, setAnswer), useKeyboard: useKeyboardWithMapCached(createShortcuts) } } // Function components. export const App: FC = () => { const ctx: HookOutput = runHooks({ name: "" }) const [ text ] = ctx.answer.asArray() return ( {text ? {text} : undefined} ) } const Row_Debug: FC = ({ ctx }) => { const { keyPress: kp, inRawMode: mode } = ctx.useKeyboard return mode ? keyPress: {kp.isSome() ? `${kp.value.toString()}` : "n/a"} : keyb disabled } const UnicornQuestion: FC = ({ ctx }) => { const [ _, setText ] = ctx.answer.asArray() const onSubmit = (answer: boolean) => { setText(answer ? "You like rainbows 🌈" : "You don't like rainbows 😥") } return ( Do you like rainbows? (Y/n) ) } // Main. inkCLIAppMainFn( () => render(), "Exiting ink", "Problem w/ exiting ink" ).catch(console.error)