March 6, 2019

Ink is a library for building and testing command-line applications using React components. Since it acts as a React renderer, you can use everything that React has to offer: hooks, class components, state, context, everything. Ink lets you build interactive and beautiful CLIs in no time. Here's basic implementation of Jest's UI (view source code):

Basic Jest UI implemented with Ink

I hope I had your curiosity, but now I need your attention. Let's step back a bit and check out the building blocks that Ink offers, which allow building such UIs as above. Here's how the most basic Ink app looks like:

1 import React from 'react' ; 2 import { render , Text } from 'ink' ; 3 4 const Hello = ( ) => < Text > Hello World </ Text > ; 5 render ( < Hello /> ) ;

Basic Ink App

Styling and colorizing output is essential for CLIs that want to look good. You can use Ink's built-in components such as <Text> and <Color> , which use everyone's beloved chalk library under the hood. It's as easy as this:

1 import { render , Color } from 'ink' ; 2 3 const Hello = ( ) => < Color green > Hello World </ Color > ; 4 render ( < Hello /> ) ;

Colors colors colors

Ink also implements Flexbox layout mechanism by leveraging Yoga Layout library from Facebook. You can finally forget about positioning elements in the output by using a bunch of spaces and \

. With Ink, for the first time ever you can use properties like flex , flexDirection , alignItems , justifyContent , margin , padding and width which you've only been able to use in the browser, until now. All <Box> es in Ink are flexbox-enabled (think <div style={{display: 'flex'}}/> by default). Here's an example layout to show you the power of Flexbox in CLIs:

1 import { render , Box , Text , Color } from 'ink' ; 2 3 const Flexbox = ( ) => ( 4 < Box padding = { 2 } flexDirection = " column " > 5 < Box > 6 < Box width = { 14 } > 7 < Text bold > Total tests : </ Text > 8 </ Box > 9 12 10 </ Box > 11 12 < Box > 13 < Box width = { 14 } > 14 < Text bold > Passed : </ Text > 15 </ Box > 16 17 < Color green > 10 </ Color > 18 </ Box > 19 20 < Box > 21 < Box width = { 14 } > 22 < Text bold > Failed : </ Text > 23 </ Box > 24 25 < Color red > 2 </ Color > 26 </ Box > 27 </ Box > 28 ) ; 29 30 render ( < Flexbox /> ) ;

Flexbox in action

As I mentioned in the beginning, Ink supports all React features, such as state. If you know React, you know Ink already. In the following counter example, you can see that the only thing different here is <Text> instead of <span> :

1 import React , { useState , useEffect } from 'react' ; 2 import { render , Text } from 'ink' ; 3 4 const Counter = ( ) => { 5 const [ count , setCount ] = useState ( 0 ) ; 6 7 useEffect ( ( ) => { 8 const interval = setInterval ( ( ) => { 9 setCount ( count => count + 1 ) ; 10 } , 100 ) ; 11 12 return ( ) => clearInterval ( interval ) ; 13 } , [ ] ) ; 14 15 return < Text > Count : { count } </ Text > ; 16 } ; 17 18 render ( < Counter /> ) ;

Hooks and state

React, Ink and Yoga combined provide a simple path towards creating beautiful, maintainable and testable command-line interfaces. Back in the day when we were working on AVA, I remember the fear of removing a space or a newline, which could mess up the output and fail tests. If we had Ink back then and embraced React components for generating output of our CLI, it would've been much easier to maintain and predict the consequences of the changes we were making. With Ink, you can test components individually or your app as a whole. You also don't need to think how to implement an interactive CLI output or how to properly accept input from the user. Ink took care of it for you, so you can deliver great CLIs faster than ever before.

It's really the future of writing interactive CLI tools.

Sindre Sorhus

I hope after trying out Ink you will think the same way Sindre does! Let me know on GitHub if you run into any issues or have some ideas on how to improve Ink!

On a personal note, it feels amazing to write this blog post, which concludes almost half a year of work, on and off. I want to thank Sindre for helping to shape Ink as it is today and providing extremely useful feedback and advice since day 0. Almost 2 years ago, we were only discussing what Ink could be on the remote Thai island at 1am with cocktails on the table!

I also want to thank Kat Marchán from npm team and Simen Bekkhus from Jest team for being early-adopters of Ink. Even if they don't end up using Ink in their projects, I'm still going to be happy that Ink was worth trying out! Kat's work and feedback helped stabilize Ink ahead of launch, which led to some important bug fixes and new features being introduced before the release.

Thank you! ❤️