Skip to content

Commit 4df9b56

Browse files
committed
fix: #2148 add comma to group three digits in numbers
1 parent 8b49346 commit 4df9b56

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
@@ -25,6 +25,7 @@ use codex_core::protocol::TaskCompleteEvent;
2525
use codex_core::protocol::TurnAbortReason;
2626
use codex_core::protocol::TurnDiffEvent;
2727
use codex_core::protocol::WebSearchBeginEvent;
28+
use codex_core::protocol::format_token_count;
2829
use owo_colors::OwoColorize;
2930
use owo_colors::Style;
3031
use shlex::try_join;
@@ -189,7 +190,8 @@ impl EventProcessor for EventProcessorWithHumanOutput {
189190
return CodexStatus::InitiateShutdown;
190191
}
191192
EventMsg::TokenCount(token_usage) => {
192-
ts_println!(self, "tokens used: {}", token_usage.blended_total());
193+
let tokens = format_token_count(token_usage.blended_total());
194+
ts_println!(self, "tokens used: {tokens}");
193195
}
194196
EventMsg::AgentMessageDelta(AgentMessageDeltaEvent { delta }) => {
195197
if !self.answer_started {

codex-rs/protocol/src/protocol.rs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,17 @@ impl TokenUsage {
558558
}
559559
}
560560

561+
/// Format a token count with thousands separators for readability.
562+
pub fn format_token_count(value: u64) -> String {
563+
let mut s = value.to_string();
564+
let mut i = s.len();
565+
while i > 3 {
566+
i -= 3;
567+
s.insert(i, ',');
568+
}
569+
s
570+
}
571+
561572
#[derive(Debug, Clone, Deserialize, Serialize)]
562573
pub struct FinalOutput {
563574
pub token_usage: TokenUsage,
@@ -572,21 +583,26 @@ impl From<TokenUsage> for FinalOutput {
572583
impl fmt::Display for FinalOutput {
573584
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
574585
let token_usage = &self.token_usage;
586+
let total = format_token_count(token_usage.blended_total());
587+
let input = format_token_count(token_usage.non_cached_input());
588+
let cached = token_usage.cached_input();
589+
let cached_str = if cached > 0 {
590+
let cached_tokens = format_token_count(cached);
591+
format!(" (+ {cached_tokens} cached)")
592+
} else {
593+
String::new()
594+
};
595+
let output = format_token_count(token_usage.output_tokens);
596+
let reasoning = token_usage
597+
.reasoning_output_tokens
598+
.map(|r| {
599+
let reasoning_tokens = format_token_count(r);
600+
format!(" (reasoning {reasoning_tokens})")
601+
})
602+
.unwrap_or_default();
575603
write!(
576604
f,
577-
"Token usage: total={} input={}{} output={}{}",
578-
token_usage.blended_total(),
579-
token_usage.non_cached_input(),
580-
if token_usage.cached_input() > 0 {
581-
format!(" (+ {} cached)", token_usage.cached_input())
582-
} else {
583-
String::new()
584-
},
585-
token_usage.output_tokens,
586-
token_usage
587-
.reasoning_output_tokens
588-
.map(|r| format!(" (reasoning {r})"))
589-
.unwrap_or_default()
605+
"Token usage: total={total} input={input}{cached_str} output={output}{reasoning}"
590606
)
591607
}
592608
}

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;
@@ -1153,8 +1154,9 @@ impl WidgetRef for &ChatComposer {
11531154
if let Some(token_usage_info) = &self.token_usage_info {
11541155
let token_usage = &token_usage_info.total_token_usage;
11551156
hint.push(Span::from(" "));
1157+
let tokens = format_token_count(token_usage.blended_total());
11561158
hint.push(
1157-
Span::from(format!("{} tokens used", token_usage.blended_total()))
1159+
Span::from(format!("{tokens} tokens used"))
11581160
.style(Style::default().add_modifier(Modifier::DIM)),
11591161
);
11601162
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
@@ -18,6 +18,7 @@ use codex_core::protocol::McpInvocation;
1818
use codex_core::protocol::SandboxPolicy;
1919
use codex_core::protocol::SessionConfiguredEvent;
2020
use codex_core::protocol::TokenUsage;
21+
use codex_core::protocol::format_token_count;
2122
use codex_login::get_auth_file;
2223
use codex_login::try_read_auth_json;
2324
use codex_protocol::parse_command::ParsedCommand;
@@ -734,23 +735,24 @@ pub(crate) fn new_status_output(
734735
// Input: <input> [+ <cached> cached]
735736
let mut input_line_spans: Vec<Span<'static>> = vec![
736737
" • Input: ".into(),
737-
usage.non_cached_input().to_string().into(),
738+
format_token_count(usage.non_cached_input()).into(),
738739
];
739740
if let Some(cached) = usage.cached_input_tokens
740741
&& cached > 0
741742
{
742-
input_line_spans.push(format!(" (+ {cached} cached)").into());
743+
let cached_tokens = format_token_count(cached);
744+
input_line_spans.push(format!(" (+ {cached_tokens} cached)").into());
743745
}
744746
lines.push(Line::from(input_line_spans));
745747
// Output: <output>
746748
lines.push(Line::from(vec![
747749
" • Output: ".into(),
748-
usage.output_tokens.to_string().into(),
750+
format_token_count(usage.output_tokens).into(),
749751
]));
750752
// Total: <total>
751753
lines.push(Line::from(vec![
752754
" • Total: ".into(),
753-
usage.blended_total().to_string().into(),
755+
format_token_count(usage.blended_total()).into(),
754756
]));
755757

756758
PlainHistoryCell { lines }

0 commit comments

Comments
 (0)