ps: Support --no-headers flag

This commit is contained in:
Tuomas Tynkkynen
2025-10-31 22:03:09 +02:00
parent 6e386d973a
commit 0d4b961446
2 changed files with 30 additions and 8 deletions
+16 -8
View File
@@ -106,15 +106,16 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
collect_code_mapping(&arg_formats)
};
let header = code_mapping
.iter()
.map(|(_, header)| header)
.map(Into::into)
.collect::<Vec<String>>();
// Apply header
let mut table = Table::from_iter([Row::from_iter(header)]);
let mut table = Table::new();
table.set_format(*FORMAT_CLEAN);
if !matches.get_flag("no-headers") {
let header = code_mapping
.iter()
.map(|(_, header)| header)
.map(Into::into)
.collect::<Vec<String>>();
table.add_row(Row::from_iter(header));
}
table.extend(rows);
print!("{table}");
@@ -263,6 +264,13 @@ pub fn uu_app() -> Command {
.value_parser(parser)
.help("user-defined format"),
)
.arg(
Arg::new("no-headers")
.long("no-headers")
.visible_alias("no-heading")
.action(ArgAction::SetTrue)
.help("do not print header at all"),
)
// .args([
// Arg::new("command").short('c').help("command name"),
// Arg::new("GID")
+14
View File
@@ -3,6 +3,8 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
#[cfg(target_os = "linux")]
use regex::Regex;
use uutests::new_ucmd;
#[test]
@@ -165,3 +167,15 @@ fn test_code_mapping() {
.stdout_contains("CMD1")
.stdout_contains("CMD2");
}
#[test]
#[cfg(target_os = "linux")]
fn test_no_headers_flags() {
let regex = Regex::new("^ *PID +").unwrap();
for flag in &["--no-headers", "--no-heading"] {
new_ucmd!()
.arg(flag)
.succeeds()
.stdout_does_not_match(&regex);
}
}