Repository: uutils/procps
Pull request #61 (2024-04-17 14:49:48)

`w`: implement --short option
# Description

We check for the parameter `-s | --short`. If it is set we exclude printing of `login time`, `JCPU time` and `PCPU time`.

# Testing

Added a test to make sure only necessary values are printed.

cakebaker: @ant0nkress ping?

Patch:
diff --git a/src/uu/w/src/w.rs b/src/uu/w/src/w.rs
index d13364a..1529f70 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) => {


Test patch:
diff --git a/tests/by-util/test_w.rs b/tests/by-util/test_w.rs
index 904cd1e..01486a2 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<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")]