Add a --full mode to ps (#1507)

* Add a --full mode to ps

* Use a slightly older heim
This commit is contained in:
Jonathan Turner
2020-03-20 20:53:49 +13:00
committed by GitHub
parent afa963fd50
commit b5ea522f0e
11 changed files with 236 additions and 496 deletions

View File

@ -9,11 +9,16 @@ impl Plugin for Ps {
fn config(&mut self) -> Result<Signature, ShellError> {
Ok(Signature::build("ps")
.desc("View information about system processes.")
.switch(
"full",
"list all available columns for each entry",
Some('f'),
)
.filter())
}
fn begin_filter(&mut self, callinfo: CallInfo) -> Result<Vec<ReturnValue>, ShellError> {
Ok(block_on(ps(callinfo.name_tag))
Ok(block_on(ps(callinfo.name_tag, callinfo.args.has("full")))
.into_iter()
.map(ReturnSuccess::value)
.collect())

View File

@ -27,8 +27,8 @@ async fn usage(process: Process) -> ProcessResult<(process::Process, Ratio, proc
Ok((process, usage_2 - usage_1, memory))
}
pub async fn ps(tag: Tag) -> Vec<Value> {
let processes = process::processes()
pub async fn ps(tag: Tag, full: bool) -> Vec<Value> {
let mut processes = process::processes()
.map_ok(|process| {
// Note that there is no `.await` here,
// as we want to pass the returned future
@ -36,7 +36,6 @@ pub async fn ps(tag: Tag) -> Vec<Value> {
usage(process)
})
.try_buffer_unordered(usize::MAX);
pin_utils::pin_mut!(processes);
let mut output = vec![];
while let Some(res) = processes.next().await {
@ -58,6 +57,22 @@ pub async fn ps(tag: Tag) -> Vec<Value> {
"virtual",
UntaggedValue::bytes(memory.vms().get::<information::byte>()),
);
if full {
if let Ok(parent_pid) = process.parent_pid().await {
dict.insert_untagged("parent", UntaggedValue::int(parent_pid))
}
if let Ok(exe) = process.exe().await {
dict.insert_untagged("exe", UntaggedValue::string(exe.to_string_lossy()))
}
if let Ok(command) = process.command().await {
dict.insert_untagged(
"command",
UntaggedValue::string(command.to_os_string().to_string_lossy()),
);
}
}
output.push(dict.into_value());
}
}