Skip to content

Commit 019bf12

Browse files
committed
fix(tui): color tool duration independently from token usage
1 parent d27c916 commit 019bf12

3 files changed

Lines changed: 126 additions & 8 deletions

File tree

‎crates/jcode-tui/src/tui/ui_messages.rs‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4496,10 +4496,14 @@ pub(crate) fn render_tool_message(
44964496
));
44974497
tool_line.push(Span::styled(")", Style::default().fg(dim_color())));
44984498
}
4499-
let token_suffix = Line::from(vec![
4500-
Span::styled(" · ", Style::default().fg(dim_color())),
4501-
Span::styled(token_badge.label, Style::default().fg(token_badge.color)),
4502-
]);
4499+
let mut usage_spans = vec![Span::styled(" · ", Style::default().fg(dim_color()))];
4500+
usage_spans.extend(tools_ui::tool_usage_spans(
4501+
token_badge.label,
4502+
token_badge.color,
4503+
msg.duration_secs,
4504+
&msg.content,
4505+
));
4506+
let token_suffix = Line::from(usage_spans);
45034507

45044508
let rendered_tool_line = super::truncate_line_preserving_suffix_to_width(
45054509
&Line::from(tool_line),

‎crates/jcode-tui/src/tui/ui_messages/tests.rs‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3455,6 +3455,83 @@ fn tool_usage_label_handles_missing_and_legacy_duration() {
34553455
);
34563456
}
34573457

3458+
#[test]
3459+
fn render_tool_message_colors_duration_independently_from_tokens() {
3460+
for (seconds, duration_color) in [
3461+
(0.42, rgb(118, 118, 118)),
3462+
(9.99, rgb(118, 118, 118)),
3463+
(10.0, rgb(214, 184, 92)),
3464+
(59.99, rgb(214, 184, 92)),
3465+
(60.0, rgb(224, 118, 118)),
3466+
] {
3467+
for content in ["ok".to_owned(), "x".repeat(48_000)] {
3468+
let mut msg = DisplayMessage::tool(
3469+
content,
3470+
crate::message::ToolCall {
3471+
id: "independent-metrics".into(),
3472+
name: "read".into(),
3473+
input: serde_json::json!({}),
3474+
intent: None,
3475+
thought_signature: None,
3476+
},
3477+
);
3478+
msg.duration_secs = Some(seconds);
3479+
let badge = tool_output_token_badge(&msg.content);
3480+
for width in [32, 120] {
3481+
let lines = render_tool_message(&msg, width, crate::config::DiffDisplayMode::Off);
3482+
let spans = &lines[0].spans;
3483+
let token_index = spans
3484+
.iter()
3485+
.position(|span| span.content == badge.label)
3486+
.expect("missing token span");
3487+
assert_eq!(spans[token_index].style.fg, Some(badge.color));
3488+
assert_eq!(spans[token_index - 1].content, ", ");
3489+
assert_eq!(spans[token_index - 2].style.fg, Some(duration_color));
3490+
assert!(lines[0].width() < width as usize);
3491+
}
3492+
}
3493+
}
3494+
}
3495+
3496+
#[test]
3497+
fn batch_subcall_colors_duration_independently_from_tokens() {
3498+
let tool = crate::message::ToolCall {
3499+
id: "timed-child".into(),
3500+
name: "read".into(),
3501+
input: serde_json::json!({}),
3502+
intent: None,
3503+
thought_signature: None,
3504+
};
3505+
for (timing, color) in [
3506+
("420ms", rgb(118, 118, 118)),
3507+
("10s", rgb(214, 184, 92)),
3508+
("1m", rgb(224, 118, 118)),
3509+
] {
3510+
let content = format!("[tool timing: duration={timing}] {}", "x".repeat(48_000));
3511+
let line = crate::tui::ui::tools_ui::render_batch_subcall_line(
3512+
&tool,
3513+
"✓",
3514+
rgb(100, 180, 100),
3515+
50,
3516+
Some(60),
3517+
Some(&content),
3518+
);
3519+
let duration = line
3520+
.spans
3521+
.iter()
3522+
.find(|span| span.content == timing)
3523+
.expect("missing duration span");
3524+
assert_eq!(duration.style.fg, Some(color));
3525+
let tokens = line
3526+
.spans
3527+
.iter()
3528+
.find(|span| span.content.ends_with(" tok"))
3529+
.expect("missing token span");
3530+
assert_eq!(tokens.style.fg, Some(rgb(224, 118, 118)));
3531+
assert!(line.width() <= 60);
3532+
}
3533+
}
3534+
34583535
#[test]
34593536
fn render_tool_message_colors_high_token_badge() {
34603537
let msg = DisplayMessage {

‎crates/jcode-tui/src/tui/ui_tools.rs‎

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,6 +1587,39 @@ pub(super) fn tool_usage_label(tokens: &str, seconds: Option<f32>, output: &str)
15871587
}
15881588
}
15891589

1590+
/// Style elapsed time independently from output token volume.
1591+
pub(super) fn tool_usage_spans(
1592+
label: String,
1593+
token_color: Color,
1594+
seconds: Option<f32>,
1595+
output: &str,
1596+
) -> Vec<Span<'static>> {
1597+
let duration = seconds
1598+
.filter(|seconds| seconds.is_finite() && *seconds >= 0.0)
1599+
.or_else(|| jcode_tui_messages::DisplayMessage::tool_duration_from_output(output));
1600+
let Some(seconds) = duration else {
1601+
return vec![Span::styled(label, Style::default().fg(token_color))];
1602+
};
1603+
let Some((duration_label, tokens)) = label.split_once(", ") else {
1604+
return vec![Span::styled(label, Style::default().fg(token_color))];
1605+
};
1606+
let duration_color = if seconds >= 60.0 {
1607+
rgb(224, 118, 118)
1608+
} else if seconds >= 10.0 {
1609+
rgb(214, 184, 92)
1610+
} else {
1611+
rgb(118, 118, 118)
1612+
};
1613+
vec![
1614+
Span::styled(
1615+
duration_label.to_owned(),
1616+
Style::default().fg(duration_color),
1617+
),
1618+
Span::styled(", ", Style::default().fg(dim_color())),
1619+
Span::styled(tokens.to_owned(), Style::default().fg(token_color)),
1620+
]
1621+
}
1622+
15901623
pub(super) fn render_batch_subcall_line(
15911624
tool: &ToolCall,
15921625
icon: &str,
@@ -1656,10 +1689,14 @@ pub(super) fn render_batch_subcall_line(
16561689
));
16571690
}
16581691
let token_suffix = token_badge.map(|(label, color)| {
1659-
Line::from(vec![
1660-
Span::styled(" · ", Style::default().fg(dim_color())),
1661-
Span::styled(label, Style::default().fg(color)),
1662-
])
1692+
let mut spans = vec![Span::styled(" · ", Style::default().fg(dim_color()))];
1693+
spans.extend(tool_usage_spans(
1694+
label,
1695+
color,
1696+
None,
1697+
output_content.unwrap_or_default(),
1698+
));
1699+
Line::from(spans)
16631700
});
16641701

16651702
if let (Some(max_width), Some(token_suffix)) = (max_width, token_suffix.as_ref()) {

0 commit comments

Comments
 (0)