Skip to content

Commit 0f7e7dc

Browse files
authored
Merge branch 'master' into rosetta
2 parents 5a56720 + 437d24b commit 0f7e7dc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+273
-181
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ dockworker = { version = "0.6.0", optional = true }
4545
getch = "0.3.1"
4646
libc = "0.2"
4747
minus = { version = "5.6", features = ["static_output", "search"] }
48-
once_cell = "1.21.1"
48+
once_cell = "1.21.2"
4949
serde = "1.0"
5050
serde_derive = "1.0"
5151
termbg = "0.6.2"

src/column.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub trait Column {
2020
) -> String;
2121
fn display_unit(&self, align: &ConfigColumnAlign) -> String;
2222
fn display_content(&self, pid: i32, align: &ConfigColumnAlign) -> Option<String>;
23+
fn display_json(&self, pid: i32) -> String;
2324
fn find_partial(&self, pid: i32, keyword: &str, content_to_lowercase: bool) -> bool;
2425
fn find_exact(&self, pid: i32, keyword: &str, content_to_lowercase: bool) -> bool;
2526
fn sorted_pid(&self, order: &ConfigSortOrder) -> Vec<i32>;
@@ -33,6 +34,7 @@ pub trait Column {
3334
);
3435
fn update_width(&mut self, pid: i32, max_width: Option<usize>);
3536
fn get_width(&self) -> usize;
37+
fn is_numeric(&self) -> bool;
3638
}
3739

3840
#[macro_export]
@@ -85,6 +87,30 @@ macro_rules! column_default_display_content {
8587
};
8688
}
8789

90+
#[macro_export]
91+
macro_rules! column_default_display_json {
92+
() => {
93+
fn display_json(&self, pid: i32) -> String {
94+
let value = if self.is_numeric() {
95+
self.raw_contents
96+
.get(&pid)
97+
.map(|x| x.to_string())
98+
.unwrap_or("".to_string())
99+
} else {
100+
let value = self
101+
.fmt_contents
102+
.get(&pid)
103+
.map(|x| x.clone())
104+
.unwrap_or("".to_string());
105+
let value = value.replace("\\", "\\\\");
106+
let value = value.replace("\"", "\\\"");
107+
format!("\"{}\"", value)
108+
};
109+
format!("\"{}\": {}", self.header, value)
110+
}
111+
};
112+
}
113+
88114
#[macro_export]
89115
macro_rules! column_default_find_partial {
90116
() => {
@@ -201,18 +227,29 @@ macro_rules! column_default_get_width {
201227
};
202228
}
203229

230+
#[macro_export]
231+
macro_rules! column_default_is_numeric {
232+
($x:expr) => {
233+
fn is_numeric(&self) -> bool {
234+
$x
235+
}
236+
};
237+
}
238+
204239
#[macro_export]
205240
macro_rules! column_default {
206-
($x:ty) => {
241+
($x:ty, $y:expr) => {
207242
$crate::column_default_display_header!();
208243
$crate::column_default_display_unit!();
209244
$crate::column_default_display_content!();
245+
$crate::column_default_display_json!();
210246
$crate::column_default_find_partial!();
211247
$crate::column_default_find_exact!();
212248
$crate::column_default_sorted_pid!($x);
213249
$crate::column_default_apply_visible!();
214250
$crate::column_default_reset_width!();
215251
$crate::column_default_update_width!();
216252
$crate::column_default_get_width!();
253+
$crate::column_default_is_numeric!($y);
217254
};
218255
}

src/columns/ccgroup.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,5 @@ impl Column for Ccgroup {
7676
self.raw_contents.insert(proc.pid, raw_content);
7777
}
7878

79-
column_default!(String);
79+
column_default!(String, false);
8080
}

src/columns/cgroup.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@ impl Column for Cgroup {
4141
self.raw_contents.insert(proc.pid, raw_content);
4242
}
4343

44-
column_default!(String);
44+
column_default!(String, false);
4545
}

src/columns/command.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl Column for Command {
5353
self.raw_contents.insert(proc.pid, raw_content);
5454
}
5555

56-
column_default!(String);
56+
column_default!(String, false);
5757
}
5858

5959
#[cfg(target_os = "macos")]
@@ -85,7 +85,7 @@ impl Column for Command {
8585
self.raw_contents.insert(proc.pid, raw_content);
8686
}
8787

88-
column_default!(String);
88+
column_default!(String, false);
8989
}
9090

9191
#[cfg(target_os = "windows")]
@@ -98,7 +98,7 @@ impl Column for Command {
9898
self.raw_contents.insert(proc.pid, raw_content);
9999
}
100100

101-
column_default!(String);
101+
column_default!(String, false);
102102
}
103103

104104
#[cfg(target_os = "freebsd")]
@@ -126,5 +126,5 @@ impl Column for Command {
126126
self.raw_contents.insert(proc.pid, raw_content);
127127
}
128128

129-
column_default!(String);
129+
column_default!(String, false);
130130
}

src/columns/context_sw.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl Column for ContextSw {
4747
self.raw_contents.insert(proc.pid, raw_content);
4848
}
4949

50-
column_default!(u64);
50+
column_default!(u64, true);
5151
}
5252

5353
#[cfg(target_os = "macos")]
@@ -60,7 +60,7 @@ impl Column for ContextSw {
6060
self.raw_contents.insert(proc.pid, raw_content);
6161
}
6262

63-
column_default!(u64);
63+
column_default!(u64, true);
6464
}
6565

6666
#[cfg(target_os = "freebsd")]
@@ -74,5 +74,5 @@ impl Column for ContextSw {
7474
self.raw_contents.insert(proc.pid, raw_content);
7575
}
7676

77-
column_default!(u64);
77+
column_default!(u64, true);
7878
}

src/columns/cpu_time.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl Column for CpuTime {
3838
self.raw_contents.insert(proc.pid, raw_content);
3939
}
4040

41-
column_default!(u64);
41+
column_default!(u64, true);
4242
}
4343

4444
#[cfg(target_os = "macos")]
@@ -55,7 +55,7 @@ impl Column for CpuTime {
5555
self.raw_contents.insert(proc.pid, raw_content);
5656
}
5757

58-
column_default!(u64);
58+
column_default!(u64, true);
5959
}
6060

6161
#[cfg(target_os = "windows")]
@@ -70,7 +70,7 @@ impl Column for CpuTime {
7070
self.raw_contents.insert(proc.pid, raw_content);
7171
}
7272

73-
column_default!(u64);
73+
column_default!(u64, true);
7474
}
7575

7676
#[cfg(target_os = "freebsd")]
@@ -89,5 +89,5 @@ impl Column for CpuTime {
8989
self.raw_contents.insert(proc.pid, raw_content);
9090
}
9191

92-
column_default!(u64);
92+
column_default!(u64, true);
9393
}

src/columns/docker.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ impl Column for Docker {
137137
self.available
138138
}
139139

140-
column_default!(String);
140+
column_default!(String, false);
141141
}
142142

143143
#[cfg(target_os = "macos")]
@@ -158,5 +158,5 @@ impl Column for Docker {
158158
self.available
159159
}
160160

161-
column_default!(String);
161+
column_default!(String, false);
162162
}

src/columns/eip.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ impl Column for Eip {
3434
self.raw_contents.insert(proc.pid, raw_content);
3535
}
3636

37-
column_default!(u64);
37+
column_default!(u64, true);
3838
}

0 commit comments

Comments
 (0)