Skip to content

Commit 9e562bb

Browse files
estodicakebaker
andauthored
pmap: implemented quiet option (#423)
* pmap: implemented quiet option * pmap: added tests for quiet option * pmap: fixed quiet option handling in tests * Update src/uu/pmap/src/pmap.rs Co-authored-by: Daniel Hofstetter <[email protected]> --------- Co-authored-by: Daniel Hofstetter <[email protected]>
1 parent e427ebd commit 9e562bb

File tree

3 files changed

+222
-139
lines changed

3 files changed

+222
-139
lines changed

src/uu/pmap/src/pmap.rs

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
4848
}
4949

5050
// Options independent with field selection:
51+
pmap_config.quiet = matches.get_flag(options::QUIET);
5152
if matches.get_flag(options::SHOW_PATH) {
5253
pmap_config.show_path = true;
5354
}
@@ -142,15 +143,19 @@ fn output_default_format(pid: &str, pmap_config: &PmapConfig) -> Result<(), Erro
142143
total += map_line.size_in_kb;
143144
})?;
144145

145-
println!(" total {total:>16}K");
146+
if !pmap_config.quiet {
147+
println!(" total {total:>16}K");
148+
}
146149

147150
Ok(())
148151
}
149152

150153
fn output_extended_format(pid: &str, pmap_config: &PmapConfig) -> Result<(), Error> {
151154
let smap_table = get_smap_table(pid)?;
152155

153-
println!("Address Kbytes RSS Dirty Mode Mapping");
156+
if !pmap_config.quiet {
157+
println!("Address Kbytes RSS Dirty Mode Mapping");
158+
}
154159

155160
for smap_entry in smap_table.entries {
156161
println!(
@@ -164,13 +169,16 @@ fn output_extended_format(pid: &str, pmap_config: &PmapConfig) -> Result<(), Err
164169
);
165170
}
166171

167-
println!("---------------- ------- ------- ------- ");
168-
println!(
169-
"total kB {:>7} {:>7} {:>7}",
170-
smap_table.info.total_size_in_kb,
171-
smap_table.info.total_rss_in_kb,
172-
smap_table.info.total_shared_dirty_in_kb + smap_table.info.total_private_dirty_in_kb,
173-
);
172+
if !pmap_config.quiet {
173+
println!("---------------- ------- ------- ------- ");
174+
println!(
175+
"total kB {:>7} {:>7} {:>7}",
176+
smap_table.info.total_size_in_kb,
177+
smap_table.info.total_rss_in_kb,
178+
smap_table.info.total_shared_dirty_in_kb + smap_table.info.total_private_dirty_in_kb,
179+
);
180+
}
181+
174182
Ok(())
175183
}
176184

@@ -185,15 +193,20 @@ fn output_custom_format(pid: &str, pmap_config: &mut PmapConfig) -> Result<(), E
185193
}
186194

187195
// Header
188-
{
196+
if !pmap_config.quiet {
189197
let mut line = format!(
190198
"{:>width$} ",
191199
pmap_field_name::ADDRESS,
192200
width = smap_table.info.get_width(pmap_field_name::ADDRESS)
193201
);
194202

203+
pmap_config.quiet = true;
195204
for field_name in pmap_config.get_field_list() {
196205
if pmap_config.is_enabled(field_name) {
206+
// If there is any field that needs footer, we can't suppress the footer
207+
if pmap_config.needs_footer(field_name) {
208+
pmap_config.quiet = false;
209+
}
197210
line += &format!(
198211
"{:>width$} ",
199212
field_name,
@@ -230,7 +243,7 @@ fn output_custom_format(pid: &str, pmap_config: &mut PmapConfig) -> Result<(), E
230243
}
231244

232245
// Footer
233-
{
246+
if !pmap_config.quiet {
234247
// Separator
235248
let mut line = format!(
236249
"{:>width$} ",
@@ -292,7 +305,11 @@ fn output_device_format(pid: &str, pmap_config: &PmapConfig) -> Result<(), Error
292305

293306
process_maps(
294307
pid,
295-
Some("Address Kbytes Mode Offset Device Mapping"),
308+
if !pmap_config.quiet {
309+
Some("Address Kbytes Mode Offset Device Mapping")
310+
} else {
311+
None
312+
},
296313
|map_line| {
297314
println!(
298315
"{} {:>7} {} {} {} {}",
@@ -315,9 +332,11 @@ fn output_device_format(pid: &str, pmap_config: &PmapConfig) -> Result<(), Error
315332
},
316333
)?;
317334

318-
println!(
319-
"mapped: {total_mapped}K writeable/private: {total_writeable_private}K shared: {total_shared}K"
320-
);
335+
if !pmap_config.quiet {
336+
println!(
337+
"mapped: {total_mapped}K writeable/private: {total_writeable_private}K shared: {total_shared}K"
338+
);
339+
}
321340

322341
Ok(())
323342
}

src/uu/pmap/src/pmap_config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ pub struct PmapConfig {
7575
// [Mapping] category
7676
pub show_path: bool,
7777
// Misc
78+
pub quiet: bool,
7879
pub custom_format_enabled: bool,
7980
}
8081

0 commit comments

Comments
 (0)