Skip to content
/ ink Public
forked from vadimdemedes/ink

🌈 React for interactive command-line apps

License

Notifications You must be signed in to change notification settings

joskid/ink

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation



Ink


React for CLIs. Build and test your CLI output using components.

Build Status

Install

$ npm install ink react

Usage

import React, {useState, useEffect} from 'react';
import {render, Color} from 'ink';

const Counter = () => {
	const [counter, setCounter] = useState(0);

	useEffect(() => {
		const timer = setInterval(() => {
			setCounter(previousCounter => previousCounter + 1);
		}, 100);

		return () => {
			clearInterval(timer);
		};
	}, []);

	return <Color green>{counter} tests passed</Color>;
};

render(<Counter />);

You can also check it out live on repl.it sandbox. Feel free to play around with the code and fork this repl at https://repl.it/@vadimdemedes/ink-counter-demo.

Who's Using Ink?

  • Gatsby - Gatsby is a modern web framework for blazing fast websites.
  • Parcel - Blazing fast, zero configuration web application bundler.
  • tap - A Test-Anything-Protocol library for JavaScript.
  • Typewriter - Generates strongly-typed Segment analytics clients from arbitrary JSON Schema.
  • Prisma - The unified data layer for modern applications.
  • Wallace - Pretty CSS analytics on the CLI.
  • tink - Next-generation runtime and package manager.
  • Splash - Observe the splash zone of a change across the Shopify's Polaris component library.
  • emoj - Find relevant emoji on the command-line.
  • emma - Terminal assistant to find and install npm packages.
  • sindresorhus - The Sindre Sorhus CLI.
  • swiff - Multi-environment command line tools for time-saving web developers.
  • share - Quickly share files from your command line.
  • Kubelive - CLI for Kubernetes to provide live data about the cluster and its resources.
  • changelog-view - Tool view changelog in console.
  • gomoku-terminal - Play online Gomoku in the terminal.
  • cfpush - An interactive Cloud Foundry tutorial in your terminal.
  • startd - Turn your React component into a web app from the command-line.
  • wiki-cli - Search Wikipedia and read summaries directly in your terminal.
  • garson - Build interactive config-based command-line interfaces.

Contents

Getting Started

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 get started with Ink quickly, use create-ink-app to quickly scaffold a new Ink-based CLI. Alternatively, here's how to configure Babel to work with Ink. To ensure all examples work and you can begin your adventure, make sure to set up Babel with a React preset. After installing Babel, configure it in package.json:

{
	"babel": {
		"presets": [
			"@babel/preset-react",
			[
				"@babel/preset-env",
				{
					"targets": {
						"node": true
					}
				}
			]
		]
	}
}

Don't forget to import React into every file that contains JSX:

import React from 'react';
import {render, Box} from 'ink';

const Demo = () => <Box>Hello World</Box>;

render(<Demo />);

Examples

API

Since Ink is a React renderer, it means that all features of React are supported. Head over to React website for documentation on how to use it. In this readme only Ink's methods will be documented.

render(tree, options)

Returns: Instance

Mount a component and render the output.

tree

Type: ReactElement

options

Type: Object

stdout

Type: stream.Writable
Default: process.stdout

Output stream where app will be rendered.

stdin

Type: stream.Readable
Default: process.stdin

Input stream where app will listen for input.

exitOnCtrlC

Type: boolean
Default: true

Configure whether Ink should listen to Ctrl+C keyboard input and exit the app. This is needed in case process.stdin is in raw mode, because then Ctrl+C is ignored by default and process is expected to handle it manually.

debug

Type: boolean
Default: false

If true, each update will be rendered as a separate output, without replacing the previous one.

There's also a shortcut to avoid passing options object:

render(<Counter>, process.stdout);

Instance

This is the object that render() returns.

rerender

Replace previous root node with a new one or update props of the current root node.

// Update props of the root node
const {rerender} = render(<Counter count={1} />);
rerender(<Counter count={2} />);

// Replace root node
const {rerender} = render(<OldCounter />);
rerender(<NewCounter />);
unmount

Manually unmount the whole Ink app.

