Skip to content

Commit f607990

Browse files
committed
fix: #2148 add comma to group three digits in numbers
1 parent ee8c4ad commit f607990

File tree

4 files changed

+41
-19
lines changed

4 files changed

+41
-19
lines changed

codex-rs/exec/src/event_processor_with_human_output.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use codex_core::protocol::SessionConfiguredEvent;
2323
use codex_core::protocol::TaskCompleteEvent;
2424
use codex_core::protocol::TurnAbortReason;
2525
use codex_core::protocol::TurnDiffEvent;
26+
use codex_core::protocol::format_token_count;
2627
use owo_colors::OwoColorize;
2728
use owo_colors::Style;
2829
use shlex::try_join;
@@ -184,7 +185,8 @@ impl EventProcessor for EventProcessorWithHumanOutput {
184185
return CodexStatus::InitiateShutdown;
185186
}
186187
EventMsg::TokenCount(token_usage) => {
187-
ts_println!(self, "tokens used: {}", token_usage.blended_total());
188+
let tokens = format_token_count(token_usage.blended_total());
189+
ts_println!(self, "tokens used: {tokens}");
188190
}
189191
EventMsg::AgentMessageDelta(AgentMessageDeltaEvent { delta }) => {
190192
if !self.answer_started {

codex-rs/protocol/src/protocol.rs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,17 @@ impl TokenUsage {
545545
}
546546
}
547547

548+
/// Format a token count with thousands separators for readability.
549+
pub fn format_token_count(value: u64) -> String {
550+
let mut s = value.to_string();
551+
let mut i = s.len();
552+
while i > 3 {
553+
i -= 3;
554+
s.insert(i, ',');
555+
}
556+
s
557+
}
558+
548559
#[derive(Debug, Clone, Deserialize, Serialize)]
549560
pub struct FinalOutput {
550561
pub token_usage: TokenUsage,
@@ -559,21 +570,26 @@ impl From<TokenUsage> for FinalOutput {
559570
impl fmt::Display for FinalOutput {
560571
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
561572
let token_usage = &self.token_usage;
573+
let total = format_token_count(token_usage.blended_total());
574+
let input = format_token_count(token_usage.non_cached_input());
575+
let cached = token_usage.cached_input();
576+
let cached_str = if cached > 0 {
577+
let cached_tokens = format_token_count(cached);
578+
format!(" (+ {cached_tokens} cached)")
579+
} else {
580+
String::new()
581+
};
582+
let output = format_token_count(token_usage.output_tokens);
583+
let reasoning = token_usage
584+
.reasoning_output_tokens
585+
.map(|r| {
586+
let reasoning_tokens = format_token_count(r);
587+
format!(" (reasoning {reasoning_tokens})")
588+
})
589+
.unwrap_or_default();
562590
write!(
563591
f,
564-
"Token usage: total={} input={}{} output={}{}",
565-
token_usage.blended_total(),
566-
token_usage.non_cached_input(),
567-
if token_usage.cached_input() > 0 {
568-
format!(" (+ {} cached)", token_usage.cached_input())
569-
} else {
570-
String::new()
571-
},
572-
token_usage.output_tokens,
573-
token_usage
574-
.reasoning_output_tokens
575-
.map(|r| format!(" (reasoning {r})"))
576-
.unwrap_or_default()
592+
"Token usage: total={total} input={input}{cached_str} output={output}{reasoning}"
577593
)
578594
}
579595
}

codex-rs/tui/src/bottom_pane/chat_composer.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use codex_core::protocol::TokenUsage;
2+
use codex_core::protocol::format_token_count;
23
use crossterm::event::KeyCode;
34
use crossterm::event::KeyEvent;
45
use crossterm::event::KeyModifiers;
@@ -683,8 +684,9 @@ impl WidgetRef for &ChatComposer {
683684
if let Some(token_usage_info) = &self.token_usage_info {
684685
let token_usage = &token_usage_info.total_token_usage;
685686
hint.push(Span::from(" "));
687+
let tokens = format_token_count(token_usage.blended_total());
686688
hint.push(
687-
Span::from(format!("{} tokens used", token_usage.blended_total()))
689+
Span::from(format!("{tokens} tokens used"))
688690
.style(Style::default().add_modifier(Modifier::DIM)),
689691
);
690692
let last_token_usage = &token_usage_info.last_token_usage;

codex-rs/tui/src/history_cell.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use codex_core::protocol::McpInvocation;
1616
use codex_core::protocol::SandboxPolicy;
1717
use codex_core::protocol::SessionConfiguredEvent;
1818
use codex_core::protocol::TokenUsage;
19+
use codex_core::protocol::format_token_count;
1920
use codex_login::get_auth_file;
2021
use codex_login::try_read_auth_json;
2122
use codex_protocol::parse_command::ParsedCommand;
@@ -611,23 +612,24 @@ pub(crate) fn new_status_output(
611612
// Input: <input> [+ <cached> cached]
612613
let mut input_line_spans: Vec<Span<'static>> = vec![
613614
" • Input: ".into(),
614-
usage.non_cached_input().to_string().into(),
615+
format_token_count(usage.non_cached_input()).into(),
615616
];
616617
if let Some(cached) = usage.cached_input_tokens
617618
&& cached > 0
618619
{
619-
input_line_spans.push(format!(" (+ {cached} cached)").into());
620+
let cached_tokens = format_token_count(cached);
621+
input_line_spans.push(format!(" (+ {cached_tokens} cached)").into());
620622
}
621623
lines.push(Line::from(input_line_spans));
622624
// Output: <output>
623625
lines.push(Line::from(vec![
624626
" • Output: ".into(),
625-
usage.output_tokens.to_string().into(),
627+
format_token_count(usage.output_tokens).into(),
626628
]));
627629
// Total: <total>
628630
lines.push(Line::from(vec![
629631
" • Total: ".into(),
630-
usage.blended_total().to_string().into(),
632+
format_token_count(usage.blended_total()).into(),
631633
]));
632634

633635
lines.push(Line::from(""));

0 commit comments

Comments
 (0)