BT

InfoQ Homepage News Interactive Terminal Apps with Ink 3 - New Built-In Hooks, Suspense and React Dev Tool Support

Interactive Terminal Apps with Ink 3 - New Built-In Hooks, Suspense and React Dev Tool Support

Bookmarks

Ink, the React renderer for interactive command-line apps, recently announced version 3 with new built-in hooks which enable better focus and stream management (e.g. stdin) in the terminal. Developers can leverage the React development tools to inspect the output of an Ink app. Ink 3 also features updated components that allow for better styling of CLIs, built-in error handler and log interception, and other performance and stability improvements.

Ink 3 offers 7 new hooks to developers of interactive command-line apps. useInput handles user input with a callback that is called for each character when a user enters any input. useApp exposes a method to manually exit the app (unmount). useStdin, useStdout and useStderr give direct access to stdin, stdout and stderr streams. Components that use the useFocus hook have their focus handled by Ink, so when a user presses Tab, Ink will switch focus to this component. If there are multiple components that execute useFocus hook, the focus will be given to them in the order that these components are rendered in. useFocusManager exposes methods to enable or disable focus management for all components or manually switch focus to next or previous components.

Ink supports React Devtools out-of-the-box. Developers can inspect the output of their Ink app, manually change the props of any component, and see the CLI update on the fly without having to restart it.

dev tool integration with Ink screenshot
(Source: release note)

Ink 3 merged all functionality from the <Color> component into the <Text> component, resulting in simplified color declaration for text content. Thus, the following code:

<Color red>
  <Text bold>Hello World</Text>
</Color>

is replaced in Ink 3 by:

<Text bold color="red">
 Hello World
</Text>

Developers can use HEX or RGB colors (if the target terminal supports it):

<Text  color="green">Green</Text>
<Text  color="#005cc5">Blue</Text>
<Text  color="rgb(232, 131, 136)">Red</Text>

Developers can also set a colorful background with thebackgroundColor property:

<Text  color="black"  backgroundColor="green">
 Black on Green
</Text>

Boxes in Ink 3 can now have seven different border styles:

Ink 3's 7 border styles

Ink 3 also intercepts React’s error messages by means of a global React error boundary. Ink 3 pretty-prints the intercepted error messages and stack trace, keeping only the relevant information. Ink 3 also intercepts calls to console methods (e.g. console.error) and ensures that logs are displayed correctly above the terminal app’s UI and do not interfere with each other.

Ink 3 additionally ships with performance and stability improvements. Ink now rerenders when the terminal is resized, refactored the codebase to TypeScript, and supports React Suspense. Ink 3 fixes rendering bugs and inconsistencies in FlexBox support, incorporating the feedback received from its users – e.g. Gatsby, Terraform, tap, Prisma, Shopify, New York Times and many others.

Ink 3’s full release note can be accessed online. Ink.js is available under the MIT open source license. Contributions are welcome via the GitHub package.

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.