From 163c555f18b7616b40f7466c039ec5614a787b46 Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Wed, 25 Nov 2026 19:40:36 +0000 Subject: [PATCH] Add a directional `box` element for custom status lines (#64008) ## What changed Custom status lines can choose between row and column layouts by setting `direction` on a `box`. Omitting `direction` stacks children in a column. Both layouts require `gap`, and only rows accept `wrap` and `justify`. Horizontal boxes group adjacent strings into one layout item even when `justify` is omitted. Use separate elements to insert a gap between text items. ## Testing Add renderer tests for default column layout, wrapping and alignment in rows, and spacing around empty boxes. Extend the TypeScript contract checks to cover dynamic direction, required gaps, and rejection of row-only props on columns. GitOrigin-RevId: 3ff9b4a8be6ae79b90512c1ad1ec1c16dafad0cf --- .../references/statusline.d.ts | 12 +++++- .../chatwidget/statusline_contract_tests.rs | 4 ++ codex-rs/tui/src/ui_tree.rs | 22 ++++++++++ codex-rs/tui/src/ui_tree/decode.rs | 1 + codex-rs/tui/src/ui_tree/render/prepare.rs | 32 ++++++++++++--- codex-rs/tui/src/ui_tree_tests.rs | 41 +++++++++++++++++++ .../fixtures/statusline/contract.check.ts | 17 ++++++++ 7 files changed, 122 insertions(+), 7 deletions(-) diff --git a/codex-rs/skills/src/assets/samples/create-statusline/references/statusline.d.ts b/codex-rs/skills/src/assets/samples/create-statusline/references/statusline.d.ts index fdcf8f8575..2a976d3966 100644 --- a/codex-rs/skills/src/assets/samples/create-statusline/references/statusline.d.ts +++ b/codex-rs/skills/src/assets/samples/create-statusline/references/statusline.d.ts @@ -14,6 +14,12 @@ export type ApprovalPolicy = "untrusted" | "on-request" | "granular" | "never"; export type ApprovalReviewer = "user" | "auto-review"; +/** + * A box defaults to a column when direction is omitted. + * Horizontal boxes group adjacent strings even without justify. Use separate elements for a gap. + */ +export type BoxProps = DirectedBox | BoxColumnProps & { direction?: never }; + export type ColProps = { /** * Blank terminal rows between flex items, as a non-negative JavaScript-safe integer. @@ -29,9 +35,11 @@ export type Context = { context_window_size?: number, used_tokens?: number, used export type Cost = { total_cost_usd?: number, total_credits?: number, }; +export type DirectedBox = { "direction": "row" } & RowProps | { "direction": "col" } & BoxColumnProps; + export type DisplayMode = "standard" | "raw"; -export type Element = { "type": "text", props?: TextProps, children: Array, } | { "type": "link", props: LinkProps, children: Array, } | { "type": "row", props: RowProps, children: Array, } | { "type": "col", props: ColProps, children: Array, } | { "type": "bold", children: Array, } | { "type": "italic", children: Array, } | { "type": "underline", children: Array, } | { "type": "code", children: Array, } | string; +export type Element = { "type": "text", props?: TextProps, children: Array, } | { "type": "link", props: LinkProps, children: Array, } | { "type": "row", props: RowProps, children: Array, } | { "type": "box", props: BoxProps, children: Array, } | { "type": "col", props: ColProps, children: Array, } | { "type": "bold", children: Array, } | { "type": "italic", children: Array, } | { "type": "underline", children: Array, } | { "type": "code", children: Array, } | string; export type FileSystem = { "type": "restricted" } | { "type": "unrestricted" }; @@ -114,7 +122,9 @@ export type UnderlineElement = Extract; export type LinkElement = Extract; export type RowElement = Extract; export type ColElement = Extract; +export type BoxElement = Extract; export type CodeElement = Extract; +type BoxColumnProps = ColProps & { [K in Exclude]?: never }; export type RateLimitWindow = { /** * Percentage of the allowance consumed during the window. Can exceed 100. diff --git a/codex-rs/tui/src/chatwidget/statusline_contract_tests.rs b/codex-rs/tui/src/chatwidget/statusline_contract_tests.rs index 2a4046897b..aa7d26df97 100644 --- a/codex-rs/tui/src/chatwidget/statusline_contract_tests.rs +++ b/codex-rs/tui/src/chatwidget/statusline_contract_tests.rs @@ -29,6 +29,7 @@ fn statusline_typescript_contract_matches_the_bundled_reference() -> color_eyre: "UiTree", "RowElement", "ColElement", + "BoxElement", "CodeElement", "StatuslineItemId", "Color", @@ -68,6 +69,7 @@ fn generate_reference(output: &Path) -> color_eyre::Result { "Link", "Row", "Col", + "Box", "Code", ] { let tag = name.to_ascii_lowercase(); @@ -77,6 +79,8 @@ fn generate_reference(output: &Path) -> color_eyre::Result { )?; } + // Column boxes must exclude row-only props even when direction is dynamic. + generated.push_str("type BoxColumnProps = ColProps & { [K in Exclude]?: never };\n"); writeln!(generated, "export {}", RateLimitWindow::decl())?; generated.push_str( "export type ThemeColor = Extract;\n\ diff --git a/codex-rs/tui/src/ui_tree.rs b/codex-rs/tui/src/ui_tree.rs index fb1796771f..4891229c63 100644 --- a/codex-rs/tui/src/ui_tree.rs +++ b/codex-rs/tui/src/ui_tree.rs @@ -47,6 +47,10 @@ pub(crate) enum UiNode { props: RowProps, children: Vec>, }, + Box { + props: BoxProps, + children: Vec>, + }, Col { props: ColProps, children: Vec>, @@ -68,6 +72,24 @@ pub(crate) enum UiNode { String(String), } +/// A box defaults to a column when direction is omitted. +/// Horizontal boxes group adjacent strings even without justify. Use separate elements for a gap. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(untagged)] +#[cfg_attr(test, derive(ts_rs::TS), ts(export_to = "statusline.d.ts"))] +pub(crate) enum BoxProps { + Directed(DirectedBox), + DefaultColumn(#[cfg_attr(test, ts(type = "BoxColumnProps & { direction?: never }"))] ColProps), +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(tag = "direction", rename_all = "lowercase")] +#[cfg_attr(test, derive(ts_rs::TS), ts(export_to = "statusline.d.ts"))] +pub(crate) enum DirectedBox { + Row(RowProps), + Col(#[cfg_attr(test, ts(type = "BoxColumnProps"))] ColProps), +} + #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] #[cfg_attr(test, derive(ts_rs::TS), ts(export_to = "statusline.d.ts"))] diff --git a/codex-rs/tui/src/ui_tree/decode.rs b/codex-rs/tui/src/ui_tree/decode.rs index f1606094cc..5c63bd09c6 100644 --- a/codex-rs/tui/src/ui_tree/decode.rs +++ b/codex-rs/tui/src/ui_tree/decode.rs @@ -43,6 +43,7 @@ impl std::str::FromStr for UiTree { | UiNode::Underline { children } | UiNode::Link { children, .. } | UiNode::Row { children, .. } + | UiNode::Box { children, .. } | UiNode::Col { children, .. } => { pending.extend(children.iter().flatten().map(|child| (child, depth + 1))); } diff --git a/codex-rs/tui/src/ui_tree/render/prepare.rs b/codex-rs/tui/src/ui_tree/render/prepare.rs index 6fab88254e..263e831fa9 100644 --- a/codex-rs/tui/src/ui_tree/render/prepare.rs +++ b/codex-rs/tui/src/ui_tree/render/prepare.rs @@ -3,6 +3,8 @@ use super::text::InlineText; use super::text::Paint; use super::text::PreparedText; +use crate::ui_tree::BoxProps; +use crate::ui_tree::DirectedBox; use crate::ui_tree::RowProps; use crate::ui_tree::RowWrap; use crate::ui_tree::UiNode; @@ -203,12 +205,7 @@ impl PreparedNode { child => { items.extend(Self::text(std::mem::take(&mut strings))); // An explicit empty element still separates flex gaps. - items.push( - Self::from_node(child, paint.clone()).unwrap_or_else(|| Self { - kind: NodeKind::Empty, - widths: Widths::default(), - }), - ); + items.push(Self::flex_item(child, paint)); } } } @@ -216,6 +213,13 @@ impl PreparedNode { items } + fn flex_item(child: UiNode, paint: &Paint) -> Self { + Self::from_node(child, paint.clone()).unwrap_or_else(|| Self { + kind: NodeKind::Empty, + widths: Widths::default(), + }) + } + fn collect_inline(flow: &mut Flow, inline: &mut InlineText, node: UiNode, mut paint: Paint) { let parent_preserves_spaces = paint.preserve_whitespace; let children = match node { @@ -233,6 +237,22 @@ impl PreparedNode { .remove_modifier(ratatui::style::Modifier::BOLD); children } + UiNode::Box { props, children } => { + let node = match props { + BoxProps::DefaultColumn(props) + | BoxProps::Directed(DirectedBox::Col(props)) => { + UiNode::Col { props, children } + } + BoxProps::Directed(DirectedBox::Row(mut props)) => { + // Boxes always use flex items, including anonymous runs of strings. + props.justify.get_or_insert_default(); + UiNode::Row { props, children } + } + }; + flow.finish_line(inline); + flow.blocks.push(Self::flex_item(node, &paint)); + return; + } UiNode::Col { props, children } => { flow.finish_line(inline); let children = Self::flex_items(children, &paint); diff --git a/codex-rs/tui/src/ui_tree_tests.rs b/codex-rs/tui/src/ui_tree_tests.rs index 0df2a6410c..2cf7d5684d 100644 --- a/codex-rs/tui/src/ui_tree_tests.rs +++ b/codex-rs/tui/src/ui_tree_tests.rs @@ -286,6 +286,47 @@ fn clamped_link_keeps_whole_graphemes_and_an_unlinked_marker() { ); } +#[test] +fn empty_horizontal_box_keeps_its_flex_slot_and_separates_text_blocks() { + let empty = json!({"type":"box","props":{"direction":"row","gap":0},"children":[]}); + let children = json!(["A", empty, "B"]); + for props in [json!({"gap":1}), json!({"gap":1,"justify":"start"})] { + let row = tree(json!({"type":"row","props":props,"children":children})); + assert_eq!(painted_rows(&row, Size::new(9, 1)), ["A B"]); + } + let block = tree(json!({"type":"text","children":children})); + assert_eq!(painted_rows(&block, Size::new(9, 3)), ["A", "B"]); +} + +#[test] +fn box_defaults_to_a_column_and_uses_row_options_in_row_direction() { + let make = |props: Value| json!({"type":"box","props":props,"children":["a","b",null,{"type":"text","children":["c"]}]}); + let bounds = Size::new(5, 4); + for props in [json!({"gap":1}), json!({"direction":"col","gap":1})] { + let value = make(props); + assert_eq!( + serde_json::to_value(parse(value.clone()).unwrap()).unwrap(), + value + ); + assert_eq!(painted_rows(&tree(value), bounds), ["ab", "", "c"]); + } + let row = make(json!({"direction":"row","gap":1,"wrap":"wrap","justify":"end"})); + assert_eq!( + painted_rows(&tree(row.clone()), Size::new(2, 3)), + ["ab", " c"] + ); + let nested = tree(json!({"type":"text","children":["x",row,"y"]})); + insta::assert_snapshot!(painted_rows(&nested, bounds).join("\n"), @"x\n ab c\ny"); + for props in [ + json!({}), + json!({"direction":"vertical","gap":0}), + json!({"direction":"col","gap":0,"justify":"end"}), + json!({"gap":-1}), + ] { + assert!(parse(make(props)).is_err()); + } +} + #[test] fn wrapped_code_keeps_a_leading_hyphen_with_the_hash() { let bounds = Size::new(8, 3); diff --git a/codex-rs/tui/tests/fixtures/statusline/contract.check.ts b/codex-rs/tui/tests/fixtures/statusline/contract.check.ts index 5dd9d341aa..f4ab6c26e2 100644 --- a/codex-rs/tui/tests/fixtures/statusline/contract.check.ts +++ b/codex-rs/tui/tests/fixtures/statusline/contract.check.ts @@ -1,6 +1,7 @@ import type { CodeElement, ColElement, + BoxElement, Color, RowElement, StatuslineItemId, @@ -48,6 +49,22 @@ const indented: UiTree = { type: "text", props: { preserveWhitespace: true }, ch const summary: UiTree = { type: "text", props: { maxLines: 2 }, children: ["build results"] }; +const stacked: BoxElement = { type: "box", props: { gap: 1 }, children: [summary, indented] }; +const horizontal: BoxElement = { ...stacked, props: { gap: 0, direction: "row", justify: "end" } }; +const responsive = (compact: boolean): BoxElement => ({ + ...stacked, + props: { gap: 1, direction: compact ? "col" : "row" }, +}); +const responsiveAlignment = (compact: boolean): BoxElement["props"] => + compact ? { gap: 1 } : { gap: 1, direction: "row", justify: "end" }; +// @ts-expect-error Omitted direction defaults to a column. +const implicitColumn: BoxElement["props"] = { gap: 1, justify: "end" }; +const invalidResponsive = (compact: boolean): BoxElement["props"] => + // @ts-expect-error The column branch cannot carry row-only props. + ({ gap: 1, direction: compact ? "col" : "row", wrap: "wrap" }); +// @ts-expect-error Boxes require a gap in either direction. +const missingBoxGap: BoxElement = { ...stacked, props: { direction: "col" } }; + const command: CodeElement = { type: "code", children: ["cargo ", null, { type: "bold", children: [" test"] }],