Skip to content

Commit 7b77e2f

Browse files
committed
w, top: fix nusers and loadavg
1 parent 246b57f commit 7b77e2f

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

src/uu/top/src/header.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use crate::platform::*;
88
use crate::tui::stat::{CpuValueMode, TuiStat};
99
use bytesize::ByteSize;
1010
use uu_vmstat::{CpuLoad, CpuLoadRaw};
11-
use uu_w::get_formatted_uptime_procps;
12-
use uucore::uptime::{get_formatted_loadavg, get_formatted_nusers, get_formatted_time};
11+
use uu_w::{get_formatted_loadavg, get_formatted_nusers, get_formatted_uptime_procps};
12+
use uucore::uptime::get_formatted_time;
1313

1414
pub(crate) struct Header {
1515
pub uptime: Uptime,
@@ -113,7 +113,7 @@ pub(crate) fn format_memory(memory_b: u64, unit: u64) -> f64 {
113113
fn user() -> String {
114114
#[cfg(target_os = "linux")]
115115
if let Ok(nusers) = get_nusers_systemd() {
116-
return uucore::uptime::format_nusers(nusers);
116+
return uu_w::format_nusers(nusers);
117117
}
118118

119119
get_formatted_nusers()

src/uu/w/src/w.rs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ use std::{process, time::Duration};
1313
use uucore::error::UResult;
1414
#[cfg(target_os = "linux")]
1515
use 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")]
2018
use 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]
214239
pub 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

Comments
 (0)