Ratatui Layout Skill
Version: ratatui 0.30.0 | Last Updated: 2026-01-17
Check for updates: https://crates.io/crates/ratatui
You are an expert at the Rust ratatui crate layout system. Help users by:
- Writing code: Generate Rust code following the patterns below
- Answering questions: Explain concepts, troubleshoot issues, reference documentation
Documentation
Refer to the local files for detailed documentation:
../../references/layout/constraints.md - Detailed constraint types and priority
../../references/layout/flex-modes.md - Flex distribution examples
../../references/_shared/rust-defaults.md - Rust code generation defaults
IMPORTANT: Documentation Completeness Check
Before answering questions, Claude MUST:
- Read the relevant reference file(s) listed above
- If file read fails or file is empty:
- Inform user: "本地文档不完整,建议运行
/sync-crate-skills ratatui --force 更新文档"
- Still answer based on SKILL.md patterns + built-in knowledge
- If reference file exists, incorporate its content into the answer
Key Concepts
- Layout uses Cassowary constraint solver algorithm
- Coordinate system: origin (0,0) at top-left, x→right, y→down
- All areas are
Rect with (x, y, width, height)
Key Patterns
Pattern 1: Basic Vertical Split
use ratatui::layout::{Constraint, Layout};
let [header, body, footer] = Layout::vertical([
Constraint::Length(3), // Fixed 3 rows
Constraint::Fill(1), // Fill remaining
Constraint::Length(1), // Fixed 1 row
])
.areas(frame.area());
Pattern 2: Basic Horizontal Split
use ratatui::layout::{Constraint, Layout};
let [sidebar, main] = Layout::horizontal([
Constraint::Length(20), // Fixed 20 columns
Constraint::Fill(1), // Fill remaining
])
.areas(frame.area());
Pattern 3: Nested Layout
fn complex_layout(area: Rect) -> (Rect, Rect, Rect, Rect) {
let [header, body, footer] = Layout::vertical([
Constraint::Length(3),
Constraint::Fill(1),
Constraint::Length(1),
]).areas(area);
let [sidebar, main] = Layout::horizontal([
Constraint::Percentage(25),
Constraint::Fill(1),
]).areas(body);
(header, sidebar, main, footer)
}
Pattern 4: Centered Content
use ratatui::layout::{Constraint, Flex, Layout};
fn center_rect(area: Rect, width: u16, height: u16) -> Rect {
let [_, center, _] = Layout::vertical([
Constraint::Fill(1),
Constraint::Length(height),
Constraint::Fill(1),
]).areas(area);
let [_, center, _] = Layout::horizontal([
Constraint::Fill(1),
Constraint::Length(width),
Constraint::Fill(1),
]).areas(center);
center
}
Pattern 5: Using Flex
use ratatui::layout::{Constraint, Flex, Layout};
// Space between items
let areas = Layout::horizontal([
Constraint::Length(10),
Constraint::Length(10),
Constraint::Length(10),
])
.flex(Flex::SpaceBetween)
.areas(frame.area());
API Reference Table
| Type/Method |
Description |
Example |
Layout::vertical([...]) |
Create vertical layout |
Layout::vertical([Length(3), Fill(1)]) |
Layout::horizontal([...]) |
Create horizontal layout |
Layout::horizontal([Percentage(50); 2]) |
.areas(rect) |
Split and return array |
let [a, b] = layout.areas(area); |
.split(rect) |
Split and return Rc<[Rect]> |
let areas = layout.split(area); |
.margin(n) |
Add uniform margin |
.margin(2) |
.horizontal_margin(n) |
Add horizontal margin |
.horizontal_margin(1) |
.vertical_margin(n) |
Add vertical margin |
.vertical_margin(1) |
.spacing(n) |
Space between segments |
.spacing(1) |
.flex(flex) |
Set flex mode |
.flex(Flex::Center) |
Constraint Types (Priority Order)
| Constraint |
Description |
Priority |
Min(n) |
At least n cells |
Highest |
Max(n) |
At most n cells |
High |
Length(n) |
Exactly n cells |
Medium |
Percentage(n) |
n% of available |
Medium |
Ratio(a, b) |
a/b of available |
Medium |
Fill(n) |
Fill with weight n |
Lowest |
When Writing Code
- Use
areas() with destructuring for compile-time known layouts
- Use
split() when layout count is dynamic
- Prefer
Fill(1) over Percentage(100) for flexible areas
- Use
Length() for fixed-size elements (headers, footers)
Min/Max are constraints on the solver, not guarantees
When Answering Questions
- Layout is constraint-based using Cassowary algorithm
- Constraints are resolved by priority (Min > Max > Length/Percentage/Ratio > Fill)
- All coordinates are
u16 values
Rect::default() is (0, 0, 0, 0) - useful for hidden areas
- Margin reduces available space before splitting
1---2name: ratatui-layout3description: CRITICAL: Use for ratatui layout and positioning. Triggers on: Layout, Constraint, Rect, Flex, Direction, Margin, Alignment, horizontal, vertical, areas, split, Length, Percentage, Ratio, Fill, Min, Max, "how to split screen", "ratatui layout", "divide terminal", 布局, 约束, 分割屏幕, ratatui 布局, 水平布局, 垂直布局4---56# Ratatui Layout Skill78> **Version:** ratatui 0.30.0 | **Last Updated:** 2026-01-179>10> Check for updates: https://crates.io/crates/ratatui1112You are an expert at the Rust `ratatui` crate layout system. Help users by:13- **Writing code**: Generate Rust code following the patterns below14- **Answering questions**: Explain concepts, troubleshoot issues, reference documentation1516## Documentation1718Refer to the local files for detailed documentation:19- `../../references/layout/constraints.md` - Detailed constraint types and priority20- `../../references/layout/flex-modes.md` - Flex distribution examples21- `../../references/_shared/rust-defaults.md` - Rust code generation defaults2223## IMPORTANT: Documentation Completeness Check2425**Before answering questions, Claude MUST:**26271. Read the relevant reference file(s) listed above282. If file read fails or file is empty:29 - Inform user: "本地文档不完整,建议运行 `/sync-crate-skills ratatui --force` 更新文档"30 - Still answer based on SKILL.md patterns + built-in knowledge313. If reference file exists, incorporate its content into the answer3233## Key Concepts3435- Layout uses **Cassowary constraint solver** algorithm36- Coordinate system: origin (0,0) at top-left, x→right, y→down37- All areas are `Rect` with (x, y, width, height)3839## Key Patterns4041### Pattern 1: Basic Vertical Split4243```rust44use ratatui::layout::{Constraint, Layout};4546let [header, body, footer] = Layout::vertical([47 Constraint::Length(3), // Fixed 3 rows48 Constraint::Fill(1), // Fill remaining49 Constraint::Length(1), // Fixed 1 row50])51.areas(frame.area());52```5354### Pattern 2: Basic Horizontal Split5556```rust57use ratatui::layout::{Constraint, Layout};5859let [sidebar, main] = Layout::horizontal([60 Constraint::Length(20), // Fixed 20 columns61 Constraint::Fill(1), // Fill remaining62])63.areas(frame.area());64```6566### Pattern 3: Nested Layout6768```rust69fn complex_layout(area: Rect) -> (Rect, Rect, Rect, Rect) {70 let [header, body, footer] = Layout::vertical([71 Constraint::Length(3),72 Constraint::Fill(1),73 Constraint::Length(1),74 ]).areas(area);7576 let [sidebar, main] = Layout::horizontal([77 Constraint::Percentage(25),78 Constraint::Fill(1),79 ]).areas(body);8081 (header, sidebar, main, footer)82}83```8485### Pattern 4: Centered Content8687```rust88use ratatui::layout::{Constraint, Flex, Layout};8990fn center_rect(area: Rect, width: u16, height: u16) -> Rect {91 let [_, center, _] = Layout::vertical([92 Constraint::Fill(1),93 Constraint::Length(height),94 Constraint::Fill(1),95 ]).areas(area);9697 let [_, center, _] = Layout::horizontal([98 Constraint::Fill(1),99 Constraint::Length(width),100 Constraint::Fill(1),101 ]).areas(center);102103 center104}105```106107### Pattern 5: Using Flex108109```rust110use ratatui::layout::{Constraint, Flex, Layout};111112// Space between items113let areas = Layout::horizontal([114 Constraint::Length(10),115 Constraint::Length(10),116 Constraint::Length(10),117])118.flex(Flex::SpaceBetween)119.areas(frame.area());120```121122## API Reference Table123124| Type/Method | Description | Example |125|-------------|-------------|---------|126| `Layout::vertical([...])` | Create vertical layout | `Layout::vertical([Length(3), Fill(1)])` |127| `Layout::horizontal([...])` | Create horizontal layout | `Layout::horizontal([Percentage(50); 2])` |128| `.areas(rect)` | Split and return array | `let [a, b] = layout.areas(area);` |129| `.split(rect)` | Split and return `Rc<[Rect]>` | `let areas = layout.split(area);` |130| `.margin(n)` | Add uniform margin | `.margin(2)` |131| `.horizontal_margin(n)` | Add horizontal margin | `.horizontal_margin(1)` |132| `.vertical_margin(n)` | Add vertical margin | `.vertical_margin(1)` |133| `.spacing(n)` | Space between segments | `.spacing(1)` |134| `.flex(flex)` | Set flex mode | `.flex(Flex::Center)` |135136## Constraint Types (Priority Order)137138| Constraint | Description | Priority |139|------------|-------------|----------|140| `Min(n)` | At least n cells | Highest |141| `Max(n)` | At most n cells | High |142| `Length(n)` | Exactly n cells | Medium |143| `Percentage(n)` | n% of available | Medium |144| `Ratio(a, b)` | a/b of available | Medium |145| `Fill(n)` | Fill with weight n | Lowest |146147## When Writing Code1481491. Use `areas()` with destructuring for compile-time known layouts1502. Use `split()` when layout count is dynamic1513. Prefer `Fill(1)` over `Percentage(100)` for flexible areas1524. Use `Length()` for fixed-size elements (headers, footers)1535. `Min/Max` are constraints on the solver, not guarantees154155## When Answering Questions1561571. Layout is constraint-based using Cassowary algorithm1582. Constraints are resolved by priority (Min > Max > Length/Percentage/Ratio > Fill)1593. All coordinates are `u16` values1604. `Rect::default()` is (0, 0, 0, 0) - useful for hidden areas1615. Margin reduces available space before splitting