const {unmount} = render(<MyApp />);
unmount();
waitUntilExit

Returns a promise, which resolves when app is unmounted.

const {unmount, waitUntilExit} = render(<MyApp />);

setTimeout(unmount, 1000);

await waitUntilExit(); // resolves after `unmount()` is called
clear

Clear output.

const {clear} = render(<MyApp />);
clear();

Building Layouts

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.

Built-in Components

<Box>

<Box> it's an essential Ink component to build your layout. It's like a <div style="display: flex"> in a browser.

Import:

import {Box} from 'ink';
Dimensions
width

Type: number, string

Width of the element in spaces. You can also set it in percent, which will calculate the width based on the width of parent element.

<Box width={4}>X</Box> //=> 'X   '
<Box width={10}>
	<Box width="50%">X</Box>Y
</Box> //=> 'X    Y'
height

Type: number, string

Height of the element in lines (rows). You can also set it in percent, which will calculate the height based on the height of parent element.

<Box height={4}>X</Box> //=> 'X\n\n\n'
<Box height={6} flexDirection="column">
	<Box height="50%">X</Box>Y
</Box> //=> 'X\n\n\nY\n\n'
minWidth

Type: number

Sets a minimum width of the element. Percentages aren't supported yet, see facebook/yoga#872.

minHeight

Type: number

Sets a minimum height of the element. Percentages aren't supported yet, see facebook/yoga#872.

Wrapping
textWrap

Type: string
Values: wrap truncate truncate-start truncate-middle truncate-end
Default: wrap

This property tells Ink to wrap or truncate text content of <Box> if its width is larger than container. If wrap is passed (by default), Ink will wrap text and split it into multiple lines. If truncate-* is passed, Ink will truncate text instead, which will result in one line of text with the rest cut off.

<Box>Hello World</Box>
//=> 'Hello\nWorld'

// `truncate` is an alias to `truncate-end`
<Box textWrap="truncate">Hello World</Box>
//=> 'Hello…'

<Box textWrap="truncate-middle">Hello World</Box>
//=> 'He…ld'

<Box textWrap="truncate-start">Hello World</Box>
//=> '…World'
Padding
paddingTop

Type: number
Default: 0

paddingBottom

Type: number
Default: 0

paddingLeft

Type: number
Default: 0

paddingRight

Type: number
Default: 0

paddingX

Type: number
Default: 0

paddingY

Type: number
Default: 0

padding

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>
Margin
marginTop

Type: number
Default: 0

marginBottom

Type: number
Default: 0

marginLeft

Type: number
Default: 0

marginRight

Type: number
Default: 0

marginX

Type: number
Default: 0

marginY

Type: number
Default: 0

margin

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>
Flex
flexGrow

Type: number
Default: 0

See flex-grow.

<Box>
	Label:
	<Box flexGrow={1}>Fills all remaining space</Box>
</Box>
flexShrink

Type: number
Default: 1

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>
flexBasis

Type: number, string

See flex-basis.

<Box width={6}>
	<Box flexBasis={3}>X</Box>Y
</Box> //=> 'X  Y'
<Box width={6}>
	<Box flexBasis="50%">X</Box>Y
</Box> //=> 'X  Y'
flexDirection

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
alignItems

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
justifyContent

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  ]
Visibility
display

Type: string
Allowed values: flex and none
Default: flex

<Color>

The <Color> component 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>

<Text>

This component can change the style of the text, make it bold, underline, italic or strikethrough.

Import:

import {Text} from 'ink';
bold

Type: boolean
Default: false

italic

Type: boolean
Default: false

underline

Type: boolean
Default: false

strikethrough

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>

<Newline>

Adds a newline (\n) character.

count

Type: number
Default: 1

Number of newlines to insert.

Usage:

import {Box, Color, Newline} from 'ink';

const Example = () => (
	<Box>
		<Color green>Hello</Color>
		<Newline />
		<Color red>World</Color>
	</Box>
);

This will output:

Hello
World

<Spacer>

A flexible space that expands along the major axis of its containing layout. It's useful as a shortcut for filling all the available spaces between elements.

