Skip to content

Commit fee00ea

Browse files
authored
Merge pull request #384 from Bluemangoo/feature/w-header
w: add uptime header
2 parents 3d54f33 + 9313de7 commit fee00ea

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

src/uu/w/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ keywords = ["acl", "uutils", "cross-platform", "cli", "utility"]
1212
categories = ["command-line-utilities"]
1313

1414
[dependencies]
15-
uucore = { workspace = true, features = ["utmpx"] }
15+
uucore = { workspace = true, features = ["utmpx", "uptime"] }
1616
clap = { workspace = true }
1717
chrono = { workspace = true, default-features = false, features = [
1818
"clock",

src/uu/w/src/w.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ use libc::{sysconf, _SC_CLK_TCK};
1212
#[cfg(target_os = "linux")]
1313
use std::{collections::HashMap, fs, path::Path, time::SystemTime};
1414
use std::{process, time::Duration};
15+
use uucore::uptime::{
16+
get_formated_uptime, get_formatted_loadavg, get_formatted_nusers, get_formatted_time,
17+
};
1518
#[cfg(target_os = "linux")]
1619
use uucore::utmpx::Utmpx;
1720
use uucore::{error::UResult, format_usage, help_about, help_usage};
@@ -184,6 +187,21 @@ fn fetch_user_info() -> Result<Vec<UserInfo>, std::io::Error> {
184187
Ok(user_info_list)
185188
}
186189

190+
fn print_uptime() {
191+
print!(" {} ", get_formatted_time());
192+
if let Ok(uptime) = get_formated_uptime(None) {
193+
print!("{}, ", uptime);
194+
} else {
195+
print!("up ???? days ??:??, ");
196+
}
197+
198+
print!(" {}", get_formatted_nusers());
199+
if let Ok(loadavg) = get_formatted_loadavg() {
200+
print!(", {}", loadavg);
201+
}
202+
println!();
203+
}
204+
187205
#[cfg(any(target_os = "macos", target_os = "windows"))]
188206
fn fetch_user_info() -> Result<Vec<UserInfo>, std::io::Error> {
189207
Ok(Vec::new())
@@ -200,6 +218,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
200218
match fetch_user_info() {
201219
Ok(user_info) => {
202220
if !no_header {
221+
print_uptime();
203222
if short {
204223
println!("{:<9}{:<9}{:<7}{:<}", "USER", "TTY", "IDLE", "WHAT");
205224
} else {

tests/by-util/test_w.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ fn test_no_header() {
2626

2727
let result = cmd.stdout_str();
2828

29+
assert!(match result.lines().next() {
30+
None => true,
31+
Some(line) => !line.contains("user") && !line.contains("load average"),
32+
});
2933
assert!(!result.contains("USER TTY LOGIN@ IDLE JCPU PCPU WHAT"));
3034
}
3135
}
@@ -41,7 +45,7 @@ fn test_option_short() {
4145

4246
let cmd_output = cmd.stdout_str();
4347
let cmd_output_lines: Vec<&str> = cmd_output.split('\n').collect();
44-
let line_output_header = cmd_output_lines[0];
48+
let line_output_header = cmd_output_lines[1];
4549
let line_output_data_words: Vec<&str> = cmd_output_lines[1]
4650
.trim()
4751
.split(' ')

0 commit comments

Comments
 (0)