Skip to content

Commit

Permalink
chore: Full-screen LayerZero experience
Browse files Browse the repository at this point in the history
  • Loading branch information
janjakubnanista committed Nov 14, 2023
1 parent 8061250 commit ed1f5f8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
10 changes: 8 additions & 2 deletions packages/create-lz-oapp/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(<Placeholder />)
const exitAltScreen = await altScreen()

await waitUntilExit()
try {
const { waitUntilExit } = render(<Placeholder />)
await waitUntilExit()
} finally {
await exitAltScreen()
}
})
.parseAsync()
33 changes: 33 additions & 0 deletions packages/create-lz-oapp/src/utilities/terminal.ts
Original file line number Diff line number Diff line change
@@ -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<void>`
*/
const createWrite = (socket: NodeJS.WriteStream) => (content: string) => {
return new Promise<void>((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)
}

0 comments on commit ed1f5f8

Please sign in to comment.