robinpath_modules/modules/
process_mod.rs1use robinpath::{RobinPath, Value};
2use std::sync::{Arc, Mutex};
3use std::time::Instant;
4
5pub fn register(rp: &mut RobinPath) {
6 let start_time = Arc::new(Instant::now());
7 let exit_code = Arc::new(Mutex::new(0i32));
8
9 rp.register_builtin("process.pid", |_args, _| {
11 Ok(Value::Number(std::process::id() as f64))
12 });
13
14 rp.register_builtin("process.args", |_args, _| {
16 let args: Vec<Value> = std::env::args()
17 .map(Value::String)
18 .collect();
19 Ok(Value::Array(args))
20 });
21
22 let ec = exit_code.clone();
24 rp.register_builtin("process.exitCode", move |args, _| {
25 let code = args.first().map(|v| v.to_number()).unwrap_or(0.0) as i32;
26 let mut stored = ec.lock().unwrap();
27 *stored = code;
28 Ok(Value::Number(code as f64))
29 });
30
31 let st = start_time.clone();
33 rp.register_builtin("process.uptime", move |_args, _| {
34 let elapsed = st.elapsed();
35 Ok(Value::Number(elapsed.as_secs_f64()))
36 });
37
38 rp.register_builtin("process.cwd", |_args, _| {
40 match std::env::current_dir() {
41 Ok(path) => Ok(Value::String(path.to_string_lossy().to_string())),
42 Err(e) => Err(format!("process.cwd: {}", e)),
43 }
44 });
45
46 rp.register_builtin("process.memoryUsage", |_args, _| {
48 let mut obj = indexmap::IndexMap::new();
49 obj.insert("rss".to_string(), Value::Number(0.0));
50 Ok(Value::Object(obj))
51 });
52}