mirror of
https://github.com/openai/codex.git
synced 2026-11-25 23:30:49 +00:00
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
This commit is contained in:
+11
-1
@@ -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<Element | null>, } | { "type": "link", props: LinkProps, children: Array<Element | null>, } | { "type": "row", props: RowProps, children: Array<Element | null>, } | { "type": "col", props: ColProps, children: Array<Element | null>, } | { "type": "bold", children: Array<Element | null>, } | { "type": "italic", children: Array<Element | null>, } | { "type": "underline", children: Array<Element | null>, } | { "type": "code", children: Array<Element | null>, } | string;
|
||||
export type Element = { "type": "text", props?: TextProps, children: Array<Element | null>, } | { "type": "link", props: LinkProps, children: Array<Element | null>, } | { "type": "row", props: RowProps, children: Array<Element | null>, } | { "type": "box", props: BoxProps, children: Array<Element | null>, } | { "type": "col", props: ColProps, children: Array<Element | null>, } | { "type": "bold", children: Array<Element | null>, } | { "type": "italic", children: Array<Element | null>, } | { "type": "underline", children: Array<Element | null>, } | { "type": "code", children: Array<Element | null>, } | string;
|
||||
|
||||
export type FileSystem = { "type": "restricted" } | { "type": "unrestricted" };
|
||||
|
||||
@@ -114,7 +122,9 @@ export type UnderlineElement = Extract<Element, { type: "underline" }>;
|
||||
export type LinkElement = Extract<Element, { type: "link" }>;
|
||||
export type RowElement = Extract<Element, { type: "row" }>;
|
||||
export type ColElement = Extract<Element, { type: "col" }>;
|
||||
export type BoxElement = Extract<Element, { type: "box" }>;
|
||||
export type CodeElement = Extract<Element, { type: "code" }>;
|
||||
type BoxColumnProps = ColProps & { [K in Exclude<keyof RowProps, keyof ColProps>]?: never };
|
||||
export type RateLimitWindow = {
|
||||
/**
|
||||
* Percentage of the allowance consumed during the window. Can exceed 100.
|
||||
|
||||
@@ -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<String> {
|
||||
"Link",
|
||||
"Row",
|
||||
"Col",
|
||||
"Box",
|
||||
"Code",
|
||||
] {
|
||||
let tag = name.to_ascii_lowercase();
|
||||
@@ -77,6 +79,8 @@ fn generate_reference(output: &Path) -> color_eyre::Result<String> {
|
||||
)?;
|
||||
}
|
||||
|
||||
// Column boxes must exclude row-only props even when direction is dynamic.
|
||||
generated.push_str("type BoxColumnProps = ColProps & { [K in Exclude<keyof RowProps, keyof ColProps>]?: never };\n");
|
||||
writeln!(generated, "export {}", RateLimitWindow::decl())?;
|
||||
generated.push_str(
|
||||
"export type ThemeColor = Extract<Color, \"primary\" | \"surface\" | \"accent\">;\n\
|
||||
|
||||
@@ -47,6 +47,10 @@ pub(crate) enum UiNode {
|
||||
props: RowProps,
|
||||
children: Vec<Option<UiNode>>,
|
||||
},
|
||||
Box {
|
||||
props: BoxProps,
|
||||
children: Vec<Option<UiNode>>,
|
||||
},
|
||||
Col {
|
||||
props: ColProps,
|
||||
children: Vec<Option<UiNode>>,
|
||||
@@ -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"))]
|
||||
|
||||
@@ -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)));
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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"] }],
|
||||
|
||||
Reference in New Issue
Block a user