-
-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathcommand.rs
More file actions
130 lines (116 loc) · 3.63 KB
/
Copy pathcommand.rs
File metadata and controls
130 lines (116 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use crate::process::ProcessInfo;
use crate::{column_default, Column};
use std::cmp;
use std::collections::HashMap;
pub struct Command {
header: String,
unit: String,
fmt_contents: HashMap<i32, String>,
raw_contents: HashMap<i32, String>,
width: usize,
}
impl Command {
pub fn new(header: Option<String>) -> Self {
let header = header.unwrap_or_else(|| String::from("Command"));
let unit = String::new();
Self {
fmt_contents: HashMap::new(),
raw_contents: HashMap::new(),
width: 0,
header,
unit,
}
}
}
#[cfg(any(target_os = "linux", target_os = "android"))]
impl Column for Command {
fn add(&mut self, proc: &ProcessInfo) {
let fmt_content = if let Ok(cmd) = &proc.curr_proc.cmdline() {
if !cmd.is_empty() {
let mut cmd = cmd
.iter()
.cloned()
.map(|mut x| {
x.push(' ');
x
})
.collect::<String>();
cmd.pop();
cmd = cmd.replace(['\n', '\t'], " ");
cmd
} else {
format!("[{}]", proc.curr_proc.stat().comm)
}
} else {
proc.curr_proc.stat().comm.clone()
};
let raw_content = fmt_content.clone();
self.fmt_contents.insert(proc.pid, fmt_content);
self.raw_contents.insert(proc.pid, raw_content);
}
column_default!(String, false);
}
#[cfg(target_os = "macos")]
impl Column for Command {
fn add(&mut self, proc: &ProcessInfo) {
let fmt_content = if let Some(path) = &proc.curr_path {
if !path.cmd.is_empty() {
let mut cmd = path
.cmd
.iter()
.cloned()
.map(|mut x| {
x.push(' ');
x
})
.collect::<String>();
cmd.pop();
cmd = cmd.replace(['\n', '\t'], " ");
cmd
} else {
String::from("")
}
} else {
String::from("")
};
let raw_content = fmt_content.clone();
self.fmt_contents.insert(proc.pid, fmt_content);
self.raw_contents.insert(proc.pid, raw_content);
}
column_default!(String, false);
}
#[cfg(target_os = "windows")]
impl Column for Command {
fn add(&mut self, proc: &ProcessInfo) {
let fmt_content = proc.command.clone();
let raw_content = fmt_content.clone();
self.fmt_contents.insert(proc.pid, fmt_content);
self.raw_contents.insert(proc.pid, raw_content);
}
column_default!(String, false);
}
#[cfg(target_os = "freebsd")]
impl Column for Command {
fn add(&mut self, proc: &ProcessInfo) {
let command = if proc.curr_proc.arg.is_empty() {
let comm = crate::util::ptr_to_cstr(proc.curr_proc.info.comm.as_ref());
if let Ok(comm) = comm {
format!("[{}]", comm.to_string_lossy())
} else {
String::from("")
}
} else {
let mut x = String::from("");
for arg in &proc.curr_proc.arg {
x.push_str(&arg);
x.push_str(" ");
}
x
};
let fmt_content = command;
let raw_content = fmt_content.clone();
self.fmt_contents.insert(proc.pid, fmt_content);
self.raw_contents.insert(proc.pid, raw_content);
}
column_default!(String, false);
}