diff --git a/packages/create-lz-oapp/src/index.tsx b/packages/create-lz-oapp/src/index.tsx index bac6160cc..ed5493866 100644 --- a/packages/create-lz-oapp/src/index.tsx +++ b/packages/create-lz-oapp/src/index.tsx @@ -2,12 +2,18 @@ import React from "react" import { render } from "ink" import { Command } from "commander" import { Placeholder } from "./components/placeholder.js" +import { altScreen } from "./utilities/terminal.js" new Command("create-lz-oapp") .description("Create LayerZero OApp with one command") .action(async () => { - const { waitUntilExit } = render() + const exitAltScreen = await altScreen() - await waitUntilExit() + try { + const { waitUntilExit } = render() + await waitUntilExit() + } finally { + await exitAltScreen() + } }) .parseAsync() diff --git a/packages/create-lz-oapp/src/utilities/terminal.ts b/packages/create-lz-oapp/src/utilities/terminal.ts new file mode 100644 index 000000000..c5c42c2df --- /dev/null +++ b/packages/create-lz-oapp/src/utilities/terminal.ts @@ -0,0 +1,33 @@ +const ENTER_ALT_SCREEN_ANSI = "\x1b[?1049h" +const EXIT_ALT_SCREEN_ANSI = "\x1b[?1049l" + +/** + * Helper function that wraps socket writes with a promise + * + * @param socket `WriteStream` + * @returns `(content: string) => Promise` + */ +const createWrite = (socket: NodeJS.WriteStream) => (content: string) => { + return new Promise((resolve, reject) => { + socket.write(content, (error) => { + if (error != null) reject(error) + else resolve() + }) + }) +} + +/** + * Starts an alt screen and returns a callback that exits back to the default screen. + * This makes the app "full screen" + * + * See https://github.com/vadimdemedes/ink/issues/263 for more info + * + * @returns `Promise<() => void>` + */ +export const altScreen = async () => { + const write = createWrite(process.stdout) + + await write(ENTER_ALT_SCREEN_ANSI) + + return () => write(EXIT_ALT_SCREEN_ANSI) +}