For example, using <Spacer> in a <Box> with default flex direction (row) will position "Left" on the left side and will push "Right" to the right side.

import {Box, Spacer} from 'ink';

const Example = () => (
	<Box>
		Left
		<Spacer />
		Right
	</Box>
);

In a vertical flex direction (column), it will position "Top" to the top of the container and push "Bottom" to the bottom of it. Note, that container needs to be tall to enough to see this in effect.

import {Box, Spacer} from 'ink';

const Example = () => (
	<Box flexDirection="column" height={10}>
		Top
		<Spacer />
		Bottom
	</Box>
);

<Static>

<Static> component permanently renders its output above everything else. It's useful for displaying activity like completed tasks or logs - things that are not changing after they're rendered (hence the name "Static").

It's preferred to use <Static> for use cases like these, when you can't know or control the amount of items that need to be rendered.

For example, Tap uses <Static> to display a list of completed tests. Gatsby uses it to display a list of generated pages, while still displaying a live progress bar.

<>
	<Static items={tests}>
		{test => <Test key={test.id} title={test.title} />}
	</Static>

	<Box marginTop={1}>
		<TestResults passed={results.passed} failed={results.failed} />
	</Box>
</>

Note: <Static> only renders new items in items prop and ignores items that were previously rendered. This means that when you add new items to items array, changes you make to previous items will not trigger a rerender.

See examples/jest for a basic implementation of Jest's UI using <Static> component.

items

Type: Array

Array of items of any type to render using a function you pass as a component child.

style

Type: object

Styles to apply to a container of child elements. See <Box> for supported properties.

<Static items={...} style={{padding: 1}}>
	{...}
</Static>
children

Type: Function

Function that is called to render every item in items array. First argument is an item itself and second argument is index of that item in items array.

Note that key must be assigned to the root component.

<Static items={['a', 'b', 'c']}>
	{(item, index) => {
		// This function is called for every item in ['a', 'b', 'c']
		// `item` is 'a', 'b', 'c'
		// `index` is 0, 1, 2
		return <Box key={index}>Item: {item}</Box>;
	}}
</Static>

<Transform>

Transform a string representation of React components before they are written to output. For example, you might want to apply a gradient to text, add a clickable link or create some text effects. These use cases can't accept React nodes as input, they are expecting a string. That's what <Transform> component does, it gives you an output string of its child components and lets you transform it in any way.

<Transform transform={output => output.toUpperCase()}>
	<Text>Hello World</Text>
</Transform>

Since transform function converts all characters to upper case, final output that's rendered to the terminal will be "HELLO WORLD", not "Hello World".

transform(children)

Type: Function

Function which transforms children output. It accepts children and must return transformed children too.

children

Type: string

Output of child components.

Hooks

useInput(inputHandler, options?)

This hook is used for handling user input. It's a more convienient alternative to using useStdin and listening to data events. The callback you pass to useInput is called for each character when user enters any input. However, if user pastes text and it's more than one character, the callback will be called only once and the whole string will be passed as input. You can find a full example of using useInput at examples/use-input.

import {useInput} from 'ink';

const UserInput = () => {
	useInput((input, key) => {
		if (input === 'q') {
			// Exit program
		}

		if (key.leftArrow) {
			// Left arrow key pressed
		}
	});

	return …
};

inputHandler

Type: Function

The handler function that you pass to useInput receives two arguments:

input

Type: string

The input that the program received.

key

Type: object

Handy information about a key that was pressed.

key.leftArrow
key.rightArrow
key.upArrow
key.downArrow

Type: boolean
Default: false

If an arrow key was pressed, the corresponding property will be true. For example, if user presses left arrow key, key.leftArrow equals true.

key.return

Type: boolean
Default: false

Return (Enter) key was pressed.

key.ctrl

Type: boolean
Default: false

Ctrl key was pressed.

key.shift

Type: boolean
Default: false

Shift key was pressed.

key.meta

Type: boolean
Default: false

Meta key was pressed.

options

Type: object

isActive

Type: boolean
Default: true

Enable or disable capturing of user input. Useful when there are multiple useInput hooks used at once to avoid handling the same input several times.

useApp

