Security News
The Unpaid Backbone of Open Source: Solo Maintainers Face Increasing Security Demands
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Ink is a React-based framework for building command-line interface (CLI) applications. It allows developers to use React components to create interactive and dynamic CLI tools.
Rendering Text
Ink allows you to render text in the terminal using React components. The example demonstrates a simple 'Hello, world!' text rendering.
const { render, Text } = require('ink');
const App = () => <Text>Hello, world!</Text>;
render(<App />);
Handling User Input
Ink provides hooks like `useInput` to handle user input. This example shows how to exit the application when the user presses 'q'.
const { render, Text, useInput } = require('ink');
const App = () => {
useInput((input, key) => {
if (input === 'q') {
process.exit();
}
});
return <Text>Press 'q' to exit.</Text>;
};
render(<App />);
Using Components
Ink supports layout components like `Box` to arrange other components. This example demonstrates a vertical layout with two text components.
const { render, Box, Text } = require('ink');
const App = () => (
<Box flexDirection="column">
<Text>Hello</Text>
<Text>World</Text>
</Box>
);
render(<App />);
Styling Text
Ink allows you to style text using properties like `color`. This example shows how to render green-colored text.
const { render, Text } = require('ink');
const App = () => (
<Text color="green">This is green text</Text>
);
render(<App />);
Blessed is a library for creating interactive command-line applications. It provides a wide range of widgets and supports mouse and keyboard input. Compared to Ink, Blessed is more low-level and imperative, whereas Ink leverages React's declarative approach.
Ink-select-input is a component for Ink that allows you to create interactive select inputs. It is specifically designed to work with Ink, providing a higher-level abstraction for creating selection menus. Unlike Ink, which is a full framework, ink-select-input is a specialized component.
Vorpal is a framework for building interactive CLI applications. It provides a command-line interface with built-in help, tab completion, and more. Vorpal is more focused on creating command-based interfaces, whereas Ink is more flexible and component-based.
React for CLIs. Build and test your CLI output using components.
$ npm install ink@next
import React, {Component} from 'react';
import {render, Color} from 'ink';
class Counter extends Component {
constructor() {
super();
this.state = {
i: 0
};
}
render() {
return (
<Color green>
{this.state.i} tests passed
</Color>
);
}
componentDidMount() {
this.timer = setInterval(() => {
this.setState({
i: this.state.i + 1
});
}, 100);
}
componentWillUnmount() {
clearInterval(this.timer);
}
}
render(<Counter/>);
console[method]
calls in a scrollable panel.Ink's goal is to provide the same component-based UI building experience that React provides, but for command-line apps. It uses yoga-layout to allow Flexbox layouts in the terminal. If you are already familiar with React, you already know Ink.
The key difference you have to remember is that the rendering result isn't a DOM, but a string, which Ink writes to the output.
To ensure all examples work and you can begin your adventure with Ink, make sure to set up Babel with a React preset. After installing Babel, configure it in package.json
:
{
"babel": {
"presets": [
"@babel/preset-react"
]
}
}
Don't forget to import React
into every file that contains JSX:
import React from 'react';
import {Box} from 'ink';
const Demo = () => <Box/>;
To get started, quickly scaffold out a project using Ink CLI Yeoman generator. To create a new component that you intend to publish, you can use Ink Component generator.
Since Ink is a React renderer, it means that all of React is supported. Head over to React website for documentation on how to use it. In this readme only Ink's methods will be documented.
Mount a component and render the output.
Type: ReactElement
Type: Stream
Default: process.stdout
Output stream where app will be rendered.
Type: Stream
Default: process.stdin
Input stream where app will listen for input.
Type: Boolean
Default: false
If true
, each update will be rendered as a separate output, without replacing the previous one.
import React, {Component} from 'react';
import {render, Box} from 'ink';
class Counter extends Component {
constructor() {
super();
this.state = {
i: 0
};
}
render() {
return (
<Box>
Iteration #${this.state.i}
</Box>
);
}
componentDidMount() {
this.timer = setInterval(() => {
this.setState({
i: this.state.i + 1
});
}, 100);
}
componentWillUnmount() {
clearInterval(this.timer);
}
}
const unmount = render(<Counter/>);
setTimeout(() => {
// Enough counting
unmount();
}, 1000);
There's also a shortcut to avoid passing options
object:
render(<Counter>, process.stdout);
Ink uses Yoga - a Flexbox layout engine to build great user interfaces for your CLIs.
It's important to remember that each element is a Flexbox container.
Think of it as if each <div>
in the browser had display: flex
.
See <Box>
built-in component below for documentation on how to use Flexbox layouts in Ink.
<Box>
it's an essential Ink component to build your layout. It's like a <div>
in a browser.
Import:
import {Box} from 'ink';
Type: number
Default: 0
Type: number
Default: 0
Type: number
Default: 0
Type: number
Default: 0
Type: number
Default: 0
Type: number
Default: 0
Type: number
Default: 0
<Box paddingTop={2}>Top</Box>
<Box paddingBottom={2}>Bottom</Box>
<Box paddingLeft={2}>Left</Box>
<Box paddingRight={2}>Right</Box>
<Box paddingX={2}>Left and right</Box>
<Box paddingY={2}>Top and bottom</Box>
<Box padding={2}>Top, bottom, left and right</Box>
Type: number
Default: 0
Type: number
Default: 0
Type: number
Default: 0
Type: number
Default: 0
Type: number
Default: 0
Type: number
Default: 0
Type: number
Default: 0
<Box marginTop={2}>Top</Box>
<Box marginBottom={2}>Bottom</Box>
<Box marginLeft={2}>Left</Box>
<Box marginRight={2}>Right</Box>
<Box marginX={2}>Left and right</Box>
<Box marginY={2}>Top and bottom</Box>
<Box margin={2}>Top, bottom, left and right</Box>
Type: number
See flex-grow.
<Box>
Label:
<Box flexGrow={1}>
Fills all remaining space
</Box>
</Box>
Type: number
See flex-shrink.
<Box width={20}>
<Box flexShrink={2} width={10}>
Will be 1/4
</Box>
<Box width={10}>
Will be 3/4
</Box>
</Box>
Type: string
Allowed values: row
, row-reverse
, column
and column-reverse
See flex-direction.
<Box>
<Box marginRight={1}>X</Box>
<Box>Y</Box>
</Box>
// X Y
<Box flexDirection="row-reverse">
<Box>X</Box>
<Box marginRight={1}>Y</Box>
</Box>
// Y X
<Box flexDirection="column">
<Box>X</Box>
<Box>Y</Box>
</Box>
// X
// Y
<Box flexDirection="column-reverse">
<Box>X</Box>
<Box>Y</Box>
</Box>
// Y
// X
Type: string
Allowed values: flex-start
, center
and flex-end
See align-items.
<Box alignItems="flex-start">
<Box marginRight={1}>X</Box>
<Box>{`A\nB\nC`}</Box>
</Box>
// X A
// B
// C
<Box alignItems="center">
<Box marginRight={1}>X</Box>
<Box>{`A\nB\nC`}</Box>
</Box>
// A
// X B
// C
<Box alignItems="flex-end">
<Box marginRight={1}>X</Box>
<Box>{`A\nB\nC`}</Box>
</Box>
// A
// B
// X C
Type: string
Allowed values: flex-start
, center
, flex-end
, space-between
and space-around
.
See justify-content.
<Box justifyContent="flex-start">
<Box>X</Box>
</Box>
// [X ]
<Box justifyContent="center">
<Box>X</Box>
</Box>
// [ X ]
<Box justifyContent="flex-end">
<Box>X</Box>
</Box>
// [ X]
<Box justifyContent="space-between">
<Box>X</Box>
<Box>Y</Box>
</Box>
// [X Y]
<Box justifyContent="space-around">
<Box>X</Box>
<Box>Y</Box>
</Box>
// [ X Y ]
The <Color>
compoment is a simple wrapper around the chalk
API.
It supports all of the chalk's methods as props
.
Import:
import {Color} from 'ink';
Usage:
<Color rgb={[255, 255, 255]} bgKeyword="magenta">
Hello!
</Color>
<Color hex="#000000" bgHex="#FFFFFF">
Hey there
</Color>
<Color blue>
I'm blue
</Color>
This component can change the style of the text, make it bold, underline, italic or strikethrough.
Import:
import {Text} from 'ink';
Type: boolean
Default: false
Type: boolean
Default: false
Type: boolean
Default: false
Type: boolean
Default: false
Usage:
<Text bold>I am bold</Text>
<Text italic>I am italic</Text>
<Text underline>I am underline</Text>
<Text strikethrough>I am strikethrough</Text>
<StdinContext>
is a React context, which exposes several props.
Import:
import {StdinContext} from 'ink';
Type: Stream
Default: process.stdin
Stdin stream passed to render()
in options.stdin
or process.stdin
by default.
Useful if your app needs to handle user input.
Usage:
<StdinContext.Consumer>
{({ stdin }) => (
<MyComponent stdin={stdin}/>
)}
</StdinContext.Consumer>
Type: function
See setRawMode.
Ink exposes this function via own <StdinContext>
to be able to handle Ctrl+C, that's why you should use Ink's setRawMode
instead of process.stdin.setRawMode
.
Usage:
<StdinContext.Consumer>
{({ setRawMode }) => (
<MyComponent setRawMode={setRawMode}/>
)}
</StdinContext.Consumer>
<StdoutContext>
is a React context, which exposes stdout stream, where Ink renders your app.
Import:
import {StdoutContext} from 'ink';
Type: Stream
Default: process.stdout
Usage:
<StdoutContext.Consumer>
{({ stdout }) => (
<MyComponent stdout={stdout}/>
)}
</StdoutContext.Consumer>
MIT © Vadim Demedes
FAQs
React for CLI
The npm package ink receives a total of 748,900 weekly downloads. As such, ink popularity was classified as popular.
We found that ink demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.
Security News
A developer is accusing Tencent of violating the GPL by modifying a Python utility and changing its license to BSD, highlighting the importance of copyleft compliance.