React for CLIs. Build and test your CLI output using components.
Ink provides the same component-based UI building experience that React offers in the browser, but for command-line apps. It uses Yoga to build Flexbox layouts in the terminal, so most CSS-like props are available in Ink as well. If you are already familiar with React, you already know Ink.
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. Only Ink's methods will be documented in this readme.
Note: This is documentation for Ink 4 and 5. If you're looking for docs on Ink 3, check out this release.
npm install ink react
<img src="media/demo.svg" width="600">import React, {useState, useEffect} from 'react'; import {render, Text} from 'ink'; const Counter = () => { const [counter, setCounter] = useState(0); useEffect(() => { const timer = setInterval(() => { setCounter(previousCounter => previousCounter + 1); }, 100); return () => { clearInterval(timer); }; }, []); return <Text color="green">{counter} tests passed</Text>; }; 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.
kyt
- a toolkit that encapsulates and manages the configuration for web apps.node_modules
directories in order to free up disk space.Use create-ink-app to quickly scaffold a new Ink-based CLI.
npx create-ink-app my-ink-cli
Alternatively, create a TypeScript project:
<details><summary>Manual JavaScript setup</summary> <p> Ink requires the same Babel setup as you would do for regular React-based apps in the browser.npx create-ink-app --typescript my-ink-cli
Set up Babel with a React preset to ensure all examples in this readme work as expected.
After installing Babel, install @babel/preset-react
and insert the following configuration in babel.config.json
:
npm install --save-dev @babel/preset-react
{ "presets": ["@babel/preset-react"] }
Next, create a file source.js
, where you'll type code that uses Ink:
import React from 'react'; import {render, Text} from 'ink'; const Demo = () => <Text>Hello World</Text>; render(<Demo />);
Then, transpile this file with Babel:
npx babel source.js -o cli.js
Now you can run cli.js
with Node.js:
node cli
If you don't like transpiling files during development, you can use import-jsx or @esbuild-kit/esm-loader to import
a JSX file and transpile it on the fly.
Ink uses Yoga - a Flexbox layout engine to build great user interfaces for your CLIs using familiar CSS-like props you've used when building apps for the browser.
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.
Note that all text must be wrapped in a <Text>
component.
<Text>
This component can display text, and change its style to make it bold, underline, italic or strikethrough.
import {render, Text} from 'ink'; const Example = () => ( <> <Text color="green">I am green</Text> <Text color="black" backgroundColor="white"> I am black on white </Text> <Text color="#ffffff">I am white</Text> <Text bold>I am bold</Text> <Text italic>I am italic</Text> <Text underline>I am underline</Text> <Text strikethrough>I am strikethrough</Text> <Text inverse>I am inversed</Text> </> ); render(<Example />);
Note: <Text>
allows only text nodes and nested <Text>
components inside of it. For example, <Box>
component can't be used inside <Text>
.
Type: string
Change text color. Ink uses chalk under the hood, so all its functionality is supported.
<img src="media/text-color.jpg" width="247"><Text color="green">Green</Text> <Text color="#005cc5">Blue</Text> <Text color="rgb(232, 131, 136)">Red</Text>
Type: string
Same as color
above, but for background.
<img src="media/text-backgroundColor.jpg" width="226"><Text backgroundColor="green" color="white">Green</Text> <Text backgroundColor="#005cc5" color="white">Blue</Text> <Text backgroundColor="rgb(232, 131, 136)" color="white">Red</Text>
Type: boolean
Default: false
Dim the color (emit a small amount of light).
<img src="media/text-dimColor.jpg" width="138"><Text color="red" dimColor> Dimmed Red </Text>
Type: boolean
Default: false
Make the text bold.
Type: boolean
Default: false
Make the text italic.
Type: boolean
Default: false
Make the text underlined.
Type: boolean
Default: false
Make the text crossed with a line.
Type: boolean
Default: false
Inverse background and foreground colors.
<img src="media/text-inverse.jpg" width="138"><Text inverse color="yellow"> Inversed Yellow </Text>
Type: string
Allowed values: wrap
truncate
truncate-start
truncate-middle
truncate-end
Default: wrap
This property tells Ink to wrap or truncate text 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 width={7}> <Text>Hello World</Text> </Box> //=> 'Hello\nWorld' // `truncate` is an alias to `truncate-end` <Box width={7}> <Text wrap="truncate">Hello World</Text> </Box> //=> 'Hello…' <Box width={7}> <Text wrap="truncate-middle">Hello World</Text> </Box> //=> 'He…ld' <Box width={7}> <Text wrap="truncate-start">Hello World</Text> </Box> //=> '…World'
<Box>
<Box>
is an essential Ink component to build your layout.
It's like <div style="display: flex">
in the browser.
import {render, Box, Text} from 'ink'; const Example = () => ( <Box margin={2}> <Text>This is a box with margin</Text> </Box> ); render(<Example />);
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}> <Text>X</Text> </Box> //=> 'X '
<Box width={10}> <Box width="50%"> <Text>X</Text> </Box> <Text>Y</Text> </Box> //=> 'X Y'
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}> <Text>X</Text> </Box> //=> 'X\n\n\n'
<Box height={6} flexDirection="column"> <Box height="50%"> <Text>X</Text> </Box> <Text>Y</Text> </Box> //=> 'X\n\n\nY\n\n'
Type: number
Sets a minimum width of the element. Percentages aren't supported yet, see https://github.com/facebook/yoga/issues/872.
Type: number
Sets a minimum height of the element. Percentages aren't supported yet, see https://github.com/facebook/yoga/issues/872.
Type: number
Default: 0
Top padding.
Type: number
Default: 0
Bottom padding.
Type: number
Default: 0
Left padding.
Type: number
Default: 0
Right padding.
Type: number
Default: 0
Horizontal padding. Equivalent to setting paddingLeft
and paddingRight
.
Type: number
Default: 0
Vertical padding. Equivalent to setting paddingTop
and paddingBottom
.
Type: number
Default: 0
Padding on all sides. Equivalent to setting paddingTop
, paddingBottom
, paddingLeft
and paddingRight
.
<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
Top margin.
Type: number
Default: 0
Bottom margin.
Type: number
Default: 0
Left margin.
Type: number
Default: 0
Right margin.
Type: number
Default:
AI小说写作助手,一站式润色、改写、扩写
蛙蛙写作—国内先进的AI写作平台,涵盖小说、学术、社交媒体等多场景。提供续写、改写、润色等功能,助力创作者高效优化写作流程。界面简洁,功能全面,适合各类写作者提升内容品质和工作效率。
字节跳动发布的AI编程神器IDE
Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。
全能AI智能助手,随时解答生活与工作的多样问题
问小白,由元石科技研发的AI智能助手,快速准确地解答各种生活和工作问题,包括但不限于搜索、规划和社交互动,帮助用户在日常生活中提高效率,轻松管理个人事务。
实时语音翻译/同声传译工具
Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。
一键生成PPT和Word,让学习生活更轻松
讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内容,它都能提供支持,帮助用户精准表达,轻松呈现各种信息。
深度推理能力全新升级,全面对标OpenAI o1
科大讯飞的星火大模型,支持语言理解、知识问答和文本创作等多功能,适用于多种文件和业务场景,提升办公和日常生活的效率。讯飞星火是一个提供丰富智能服务的平台,涵盖科技资讯、图像创作、写作辅助、编程解答、科研文献解读等功能,能为不同需求的用户提供便捷高效的帮助,助力用户轻松获取信息、解决问题,满足多样化使用场景。
一种基于大语言模型的高效单流解耦语音令牌文本到语音合成模型
Spark-TTS 是一个基于 PyTorch 的开源文本到语音合成项目,由多个知名机构联合参与。该项目提供了高效的 LLM(大 语言模型)驱动的语音合成方案,支持语音克隆和语音创建功能,可通过命令行界面(CLI)和 Web UI 两种方式使用。用户可以根据需求调整语音的性别、音高、速度等参数,生成高质量的语音。该项目适用于多种场景,如有声读物制作、智能语音助手开发等。
AI助力,做PPT更简单!
咔片是一款轻量化在线演示设计工具,借助 AI 技术,实现从内容生成到智能设计的一站式 PPT 制作服务。支持多种文档格式导入生成 PPT,提供海量模板、智能美化、素材替换等功能,适用于销售、教师、学生等各类人群,能高效制作出高品质 PPT,满足不同场景演示需求。
选题、配图、成文,一站式创作,让内容运营更高效
讯飞绘文,一个AI集成平台,支持写作、选题、配图、排版和发布。高效生成适用于各类媒体的定制内容,加速品牌传播,提升内容营销效果。
专业的AI公文写作平台,公文写作神器
AI 材料星,专业的 AI 公文写作辅助平台,为体制内工作人员提供高效的公文写作解决方案。拥有海量公文文库、9 大核心 AI 功能,支持 30 + 文稿类型生成,助力快速完成领导讲话、工作总结、述职报告等材料,提升办公效率,是体制打工人的得力写作神器。
最新AI工具、AI资讯
独家AI资源、AI项目落地
微信扫一扫关注公众号