useApp is a React hook, which exposes a method to manually exit the app (unmount).

exit

Type: Function

Exit (unmount) the whole Ink app. If exit is called with an Error, waitUntilExit will reject with that error.

import {useApp} from 'ink';

const Example = () => {
	const {exit} = useApp();

	// Exit the app after 5 seconds
	useEffect(() => {
		setTimeout(() => {
			exit();
		}, 5000);
	}, []);

	return …
};

useStdin

useStdin is a React hook, which exposes stdin stream.

stdin

Type: stream.Readable
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.

import {useStdin} from 'ink';

const Example = () => {
	const {stdin} = useStdin();

	return …
};

isRawModeSupported

Type: boolean

A boolean flag determining if the current stdin supports setRawMode. A component using setRawMode might want to use isRawModeSupported to nicely fall back in environments where raw mode is not supported.

Usage:

import {useStdin} from 'ink';

const Example = () => {
	const {isRawModeSupported} = useStdin();

	return isRawModeSupported ? (
		<MyInputComponent />
	) : (
		<MyComponentThatDoesntUseInput />
	);
};

setRawMode

Type: function

See setRawMode. Ink exposes this function to be able to handle Ctrl+C, that's why you should use Ink's setRawMode instead of process.stdin.setRawMode.

Warning: This function will throw unless the current stdin supports setRawMode. Use isRawModeSupported to detect setRawMode support.

Usage:

import {useStdin} from 'ink';

const Example = () => {
	const {setRawMode} = useStdin();

	useEffect(() => {
		setRawMode(true);

		return () => {
			setRawMode(false);
		};
	});

	return …
};

useStdout

useStdout is a React hook, which exposes stdout stream, where Ink renders your app.

stdout

Type: stream.Writable
Default: process.stdout

Usage:

import {useStdout} from 'ink';

const Example = () => {
	const {stdout} = useStdout;

	return …
};

write(data)

Write any string to stdout, while preserving Ink's output. It's useful when you want to display some external information outside of Ink's rendering and ensure there's no conflict between the two. It's similar to <Static>, except it can't accept components, it only works with strings.

data

Type: string

Data to write to stdout.

import {useStdout} from 'ink';

const Example = () => {
	const {write} = useStdout();

	useEffect(() => {
		// Write a single message to stdout, above Ink's output
		write('Hello from Ink to stdout\n');
	}, []);

	return …
};

See additional usage example in examples/use-stdout.

useStderr

useStderr is a React hook, which exposes stderr stream.

stderr

Type: stream.Writable
Default: process.stderr

Stderr stream.

import {useStderr} from 'ink';

const Example = () => {
	const {stderr} = useStderr();

	return …
};

write(data)

Write any string to stderr, while preserving Ink's output.

It's useful when you want to display some external information outside of Ink's rendering and ensure there's no conflict between the two. It's similar to <Static>, except it can't accept components, it only works with strings.

data

Type: string

Data to write to stderr.

import {useStderr} from 'ink';

const Example = () => {
	const {write} = useStderr();

	useEffect(() => {
		// Write a single message to stderr, above Ink's output
		write('Hello from Ink to stderr\n');
	}, []);

	return …
};

Useful Hooks

Useful Components

Testing

Ink components are simple to test with ink-testing-library. Here's a simple example that checks how component is rendered:

import React from 'react';
import {Text} from 'ink';
import {render} from 'ink-testing-library';

const Test = () => <Text>Hello World</Text>;
const {lastFrame} = render(<Test />);

lastFrame() === 'Hello World'; //=> true

Visit ink-testing-library for more examples and full documentation.

Using React Devtools

Ink supports React Devtools out-of-the-box. To enable integration with React Devtools in your Ink-based CLI, run it with DEV=true environment variable:

$ DEV=true my-cli

Then, start React Devtools itself:

$ npx react-devtools

After it starts up, you should see the component tree of your CLI. You can even inspect and change the props of components, and see the results immediatelly in the CLI, without restarting it.

Note: You must manually quit your CLI via Ctrl+C after you're done testing.

Maintainers

About

🌈 React for interactive command-line apps

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 96.1%
  • JavaScript 3.9%