Skip to content

Commit

Permalink
w: implement --short option (uutils#61)
Browse files Browse the repository at this point in the history
* 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 <daniel.hofstetter@42dh.com>
  • Loading branch information
ant0nkress and cakebaker authored May 14, 2024
1 parent 1f0ae84 commit cb0681a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 11 deletions.
34 changes: 23 additions & 11 deletions src/uu/w/src/w.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
32 changes: 32 additions & 0 deletions tests/by-util/test_w.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Regex> = 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")]
Expand Down

0 comments on commit cb0681a

Please sign in to comment.