From cb0681a439c6437e4b492e965cf3ac5080632626 Mon Sep 17 00:00:00 2001 From: ant0nkress <167328695+ant0nkress@users.noreply.github.com> Date: Tue, 14 May 2024 16:18:21 +0200 Subject: [PATCH] `w`: implement --short option (#61) * w: implement --short option * test/w: implement test for --short option * w: format code with fmt * w: use char instead of &str for split * w: make test linux-only * w: run part of tests only if in terminal * w: remove negation of two conditions and switch if/else cases --------- Co-authored-by: Daniel Hofstetter --- src/uu/w/src/w.rs | 34 +++++++++++++++++++++++----------- tests/by-util/test_w.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 11 deletions(-) diff --git a/src/uu/w/src/w.rs b/src/uu/w/src/w.rs index d13364af..1529f70d 100644 --- a/src/uu/w/src/w.rs +++ b/src/uu/w/src/w.rs @@ -149,23 +149,35 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let matches = uu_app().try_get_matches_from(args)?; let no_header = matches.get_flag("no-header"); + let short = matches.get_flag("short"); match fetch_user_info() { Ok(user_info) => { if !no_header { - println!("USER\tTTY\tLOGIN@\tIDLE\tJCPU\tPCPU\tWHAT"); + if short { + println!("USER\tTTY\tIDLE\tWHAT"); + } else { + println!("USER\tTTY\tLOGIN@\tIDLE\tJCPU\tPCPU\tWHAT"); + } } for user in user_info { - println!( - "{}\t{}\t{}\t{}\t{}s\t{}s\t{}", - user.user, - user.terminal, - user.login_time, - user.idle_time, - user.jcpu, - user.pcpu, - user.command - ); + if short { + println!( + "{}\t{}\t{}\t{}", + user.user, user.terminal, user.idle_time, user.command + ); + } else { + println!( + "{}\t{}\t{}\t{}\t{}s\t{}s\t{}", + user.user, + user.terminal, + user.login_time, + user.idle_time, + user.jcpu, + user.pcpu, + user.command + ); + } } } Err(e) => { diff --git a/tests/by-util/test_w.rs b/tests/by-util/test_w.rs index 904cd1e1..01486a24 100644 --- a/tests/by-util/test_w.rs +++ b/tests/by-util/test_w.rs @@ -31,6 +31,38 @@ fn test_no_header() { } } +#[test] +// As of now, --short is only implemented for Linux +#[cfg(target_os = "linux")] +fn test_option_short() { + use std::io::IsTerminal; + + use regex::Regex; + let cmd = new_ucmd!().arg("--short").succeeds(); + + let cmd_output = cmd.stdout_str(); + let cmd_output_lines: Vec<&str> = cmd_output.split('\n').collect(); + let line_output_header = cmd_output_lines[0]; + let line_output_data_words: Vec<&str> = cmd_output_lines[1].split('\t').collect(); + + assert!(line_output_header.contains("USER\tTTY\tIDLE\tWHAT")); + assert!(!line_output_header.contains("USER\tTTY\tLOGIN@\tIDLE\tJCPU\tPCPU\tWHAT")); + + if std::io::stdout().is_terminal() { + let pattern: Vec = vec![ + Regex::new(r"^(\S+)").unwrap(), // USER + Regex::new(r"(\S+)").unwrap(), // TERMINAL + Regex::new(r"(^$)").unwrap(), // IDLE_TIME => empty str until IDLE_TIME implemented + Regex::new(r"(\d+\.\d+s)?").unwrap(), // COMMAND + ]; + + assert!(pattern[0].is_match(line_output_data_words[0])); + assert!(pattern[1].is_match(line_output_data_words[1])); + assert!(pattern[2].is_match(line_output_data_words[2])); + assert!(pattern[3].is_match(line_output_data_words[3])); + } +} + #[test] // As of now, output is only implemented for Linux #[cfg(target_os = "linux")]