Skip to content

Commit 19a2733

Browse files
committed
Add functionality and tests
Signed-off-by: Malhar Vora <mlvora.2010@gmail.com>
1 parent ffae094 commit 19a2733

3 files changed

Lines changed: 30 additions & 5 deletions

File tree

src/uu/lsns/src/lsns.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
102102

103103
read_namespaces(&mut lsns)?;
104104

105-
display_namespaces(&lsns)?;
105+
display_namespaces(&lsns, noheadings)?;
106106

107107
Ok(())
108108
}
@@ -491,7 +491,7 @@ impl NamespaceType {
491491

492492
/// Display namespaces in default format using smartcols
493493
#[cfg(target_os = "linux")]
494-
fn display_namespaces(lsns: &Lsns) -> Result<(), LsnsError> {
494+
fn display_namespaces(lsns: &Lsns, noheadings: bool) -> Result<(), LsnsError> {
495495
use smartcols_sys::{SCOLS_FL_RIGHT, SCOLS_FL_TRUNC};
496496

497497
// Initialize smartcols
@@ -500,6 +500,11 @@ fn display_namespaces(lsns: &Lsns) -> Result<(), LsnsError> {
500500
// Create table
501501
let mut table = Table::new()?;
502502

503+
// Enable or disable headings based on flag
504+
if noheadings {
505+
table.enable_headings(false)?;
506+
}
507+
503508
// NS: width_hint=10, right-aligned
504509
table.new_column(c"NS", 10.0, SCOLS_FL_RIGHT)?;
505510
// TYPE: width_hint=5, left-aligned
@@ -567,7 +572,7 @@ fn display_namespaces(lsns: &Lsns) -> Result<(), LsnsError> {
567572
}
568573

569574
#[cfg(not(target_os = "linux"))]
570-
fn display_namespaces(_lsns: &Lsns) -> Result<(), LsnsError> {
575+
fn display_namespaces(_lsns: &Lsns, _noheadings: bool) -> Result<(), LsnsError> {
571576
Err(LsnsError::UnsupportedPlatform)
572577
}
573578

src/uu/lsns/src/smartcols.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use std::{io, ptr};
99

1010
use smartcols_sys::{
1111
libscols_column, libscols_line, libscols_table, scols_init_debug, scols_line_set_data,
12-
scols_new_table, scols_print_table, scols_table_new_column, scols_table_new_line,
13-
scols_unref_table,
12+
scols_new_table, scols_print_table, scols_table_enable_noheadings, scols_table_new_column,
13+
scols_table_new_line, scols_unref_table,
1414
};
1515

1616
use crate::errors::LsnsError;
@@ -45,6 +45,12 @@ impl Drop for Table {
4545
pub(crate) trait TableOperations: Sized {
4646
fn as_ptr(&self) -> *mut libscols_table;
4747

48+
fn enable_headings(&mut self, enable: bool) -> Result<(), LsnsError> {
49+
let no_headings = c_int::from(!enable);
50+
let r = unsafe { scols_table_enable_noheadings(self.as_ptr(), no_headings) };
51+
LsnsError::io_from_neg_errno("scols_table_enable_noheadings", r).map(|_| ())
52+
}
53+
4854
fn new_column(
4955
&mut self,
5056
name: &CStr,

tests/by-util/test_lsns.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,20 @@ fn test_output_format() {
9494
}
9595
}
9696

97+
#[test]
98+
#[cfg(target_os = "linux")]
99+
fn test_noheadings() {
100+
let res = new_ucmd!().arg("-n").succeeds();
101+
let stdout = res.no_stderr().stdout_str();
102+
103+
let headers = ["NS", "TYPE", "NPROCS", "PID", "USER", "COMMAND"];
104+
105+
for header in headers {
106+
let msg = format!("{} header should not be present when -n is used", header);
107+
assert!(!stdout.contains(header), "{}", msg);
108+
}
109+
}
110+
97111
#[test]
98112
#[cfg(target_os = "linux")]
99113
fn test_namespace_ids_are_numeric() {

0 commit comments

Comments
 (0)