Skip to content

Commit 098372d

Browse files
committed
feat: render env as json object when in json
1 parent a52ad46 commit 098372d

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

Cargo.lock

Lines changed: 1 addition & 0 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ minus = { version = "5.6", features = ["static_output", "search"] }
4848
once_cell = "1.21.3"
4949
serde = "1.0"
5050
serde_derive = "1.0"
51+
serde_json = "1.0"
5152
termbg = "0.6.2"
5253
tokio = { version = "1.47", optional = true, features = ["rt"] }
5354
toml = "0.9"

src/column.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,19 @@ macro_rules! column_default {
253253
$crate::column_default_is_numeric!($y);
254254
};
255255
}
256+
#[macro_export]
257+
macro_rules! column_default_custom_json {
258+
($x:ty, $y:expr) => {
259+
$crate::column_default_display_header!();
260+
$crate::column_default_display_unit!();
261+
$crate::column_default_display_content!();
262+
$crate::column_default_find_partial!();
263+
$crate::column_default_find_exact!();
264+
$crate::column_default_sorted_pid!($x);
265+
$crate::column_default_apply_visible!();
266+
$crate::column_default_reset_width!();
267+
$crate::column_default_update_width!();
268+
$crate::column_default_get_width!();
269+
$crate::column_default_is_numeric!($y);
270+
};
271+
}

src/columns/env.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::process::ProcessInfo;
2-
use crate::{column_default, Column};
2+
use crate::{column_default_custom_json, Column};
33
use std::cmp;
44
use std::collections::HashMap;
55
use std::path::PathBuf;
@@ -73,16 +73,27 @@ impl Column for Env {
7373
impl Column for Env {
7474
fn add(&mut self, proc: &ProcessInfo) {
7575
let mut fmt_content = String::new();
76+
let mut raw_content = Vec::new();
7677
if let Some(path) = &proc.curr_path {
7778
for env in &path.env {
78-
fmt_content.push_str(&format!("{} ", env.replace('\"', "\\\"")));
79+
let json_str = serde_json::json!(env);
80+
fmt_content.push_str(json_str.to_string().as_str());
81+
raw_content.push(env);
7982
}
8083
};
81-
let raw_content = fmt_content.clone();
84+
let raw_content = serde_json::json!(raw_content).to_string();
8285

8386
self.fmt_contents.insert(proc.pid, fmt_content);
8487
self.raw_contents.insert(proc.pid, raw_content);
8588
}
8689

87-
column_default!(String, false);
90+
column_default_custom_json!(String, false);
91+
92+
fn display_json(&self, pid: i32) -> String {
93+
format!(
94+
"\"{}\": {}",
95+
self.header,
96+
self.raw_contents.get(&pid).unwrap_or(&"[]".to_string())
97+
)
98+
}
8899
}

0 commit comments

Comments
 (0)