11use std:: {
22 env,
3+ fmt:: Write as _,
34 fs:: File ,
45 io:: { self , Read } ,
56} ;
@@ -12,27 +13,40 @@ use crate::colors::COLORS;
1213#[ cfg_attr( feature = "hotpath" , hotpath:: measure) ]
1314pub fn get_username_and_hostname ( utsname : & UtsName ) -> String {
1415 let username = env:: var ( "USER" ) . unwrap_or_else ( |_| "unknown_user" . to_owned ( ) ) ;
15- let hostname = utsname
16- . nodename ( )
17- . to_str ( )
18- . unwrap_or ( "unknown_host" )
19- . to_owned ( ) ;
20- format ! (
21- "{yellow}{username}{red}@{green}{hostname}{reset}" ,
22- yellow = COLORS . yellow,
23- red = COLORS . red,
24- green = COLORS . green,
25- reset = COLORS . reset,
26- )
16+ let hostname = utsname. nodename ( ) . to_str ( ) . unwrap_or ( "unknown_host" ) ;
17+
18+ let capacity = COLORS . yellow . len ( )
19+ + username. len ( )
20+ + COLORS . red . len ( )
21+ + 1
22+ + COLORS . green . len ( )
23+ + hostname. len ( )
24+ + COLORS . reset . len ( ) ;
25+ let mut result = String :: with_capacity ( capacity) ;
26+
27+ result. push_str ( COLORS . yellow ) ;
28+ result. push_str ( & username) ;
29+ result. push_str ( COLORS . red ) ;
30+ result. push ( '@' ) ;
31+ result. push_str ( COLORS . green ) ;
32+ result. push_str ( hostname) ;
33+ result. push_str ( COLORS . reset ) ;
34+
35+ result
2736}
2837
2938#[ must_use]
3039#[ cfg_attr( feature = "hotpath" , hotpath:: measure) ]
3140pub fn get_shell ( ) -> String {
3241 let shell_path =
3342 env:: var ( "SHELL" ) . unwrap_or_else ( |_| "unknown_shell" . to_owned ( ) ) ;
34- let shell_name = shell_path. rsplit ( '/' ) . next ( ) . unwrap_or ( "unknown_shell" ) ;
35- shell_name. to_owned ( )
43+
44+ // Find last '/' and get the part after it, avoiding allocation
45+ shell_path
46+ . rsplit ( '/' )
47+ . next ( )
48+ . unwrap_or ( "unknown_shell" )
49+ . to_owned ( )
3650}
3751
3852#[ cfg_attr( feature = "hotpath" , hotpath:: measure) ]
@@ -49,11 +63,16 @@ pub fn get_root_disk_usage() -> Result<String, io::Error> {
4963 let used_size = used_size as f64 / ( 1024.0 * 1024.0 * 1024.0 ) ;
5064 let usage = ( used_size / total_size) * 100.0 ;
5165
52- Ok ( format ! (
66+ let mut result = String :: with_capacity ( 64 ) ;
67+ write ! (
68+ result,
5369 "{used_size:.2} GiB / {total_size:.2} GiB ({cyan}{usage:.0}%{reset})" ,
5470 cyan = COLORS . cyan,
5571 reset = COLORS . reset,
56- ) )
72+ )
73+ . unwrap ( ) ;
74+
75+ Ok ( result)
5776}
5877
5978#[ cfg_attr( feature = "hotpath" , hotpath:: measure) ]
@@ -70,7 +89,7 @@ pub fn get_memory_usage() -> Result<String, io::Error> {
7089 let mut split = line. split_whitespace ( ) ;
7190 match split. next ( ) . unwrap_or_default ( ) {
7291 "MemTotal:" => {
73- total_memory_kb = split. next ( ) . unwrap_or ( "0" ) . parse ( ) . unwrap_or ( 0.0 )
92+ total_memory_kb = split. next ( ) . unwrap_or ( "0" ) . parse ( ) . unwrap_or ( 0.0 ) ;
7493 } ,
7594 "MemAvailable:" => {
7695 available_memory_kb =
@@ -92,10 +111,15 @@ pub fn get_memory_usage() -> Result<String, io::Error> {
92111 let ( used_memory, total_memory) = parse_memory_info ( ) ?;
93112 let percentage_used = ( used_memory / total_memory * 100.0 ) . round ( ) as u64 ;
94113
95- Ok ( format ! (
114+ let mut result = String :: with_capacity ( 64 ) ;
115+ write ! (
116+ result,
96117 "{used_memory:.2} GiB / {total_memory:.2} GiB \
97118 ({cyan}{percentage_used}%{reset})",
98119 cyan = COLORS . cyan,
99120 reset = COLORS . reset,
100- ) )
121+ )
122+ . unwrap ( ) ;
123+
124+ Ok ( result)
101125}
0 commit comments