1use std::sync::Arc;
2
3use crate::{
4 common::{Exec, Process, ProcessState},
5 error::Result,
6 state::State,
7 Pid,
8};
9
10#[derive(Debug, Default, PartialEq)]
11pub struct PsArgs {
12 pub processes: Vec<String>,
13}
14
15pub struct PsOutput {
16 pub name: String,
17 pub state: ProcessState,
18 pub pid: Option<Pid>,
19}
20
21impl From<Process> for PsOutput {
22 fn from(value: Process) -> Self {
23 Self {
24 name: value.name,
25 state: value.state,
26 pid: value.pid,
27 }
28 }
29}
30
31pub struct Ps {
32 args: PsArgs,
33 state: Arc<State>,
34}
35
36impl Ps {
37 pub fn new(args: PsArgs, state: Arc<State>) -> Self {
38 Ps { args, state }
39 }
40
41 pub async fn run(&self) -> Result<Vec<PsOutput>> {
42 let mut processes = self.state.filter_processes(&self.args.processes).await?;
43 processes.sort();
44 Ok(processes.into_iter().map(PsOutput::from).collect())
45 }
46}
47
48impl Exec<Vec<PsOutput>> for Ps {
49 async fn exec(&self) -> Result<Vec<PsOutput>> {
50 self.run().await
51 }
52}