@@ -13,9 +13,7 @@ use std::{process, time::Duration};
1313use uucore:: error:: UResult ;
1414#[ cfg( target_os = "linux" ) ]
1515use uucore:: libc:: { sysconf, _SC_CLK_TCK} ;
16- use uucore:: uptime:: {
17- get_formatted_loadavg, get_formatted_nusers, get_formatted_time, get_uptime, UptimeError ,
18- } ;
16+ use uucore:: uptime:: { get_formatted_time, get_loadavg, get_nusers, get_uptime, UptimeError } ;
1917#[ cfg( target_os = "linux" ) ]
2018use uucore:: utmpx:: Utmpx ;
2119
@@ -210,6 +208,33 @@ pub fn format_uptime_procps(up_secs: i64) -> UResult<String> {
210208 Ok ( format ! ( "{day_str}{hour_min_str}" ) )
211209}
212210
211+ #[ inline]
212+ pub fn format_nusers ( nusers : usize ) -> String {
213+ match nusers {
214+ 0 => "0 user" . to_string ( ) ,
215+ 1 => "1 user" . to_string ( ) ,
216+ _ => format ! ( "{} users" , nusers) ,
217+ }
218+ }
219+
220+ #[ inline]
221+ pub fn get_formatted_nusers ( ) -> String {
222+ #[ cfg( not( target_os = "openbsd" ) ) ]
223+ return format_nusers ( get_nusers ( ) ) ;
224+
225+ #[ cfg( target_os = "openbsd" ) ]
226+ format_nusers ( get_nusers ( "/var/run/utmp" ) )
227+ }
228+
229+ #[ inline]
230+ pub fn get_formatted_loadavg ( ) -> UResult < String > {
231+ let loadavg = get_loadavg ( ) ?;
232+ Ok ( format ! (
233+ "load average: {:.2}, {:.2}, {:.2}" ,
234+ loadavg. 0 , loadavg. 1 , loadavg. 2
235+ ) )
236+ }
237+
213238#[ inline]
214239pub fn get_formatted_uptime_procps ( ) -> UResult < String > {
215240 let time_str = format_uptime_procps ( get_uptime ( None ) ?) ?;
@@ -406,7 +431,7 @@ mod tests {
406431 assert_eq ! (
407432 format_time( pre_formatted) . unwrap( ) ,
408433 td. format( "%a%d" ) . to_string( )
409- )
434+ ) ;
410435 }
411436
412437 #[ test]
@@ -431,7 +456,7 @@ mod tests {
431456 assert_eq ! (
432457 fs:: read_to_string( path) . unwrap( ) ,
433458 fetch_cmdline( pid) . unwrap( )
434- )
459+ ) ;
435460 }
436461
437462 #[ test]
@@ -441,7 +466,7 @@ mod tests {
441466 let f = fs:: read_to_string ( path) . unwrap ( ) ;
442467 let stat: Vec < & str > = f. split_whitespace ( ) . collect ( ) ;
443468 let term_num = stat[ 6 ] ;
444- assert_eq ! ( fetch_terminal_number( pid) . unwrap( ) . to_string( ) , term_num)
469+ assert_eq ! ( fetch_terminal_number( pid) . unwrap( ) . to_string( ) , term_num) ;
445470 }
446471
447472 #[ test]
@@ -455,7 +480,7 @@ mod tests {
455480 assert_eq ! (
456481 fetch_pcpu_time( pid) . unwrap( ) ,
457482 ( utime + stime) / get_clock_tick( ) as f64
458- )
483+ ) ;
459484 }
460485
461486 #[ test]
@@ -477,6 +502,6 @@ mod tests {
477502 let result = format_uptime_procps ( up_secs. 0 ) . unwrap ( ) ;
478503
479504 assert_eq ! ( result, up_secs. 1 ) ;
480- } )
505+ } ) ;
481506 }
482507}
0 commit comments