11use super :: be_http_client;
22use crate :: config:: Config ;
33use crate :: error:: Result ;
4+ use crate :: tools:: common:: format_utils;
45use crate :: tools:: { ExecutionResult , Tool } ;
56use crate :: ui;
67use chrono:: Utc ;
@@ -102,23 +103,6 @@ impl Tool for MemzGlobalTool {
102103 }
103104}
104105
105- /// Format bytes to a human-readable string
106- fn format_bytes ( bytes : u64 ) -> String {
107- const KB : u64 = 1024 ;
108- const MB : u64 = KB * 1024 ;
109- const GB : u64 = MB * 1024 ;
110-
111- if bytes >= GB {
112- format ! ( "{:.2} GB ({bytes} bytes)" , bytes as f64 / GB as f64 )
113- } else if bytes >= MB {
114- format ! ( "{:.2} MB ({bytes} bytes)" , bytes as f64 / MB as f64 )
115- } else if bytes >= KB {
116- format ! ( "{:.2} KB ({bytes} bytes)" , bytes as f64 / KB as f64 )
117- } else {
118- format ! ( "{bytes} bytes" )
119- }
120- }
121-
122106/// Extract memory metrics from the HTML response
123107fn extract_memory_metrics ( html_content : & str ) -> ( String , String ) {
124108 let re = Regex :: new ( r"Allocated: (\d+), active: (\d+), metadata: (\d+).*?, resident: (\d+), mapped: (\d+), retained: (\d+)" ) . unwrap ( ) ;
@@ -141,27 +125,27 @@ fn extract_memory_metrics(html_content: &str) -> (String, String) {
141125 {
142126 let caps = re. captures ( html_content) . unwrap ( ) ;
143127 if let Some ( bytes) = caps. get ( 1 ) . and_then ( |m| m. as_str ( ) . parse :: < u64 > ( ) . ok ( ) ) {
144- allocated = format_bytes ( bytes) ;
128+ allocated = format_utils :: format_bytes ( bytes, 2 , true ) ;
145129 }
146130
147131 if let Some ( bytes) = caps. get ( 2 ) . and_then ( |m| m. as_str ( ) . parse :: < u64 > ( ) . ok ( ) ) {
148- active = format_bytes ( bytes) ;
132+ active = format_utils :: format_bytes ( bytes, 2 , true ) ;
149133 }
150134
151135 if let Some ( bytes) = caps. get ( 3 ) . and_then ( |m| m. as_str ( ) . parse :: < u64 > ( ) . ok ( ) ) {
152- metadata = format_bytes ( bytes) ;
136+ metadata = format_utils :: format_bytes ( bytes, 2 , true ) ;
153137 }
154138
155139 if let Some ( bytes) = caps. get ( 4 ) . and_then ( |m| m. as_str ( ) . parse :: < u64 > ( ) . ok ( ) ) {
156- resident = format_bytes ( bytes) ;
140+ resident = format_utils :: format_bytes ( bytes, 2 , true ) ;
157141 }
158142
159143 if let Some ( bytes) = caps. get ( 5 ) . and_then ( |m| m. as_str ( ) . parse :: < u64 > ( ) . ok ( ) ) {
160- mapped = format_bytes ( bytes) ;
144+ mapped = format_utils :: format_bytes ( bytes, 2 , true ) ;
161145 }
162146
163147 if let Some ( bytes) = caps. get ( 6 ) . and_then ( |m| m. as_str ( ) . parse :: < u64 > ( ) . ok ( ) ) {
164- retained = format_bytes ( bytes) ;
148+ retained = format_utils :: format_bytes ( bytes, 2 , true ) ;
165149 }
166150 }
167151
@@ -170,15 +154,15 @@ fn extract_memory_metrics(html_content: &str) -> (String, String) {
170154 . and_then ( |caps| caps. get ( 1 ) )
171155 . and_then ( |m| m. as_str ( ) . parse :: < u64 > ( ) . ok ( ) )
172156 {
173- thread_cache = format_bytes ( bytes) ;
157+ thread_cache = format_utils :: format_bytes ( bytes, 2 , true ) ;
174158 }
175159
176160 if let Some ( bytes) = dirty_pages_re
177161 . captures ( html_content)
178162 . and_then ( |caps| caps. get ( 1 ) )
179163 . and_then ( |m| m. as_str ( ) . parse :: < u64 > ( ) . ok ( ) )
180164 {
181- dirty_pages = format_bytes ( bytes) ;
165+ dirty_pages = format_utils :: format_bytes ( bytes, 2 , true ) ;
182166 }
183167
184168 let table = format ! (
0 commit comments