From 53c1a3a772e7cf2a9ef5f776731a45dae7834e3a Mon Sep 17 00:00:00 2001 From: zyoshoka <107108195+zyoshoka@users.noreply.github.com> Date: Fri, 10 Nov 2023 01:21:15 +0900 Subject: [PATCH] feat: ask for desired shell if no args (#43) --- src/commands/root.ts | 3 ++- src/ui/ui-init.tsx | 64 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 src/ui/ui-init.tsx diff --git a/src/commands/root.ts b/src/commands/root.ts index 6e9f6ff..be19a4f 100644 --- a/src/commands/root.ts +++ b/src/commands/root.ts @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +import { initRender } from "../ui/ui-init.js"; import { render } from "../ui/ui-root.js"; import { executeShellCommandTTY, ExecuteShellCommandTTYResult } from "../runtime/utils.js"; import { saveCommand, loadCommand } from "../utils/cache.js"; @@ -21,7 +22,7 @@ export const action = async (options: RootCommandOptions) => { process.exit(0); } - const shell = options.shell ?? ""; + const shell = options.shell ?? (await initRender()) ?? ""; if (!shells.map((s) => s.valueOf()).includes(shell)) { console.error(`Unsupported shell: '${shell}', supported shells: ${supportedShells}`); process.exit(1); diff --git a/src/ui/ui-init.tsx b/src/ui/ui-init.tsx new file mode 100644 index 0000000..0c60fa8 --- /dev/null +++ b/src/ui/ui-init.tsx @@ -0,0 +1,64 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +import React, { useState } from "react"; +import { Box, Text, render, useApp, useInput } from "ink"; + +import { supportedShells } from "../utils/bindings.js"; + +let uiResult = undefined; + +function UI() { + const { exit } = useApp(); + const [selectionIdx, setSelectionIdx] = useState(0); + const [exited, setExited] = useState(false); + + useInput(async (_, key) => { + if (key.upArrow) { + setSelectionIdx(Math.max(0, selectionIdx - 1)); + } else if (key.downArrow) { + setSelectionIdx(Math.min(supportedShells.length - 1, selectionIdx + 1)); + } else if (key.return) { + uiResult = supportedShells[selectionIdx]; + setExited(true); + setTimeout(exit, 0); + } + }); + + return ( + <> + {exited ? null : ( + + + Select your desired shell + + + {supportedShells.map((shell, idx) => { + if (idx == selectionIdx) { + return ( + + {">"} {shell} + + ); + } + return ( + + {" "} + {shell} + + ); + })} + + + )} + + ); +} + +export const initRender = async (): Promise => { + uiResult = undefined; + const { waitUntilExit } = render(); + await waitUntilExit(); + + return uiResult; +};