How to display a flex box in terminal
Displaying a flexbox-like layout in a terminal is not as straightforward as in web development, since terminals don’t support CSS. However, you can achieve similar effects using tools like:
NCurses (for C/C++) – Provides a way to create UI elements in the terminal.
Blessed (for Node.js) – A library that allows flexible TUI (Text-based UI) layouts.
Rich (for Python) – Supports flexible text-based layouts.
ASCII Art & Padding – Manually format text using spaces or box-drawing characters.
rich
library
If you're using Python, the
rich
library supports flexible layouts using tables and
columns.
bashpip install rich
pythonfrom rich.console import Console from rich.layout import Layout console = Console() layout = Layout() layout.split_row( Layout(name="left", size=30), Layout(name="right") ) layout["left"].update("Left Content") layout["right"].update("Right Content") console.print(layout)
This will create a
two-column flexible layout, similar to a flexbox with
display: flex; justify-content:
space-between;
.
If you're using JavaScript (Node.js), you can use the
blessed
package.
bashnpm install blessed
javascriptconst blessed = require('blessed'); const screen = blessed.screen(); const box1 = blessed.box({ top: 'center', left: 'left', width: '50%', height: '100%', content: 'Left Box', border: { type: 'line' } }); const box2 = blessed.box({ top: 'center', left: '50%', width: '50%', height: '100%', content: 'Right Box', border: { type: 'line' } }); screen.append(box1); screen.append(box2); screen.render();
This splits the terminal into two flexible sections, much like a flexbox layout.
Would you like a solution in another language or a more specific terminal tool? 🚀
If you're building a TUI (Text User Interface) application in Node.js, here are the most useful and powerful packages you can use:
📌
GitHub:
chjj/blessed
✔️
Best for:
Creating full-featured terminal UIs with widgets
(buttons, text boxes, lists, etc.)
🔹 Features:
Supports layouts similar to CSS Flexbox
Can create interactive forms, tables, and even dashboards
Works with mouse and keyboard input
🔹 Installation:
bashnpm install blessed
🔹 Example:
javascriptconst blessed = require('blessed'); const screen = blessed.screen(); const box = blessed.box({ top: 'center', left: 'center', width: '50%', height: '50%', content: 'Hello, Terminal!', border: { type: 'line' } }); screen.append(box); screen.render();
📌
GitHub:
yaronn/blessed-contrib
✔️
Best for:
Creating
beautiful dashboards
in the terminal with charts, grids, and tables
🔹 Features:
Supports bar charts, line graphs, maps, and tables
Built on top of Blessed
🔹 Installation:
bashnpm install blessed-contrib
🔹 Example (Simple Dashboard):
javascriptconst blessed = require('blessed'); const contrib = require('blessed-contrib'); const screen = blessed.screen(); const grid = new contrib.grid({ rows: 2, cols: 2, screen }); const chart = contrib.line({ label: 'CPU Usage', style: { line: 'yellow', text: 'green', baseline: 'black' } }); grid.set(0, 0, 2, 2, chart); screen.render();
📌
GitHub:
vadimdemedes/ink
✔️
Best for:
Building terminal apps using
React components
🔹 Features:
Uses
React syntax
(<Component />
) for building UIs
Supports hooks (useState
,
useEffect
)
Works well with CLI applications
🔹 Installation:
bashnpm install ink
🔹 Example (Simple React TUI App):
javascriptconst React = require('react'); const { render, Text } = require('ink'); const App = () => <Text>Hello, Terminal!</Text>; render(<App />);
📌
GitHub:
cronvel/terminal-kit
✔️
Best for:
Creating interactive
command-line tools
🔹 Features:
Handles colors, menus, input fields, and tables
Supports autocomplete and progress bars
🔹 Installation:
bashnpm install terminal-kit
🔹 Example (User Input):
javascriptconst term = require('terminal-kit').terminal; term.green('What is your name? '); term.inputField({}, (err, input) => { term.green(`\nHello, ${input}!\n`); process.exit(); });
📌
GitHub:
nutboltu/cli-ui
✔️
Best for:
Simple
interactive prompts and UI elements
🔹 Features:
Lightweight and simple API
Includes spinners, progress bars, and colors
🔹 Installation:
bashnpm install cli-ui
🔹 Example:
javascriptconst cliUi = require('cli-ui'); cliUi.loading.start('Fetching data...'); setTimeout(() => cliUi.loading.stop('Done!'), 3000);
Package | Best For |
---|---|
Blessed | Full TUI apps with flexbox layouts |
Blessed-Contrib | Dashboards with charts & tables |
Ink | React-style terminal apps |
Terminal-Kit | Interactive CLI tools |
Cli-Ui | Lightweight UI elements like spinners & progress bars |
Do you need help choosing the right one for your project? 🚀
Yoga is an open-source cross-platform layout engine that implements Flexbox for UI development. It was created by Meta (Facebook) to provide fast and efficient layout calculations for various platforms, including mobile, web, and desktop applications.
🔹
GitHub Repo:
facebook/yoga
🔹
Written In:
C++
🔹
Bindings Available For:
JavaScript, TypeScript, Python, Rust, Go, and more.
Implements CSS Flexbox: Brings the power of CSS Flexbox to non-web environments.
Lightweight & Fast: Designed for high-performance rendering in UI frameworks.
Cross-Platform: Works on Android, iOS, React Native, WebAssembly, and more.
Low-Level Control: Can be embedded in game engines, terminal UIs, or native apps.
Used in React Native: React Native’s layout system is powered by Yoga.
Instead of relying on a browser's CSS engine, Yoga computes layouts independently using a C++ implementation of Flexbox rules. It takes a tree of nodes (like a UI structure) and calculates the sizes/positions based on Flexbox rules.
Let's use JavaScript (Node.js) with yoga-layout:
bashnpm install yoga-layout
javascriptconst yoga = require('yoga-layout'); // Create a root node const root = yoga.Node.create(); root.setWidth(500); root.setHeight(300); root.setFlexDirection(yoga.FLEX_DIRECTION_ROW); // Like "display: flex; flex-direction: row;" // Create a child node const child = yoga.Node.create(); child.setWidth(100); child.setHeight(100); child.setMargin(yoga.EDGE_ALL, 10); // Add margin // Attach child to root root.insertChild(child, 0); // Calculate layout root.calculateLayout(500, 300, yoga.DIRECTION_LTR); console.log(root.getComputedLayout()); // Output the calculated positions console.log(child.getComputedLayout()); // Output child position
React Native (Handles all layout calculations)
Component-based UI frameworks
Terminal-based UIs (TUI)
Game UI layouts (Unity, Unreal, etc.)
Cross-platform desktop applications
✔ Yes, if you:
Need flexbox-style layouts outside of a browser.
Are working with React Native or embedded UI systems.
Want consistent UI layouts across platforms.
❌ No, if you:
Are just building for the web (use CSS Flexbox instead).
Don't need fine-grained control over layout calculations.
Would you like help integrating Yoga into a specific project? 🚀
You can use Yoga Layout for flexbox-style layouts in a terminal UI (TUI) by combining it with a rendering library like Blessed or Ink (React for terminal).
Use Yoga Layout to calculate flexible layouts.
Render the computed layout inside a terminal UI framework like Blessed or Ink.
🔹 Install Dependencies:
bashnpm install blessed yoga-layout
🔹 Code Example (Basic TUI with Flexbox)
javascriptconst blessed = require('blessed'); const yoga = require('yoga-layout'); // Create the root node const root = yoga.Node.create(); root.setWidth(80); root.setHeight(24); root.setFlexDirection(yoga.FLEX_DIRECTION_ROW); // Create a left panel const leftPanel = yoga.Node.create(); leftPanel.setWidth(40); leftPanel.setHeight(24); leftPanel.setMargin(yoga.EDGE_ALL, 1); root.insertChild(leftPanel, 0); // Create a right panel const rightPanel = yoga.Node.create(); rightPanel.setFlexGrow(1); rightPanel.setHeight(24); rightPanel.setMargin(yoga.EDGE_ALL, 1); root.insertChild(rightPanel, 1); // Calculate layout root.calculateLayout(80, 24, yoga.DIRECTION_LTR); // Create a Blessed screen const screen = blessed.screen(); const box1 = blessed.box({ top: root.getComputedTop(), left: root.getComputedLeft(), width: leftPanel.getComputedWidth(), height: leftPanel.getComputedHeight(), content: 'Left Panel', border: { type: 'line' }, style: { border: { fg: 'blue' } } }); const box2 = blessed.box({ top: rightPanel.getComputedTop(), left: rightPanel.getComputedLeft(), width: rightPanel.getComputedWidth(), height: rightPanel.getComputedHeight(), content: 'Right Panel (Flexible)', border: { type: 'line' }, style: { border: { fg: 'green' } } }); // Append boxes and render screen screen.append(box1); screen.append(box2); screen.render();
Yoga calculates the flexible layout.
Blessed renders the UI based on Yoga's computed positions.
The left panel has a fixed width.
The right panel flexes and fills the remaining space.
Ink is a React-based framework for building TUI apps. You can use Yoga’s layout engine inside Ink components.
🔹 Install Dependencies:
bashnpm install ink yoga-layout
🔹 Code Example (Basic Flexbox in Ink)
javascriptconst React = require('react'); const { render, Box, Text } = require('ink'); const App = () => ( <Box flexDirection="row"> <Box width={30} borderStyle="round" borderColor="blue" padding={1}> <Text>Left Panel</Text> </Box> <Box flexGrow={1} borderStyle="round" borderColor="green" padding={1}> <Text>Right Panel (Flexible)</Text> </Box> </Box> ); render(<App />);
Ink internally uses Yoga Layout.
flexGrow={1}
makes the right panel flexible.
Ink handles rendering without needing direct Yoga calls.
Framework | Best For |
---|---|
Blessed + Yoga | Low-level TUI with full Yoga control |
Ink (Built-in Yoga) | React-style TUI with Flexbox |
🔹
Use Ink
if you prefer
React-style development.
🔹
Use Blessed + Yoga
if you need
fine-grained Yoga control.
Would you like to extend this into a full interactive terminal app? 🚀
No, you don’t have to explicitly set a width and height in Yoga. Instead, Yoga provides flexbox properties that allow elements to dynamically size based on their content and parent container.
If you don't define
setWidth()
and
setHeight()
, the node will:
Shrink to fit its content (if no flex properties are set).
Expand dynamically
if
flexGrow
is enabled.
Be constrained by its parent’s layout if a parent node defines width/height.
javascriptconst yoga = require('yoga-layout'); // Root node (container) const root = yoga.Node.create(); root.setFlexDirection(yoga.FLEX_DIRECTION_ROW); root.setWidth(500); // Fixed width, height can be dynamic // Child node 1 (Fixed size) const box1 = yoga.Node.create(); box1.setWidth(100); box1.setHeight(100); root.insertChild(box1, 0); // Child node 2 (Flexible size) const box2 = yoga.Node.create(); box2.setFlexGrow(1); // Expands to fill remaining space box2.setHeight(100); root.insertChild(box2, 1); // Compute layout root.calculateLayout(undefined, undefined, yoga.DIRECTION_LTR); console.log('Box1:', box1.getComputedLayout()); console.log('Box2:', box2.getComputedLayout());
Property | Behavior |
---|---|
setWidth()
/
setHeight()
|
Sets a fixed size. |
setFlexGrow(1)
|
Expands to fill remaining space. |
setFlexShrink(1)
|
Shrinks when necessary. |
setAlignSelf(yoga.ALIGN_STRETCH)
|
Stretches to fill parent’s height. |
ALIGN_STRETCH
javascriptconst container = yoga.Node.create(); container.setWidth(300); container.setHeight(400); container.setFlexDirection(yoga.FLEX_DIRECTION_COLUMN); const child = yoga.Node.create(); child.setFlexGrow(1); // Expands to fill available space child.setAlignSelf(yoga.ALIGN_STRETCH); // Stretches to parent's height container.insertChild(child, 0); container.calculateLayout(undefined, undefined, yoga.DIRECTION_LTR); console.log('Child layout:', child.getComputedLayout());
✔
No, you don’t need to set explicit
width/height.
✔ Use
Flexbox properties
like
flexGrow
,
flexShrink
, and
alignSelf
to create dynamic layouts.
✔ If an element has no content and no explicit size,
it may collapse to
0x0
size.
Would you like a real-world example in a TUI? 🚀
No,
Blessed does NOT support Flexbox natively. It relies on
absolute positioning
(top
,
left
,
width
,
height
)
and
box models
instead. However, you can
combine Yoga Layout
with Blessed to achieve
Flexbox-like layouts
in a terminal.
Blessed has a grid-like positioning system with properties like:
top
,
left
,
width
,
height
(absolute positioning)
shrink
(content-based sizing)
align
,
valign
(text alignment, not Flexbox)
padding
,
margin
(basic spacing)
🚫 What Blessed lacks:
No
flexDirection
(row
/
column
layouts)
No
flexGrow
/
flexShrink
No auto-spacing like CSS Flexbox
Since Blessed doesn’t have built-in Flexbox, you can use Yoga Layout to calculate positions dynamically, then apply them manually to Blessed elements.
javascriptconst blessed = require('blessed'); const yoga = require('yoga-layout'); // Create a root layout node const root = yoga.Node.create(); root.setWidth(80); root.setHeight(24); root.setFlexDirection(yoga.FLEX_DIRECTION_ROW); // Left panel (fixed size) const leftPanel = yoga.Node.create(); leftPanel.setWidth(30); leftPanel.setHeight(24); root.insertChild(leftPanel, 0); // Right panel (flexible) const rightPanel = yoga.Node.create(); rightPanel.setFlexGrow(1); // Takes remaining space rightPanel.setHeight(24); root.insertChild(rightPanel, 1); // Calculate layout root.calculateLayout(80, 24, yoga.DIRECTION_LTR); // Create Blessed screen const screen = blessed.screen(); const box1 = blessed.box({ top: 0, left: 0, width: leftPanel.getComputedWidth(), height: leftPanel.getComputedHeight(), content: 'Left Panel (Fixed)', border: { type: 'line' }, style: { border: { fg: 'blue' } } }); const box2 = blessed.box({ top: 0, left: leftPanel.getComputedWidth(), width: rightPanel.getComputedWidth(), height: rightPanel.getComputedHeight(), content: 'Right Panel (Flexbox)', border: { type: 'line' }, style: { border: { fg: 'green' } } }); // Append and render screen.append(box1); screen.append(box2); screen.render();
If you only need basic flexible layouts, you can use blessed-contrib, which provides a grid system (though it’s not true Flexbox).
🔹
Install
blessed-contrib
:
bashnpm install blessed blessed-contrib
🔹
Example: Using
blessed-contrib
Grid
javascriptconst blessed = require('blessed'); const contrib = require('blessed-contrib'); // Create screen and grid const screen = blessed.screen(); const grid = new contrib.grid({ rows: 1, cols: 2, screen }); // Left panel (Fixed) const box1 = grid.set(0, 0, 1, 1, blessed.box, { content: 'Left Panel', border: { type: 'line' }, style: { border: { fg: 'blue' } } }); // Right panel (Auto width) const box2 = grid.set(0, 1, 1, 1, blessed.box, { content: 'Right Panel (Flex-like)', border: { type: 'line' }, style: { border: { fg: 'green' } } }); screen.render();
Approach | Pros | Cons |
---|---|---|
Blessed + Yoga | True Flexbox behavior | Manual positioning required |
Blessed-Contrib Grid | Simple, built-in | Less flexible than Yoga |
Raw Blessed | More control | No Flexbox |
🔹 Best choice?
If you want real Flexbox, use Yoga Layout + Blessed.
If you want easier layouts, try blessed-contrib.
Would you like help with a specific TUI layout? 🚀
Yes! You can use React + Ink to create a JSX-based terminal UI with Flexbox layouts using Yoga.
🚀 Ink is a React renderer for CLI applications, and it uses Yoga Layout internally for rendering, meaning you get Flexbox support out of the box—just like React Native.
bashnpm install ink
jsximport React from 'react'; import { render, Box, Text } from 'ink'; const App = () => ( <Box flexDirection="row"> {/* Left Panel (Fixed width) */} <Box width={30} borderStyle="round" borderColor="blue"> <Text>Left Panel</Text> </Box> {/* Right Panel (Flexible width) */} <Box flexGrow={1} borderStyle="round" borderColor="green"> <Text>Right Panel (Flexbox)</Text> </Box> </Box> ); render(<App />);
🔹 Box
in
Ink behaves just like a
Yoga node, so you can use
flexDirection
,
flexGrow
,
justifyContent
, etc.
If you want to use Blessed with JSX, you can use React-Blessed, but it does not support Yoga Layout.
jsximport React from 'react'; import { render } from 'react-blessed'; const App = () => ( <box top="0" left="0" width="50%" height="100%" border={{ type: 'line' }}> Left Panel </box> ); const screen = render(<App />);
📌 Why Ink is Better?
Supports Flexbox natively using Yoga.
More React-like (closer to React Native).
More active development than React-Blessed.
✔
Ink is the best option
for JSX-based TUI with Yoga-style Flexbox.
✔ If you still need Blessed,
use Ink for layout
and
Blessed for widgets.
✔
React-Blessed is an alternative, but lacks Yoga's Flexbox support.
Would you like a more complex Ink example (e.g., handling user input)? 🚀
Got it! You want to map Yoga + Blessed into JSX components while still using Blessed for rendering. This is possible using React-Blessed, which lets you write JSX components that render in the terminal using Blessed.
We will:
Use React-Blessed to handle the terminal UI in JSX.
Use Yoga Layout inside JSX components to calculate positions.
Render the result into Blessed elements dynamically.
bashnpm install react react-blessed yoga-layout blessed
App.jsx
Here’s how you can replace your Yoga + Blessed setup with JSX components:
jsximport React, { useEffect, useState } from "react"; import { render } from "react-blessed"; import blessed from "blessed"; import yoga from "yoga-layout"; // Flexbox component using Yoga Layout const FlexContainer = ({ children }) => { const [layout, setLayout] = useState(null); useEffect(() => { // Root Yoga Node (80x24) const root = yoga.Node.create(); root.setWidth(80); root.setHeight(24); root.setFlexDirection(yoga.FLEX_DIRECTION_ROW); // Left panel (fixed size) const leftPanel = yoga.Node.create(); leftPanel.setWidth(30); leftPanel.setHeight(24); root.insertChild(leftPanel, 0); // Right panel (flexible) const rightPanel = yoga.Node.create(); rightPanel.setFlexGrow(1); rightPanel.setHeight(24); root.insertChild(rightPanel, 1); // Calculate layout root.calculateLayout(80, 24, yoga.DIRECTION_LTR); setLayout({ leftPanel: { width: leftPanel.getComputedWidth(), height: leftPanel.getComputedHeight(), }, rightPanel: { width: rightPanel.getComputedWidth(), height: rightPanel.getComputedHeight(), left: leftPanel.getComputedWidth(), }, }); }, []); if (!layout) return null; return ( <> <box top={0} left={0} width={layout.leftPanel.width} height={layout.leftPanel.height} border={{ type: "line" }} style={{ border: { fg: "blue" } }} > Left Panel (Fixed) </box> <box top={0} left={layout.rightPanel.left} width={layout.rightPanel.width} height={layout.rightPanel.height} border={{ type: "line" }} style={{ border: { fg: "green" } }} > Right Panel (Flexbox) </box> </> ); }; // Main App Component const App = () => { return ( <FlexContainer /> ); }; // Create Blessed Screen & Render App const screen = blessed.screen(); render(<App />, screen);
✅ Uses
JSX components
instead of raw Blessed objects.
✅ Uses
Yoga for layout calculations
inside
useEffect()
.
✅ Dynamically
maps Yoga nodes to Blessed JSX elements.
✅
Automatically re-renders when layout
updates.
✔
Write Terminal UI in JSX, making it easier to structure.
✔
Keep Flexbox layout using Yoga, instead of manual calculations.
✔
React-Blessed handles rendering, just like a normal React app.
Would you like to add more features like input handling or dynamic resizing? 🚀