ros2_tui 0.1.5

A Terminal User Interface (TUI) for monitoring and managing ROS2 topics and parameters
Documentation
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
use serde::{Deserialize, Serialize};
use serde_yaml::Value;
use std::fmt;
use tokio::process::Command;

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ParamInfo {
    pub node_name: String,
    pub param_name: String,
    pub value: Option<String>,
    pub param_type: String,
}

#[derive(Debug)]
pub enum ParamError {
    Io(std::io::Error),
    ParseError(String),
    InvalidType(String),
}

impl fmt::Display for ParamError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            ParamError::Io(e) => write!(f, "IO error: {}", e),
            ParamError::ParseError(s) => write!(f, "Parse error: {}", s),
            ParamError::InvalidType(s) => write!(f, "Type validation error: {}", s),
        }
    }
}

impl From<std::io::Error> for ParamError {
    fn from(err: std::io::Error) -> Self {
        ParamError::Io(err)
    }
}

pub async fn get_param_list() -> Result<Vec<ParamInfo>, ParamError> {
    let start_time = std::time::Instant::now();

    crate::debug_log("Executing: ros2 param list");
    let command_str = "ros2 param list".to_string();

    let output = Command::new("bash")
        .args(["-c", &command_str])
        .output()
        .await?;

    let _duration = start_time.elapsed();
    let stderr = String::from_utf8_lossy(&output.stderr);
    let stdout = String::from_utf8_lossy(&output.stdout);

    crate::debug_log(&format!("Command exit status: {}", output.status));
    crate::debug_log(&format!("Command stderr: '{}'", stderr));
    crate::debug_log(&format!("Command stdout: '{}'", stdout));

    if !output.status.success() {
        crate::debug_log(&format!(
            "ros2 param list command failed with status: {}",
            output.status
        ));
        return Err(ParamError::ParseError(format!(
            "ros2 param list failed: {} - stderr: {}",
            output.status, stderr
        )));
    }

    crate::debug_log(&format!("ros2 param list raw output: '{}'", stdout));
    let params = parse_param_list(&stdout)?;
    crate::debug_log(&format!("Parsed {} parameters from output", params.len()));

    Ok(params)
}

pub fn parse_param_list(output: &str) -> Result<Vec<ParamInfo>, ParamError> {
    let mut params = Vec::new();
    let mut current_node = String::new();

    crate::debug_log(&format!(
        "Parsing parameter list from {} lines",
        output.lines().count()
    ));

    for (line_num, line) in output.lines().enumerate() {
        let original_line = line;
        let trimmed_line = line.trim();
        crate::debug_log(&format!(
            "Line {}: '{}' (original: '{}')",
            line_num, trimmed_line, original_line
        ));

        if trimmed_line.is_empty()
            || trimmed_line.contains("WARN")
            || trimmed_line.contains("ERROR")
            || trimmed_line.starts_with("20")
        {
            // Added checks for WARN, ERROR, and timestamp
            crate::debug_log(&format!(
                "  -> Skipping non-parameter line: '{}'",
                trimmed_line
            ));
            continue;
        }

        // Check if this is a node name (starts with / and ends with :)
        if trimmed_line.starts_with('/') && trimmed_line.ends_with(':') {
            // Remove the trailing ':'
            current_node = trimmed_line[0..trimmed_line.len() - 1].to_string();
            crate::debug_log(&format!("  -> Found node: '{}'", current_node));
        } else if !current_node.is_empty() && original_line.starts_with(' ') {
            // This is a parameter name (indented under the node) - check original line for indentation
            let param_name = trimmed_line.to_string();
            crate::debug_log(&format!(
                "  -> Found parameter '{}' for node '{}'",
                param_name, current_node
            ));

            // Handle namespaced parameters (e.g., "ai_module.batch_size")
            if param_name.contains('.') {
                create_namespace_hierarchy(&mut params, &current_node, &param_name);
            } else {
                // Regular parameter (no namespace)
                params.push(ParamInfo {
                    node_name: current_node.clone(),
                    param_name,
                    value: None,
                    param_type: String::new(),
                });
            }
        } else {
            crate::debug_log(&format!("  -> Unmatched line: '{}'", trimmed_line));
        }
    }

    crate::debug_log(&format!(
        "Finished parsing - found {} parameters",
        params.len()
    ));
    Ok(params)
}

fn create_namespace_hierarchy(params: &mut Vec<ParamInfo>, node_name: &str, param_name: &str) {
    let parts: Vec<&str> = param_name.split('.').collect();

    // Create namespace entries for each level of hierarchy
    for i in 0..parts.len() {
        let current_path = parts[0..=i].join(".");
        let is_leaf = i == parts.len() - 1;

        // Check if this path already exists
        let exists = params
            .iter()
            .any(|p| p.node_name == node_name && p.param_name == current_path);

        if !exists {
            if is_leaf {
                // This is the actual parameter
                crate::debug_log(&format!("    -> Created parameter: '{}'", current_path));
                params.push(ParamInfo {
                    node_name: node_name.to_string(),
                    param_name: current_path,
                    value: None,
                    param_type: String::new(),
                });
            } else {
                // This is a namespace container
                crate::debug_log(&format!("    -> Created namespace: '{}'", current_path));
                params.push(ParamInfo {
                    node_name: node_name.to_string(),
                    param_name: current_path,
                    value: Some("namespace".to_string()),
                    param_type: "Namespace".to_string(),
                });
            }
        }
    }
}

/// Convert array values to ROS2-compatible format by removing spaces after commas
/// ROS2 treats spaces in arrays as separate command-line arguments, causing parse errors
/// - [true, false] -> [true,false]
/// - [1, 2, 3] -> [1,2,3]
pub fn convert_to_ros2_format(value: &str) -> String {
    // Handle array values like [true, false] -> [true,false] (remove spaces after commas)
    if value.starts_with('[') && value.ends_with(']') {
        let inner = &value[1..value.len() - 1]; // Remove brackets
        let elements: Vec<String> = inner
            .split(',')
            .map(|s| s.trim().to_string()) // Trim whitespace from each element
            .collect();
        format!("[{}]", elements.join(",")) // Join without spaces
    } else {
        // Single values don't need modification
        value.to_string()
    }
}

pub async fn set_param_value(
    node_name: &str,
    param_name: &str,
    value: &str,
) -> Result<String, ParamError> {
    // Convert value to ROS2-compatible format (remove spaces in arrays to avoid command-line parsing issues)
    let ros2_value = convert_to_ros2_format(value);
    let command_str = format!(
        "ros2 param set '{}' '{}' {}",
        node_name, param_name, ros2_value
    );
    crate::debug_log(&format!("Executing: {}", command_str));

    let output = Command::new("bash")
        .args(["-c", &command_str])
        .output()
        .await?;

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);

    // Check ROS2's actual response
    if stdout.contains("Set parameter successful") {
        return Ok("Set parameter successful".to_string());
    }

    // If we get a failure, return the original error message from stdout
    if stdout.contains("Setting parameter failed") {
        let error_msg = stdout
            .lines()
            .find(|line| line.contains("Setting parameter failed"))
            .unwrap_or("Setting parameter failed")
            .to_string();
        return Err(ParamError::ParseError(error_msg));
    }

    // If no clear success/failure indication, check stderr
    if !stderr.is_empty() {
        return Err(ParamError::ParseError(stderr.to_string()));
    }

    // Fallback error
    Err(ParamError::ParseError(
        "Unknown parameter setting result".to_string(),
    ))
}

pub async fn dump_params(node_name: &str, file_path: &str) -> Result<(), ParamError> {
    let command_str = format!("ros2 param dump {} > {}", node_name, file_path);

    let output = Command::new("bash")
        .args(["-c", &command_str])
        .output()
        .await?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        return Err(ParamError::ParseError(format!(
            "ros2 param dump failed: {}",
            stderr
        )));
    }

    Ok(())
}

pub async fn load_params(node_name: &str, file_path: &str) -> Result<(), ParamError> {
    let command_str = format!("ros2 param load {} {}", node_name, file_path);

    let output = Command::new("bash")
        .args(["-c", &command_str])
        .output()
        .await?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        return Err(ParamError::ParseError(format!(
            "ros2 param load failed: {}",
            stderr
        )));
    }

    Ok(())
}

pub async fn get_params_for_node(node_name: &str) -> Result<Vec<ParamInfo>, ParamError> {
    let command_str = format!("ros2 param list {}", node_name);

    let output = Command::new("bash")
        .args(["-c", &command_str])
        .output()
        .await?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        return Err(ParamError::ParseError(format!(
            "ros2 param list failed: {}",
            stderr
        )));
    }

    let stdout = String::from_utf8_lossy(&output.stdout);
    let mut params = Vec::new();

    for line in stdout.lines() {
        let line = line.trim();
        if !line.is_empty() && !line.starts_with(node_name) {
            params.push(ParamInfo {
                node_name: node_name.to_string(),
                param_name: line.to_string(),
                value: None,
                param_type: String::new(),
            });
        }
    }

    Ok(params)
}

// New improved implementation using ros2 param dump
pub async fn get_node_list() -> Result<Vec<String>, ParamError> {
    crate::debug_log("Executing: ros2 node list");
    let command_str = "ros2 node list".to_string();

    let output = Command::new("bash")
        .args(["-c", &command_str])
        .output()
        .await?;

    let stderr = String::from_utf8_lossy(&output.stderr);
    let stdout = String::from_utf8_lossy(&output.stdout);

    crate::debug_log(&format!("ros2 node list exit status: {}", output.status));
    crate::debug_log(&format!("ros2 node list stderr: '{}'", stderr));
    crate::debug_log(&format!("ros2 node list stdout: '{}'", stdout));

    if !output.status.success() {
        return Err(ParamError::ParseError(format!(
            "ros2 node list failed: {} - stderr: {}",
            output.status, stderr
        )));
    }

    let nodes: Vec<String> = stdout
        .lines()
        .map(|line| line.trim())
        .filter(|line| !line.is_empty() && line.starts_with('/'))
        .map(|line| line.to_string())
        .collect();

    crate::debug_log(&format!("Found {} nodes", nodes.len()));
    Ok(nodes)
}

pub async fn get_node_params_dump(node_name: &str) -> Result<Vec<ParamInfo>, ParamError> {
    crate::debug_log(&format!("Executing: ros2 param dump {}", node_name));
    let command_str = format!("ros2 param dump '{}'", node_name);

    let output = Command::new("bash")
        .args(["-c", &command_str])
        .output()
        .await?;

    let stderr = String::from_utf8_lossy(&output.stderr);
    let stdout = String::from_utf8_lossy(&output.stdout);

    crate::debug_log(&format!("ros2 param dump exit status: {}", output.status));
    crate::debug_log(&format!("ros2 param dump stderr: '{}'", stderr));
    crate::debug_log(&format!("ros2 param dump stdout length: {}", stdout.len()));

    if !output.status.success() {
        // Node might not have parameters, that's OK
        crate::debug_log(&format!(
            "Node {} has no parameters or dump failed: {}",
            node_name, stderr
        ));
        return Ok(Vec::new());
    }

    parse_param_dump_yaml(node_name, &stdout)
}

fn parse_param_dump_yaml(
    node_name: &str,
    yaml_content: &str,
) -> Result<Vec<ParamInfo>, ParamError> {
    crate::debug_log(&format!("Parsing YAML dump for node: {}", node_name));

    if yaml_content.trim().is_empty() {
        crate::debug_log("Empty YAML content");
        return Ok(Vec::new());
    }

    // Parse YAML
    let yaml_value: Value = serde_yaml::from_str(yaml_content)
        .map_err(|e| ParamError::ParseError(format!("Failed to parse YAML: {}", e)))?;

    let mut params = Vec::new();

    // The YAML structure is: /node_name: ros__parameters: { param1: value1, param2: value2, ... }
    if let Some(node_data) = yaml_value.get(node_name) {
        if let Some(ros_params) = node_data.get("ros__parameters") {
            if let Some(param_map) = ros_params.as_mapping() {
                parse_nested_params(&mut params, node_name, "", param_map);
            }
        }
    }

    crate::debug_log(&format!(
        "Parsed {} parameters from YAML dump",
        params.len()
    ));
    Ok(params)
}

fn parse_nested_params(
    params: &mut Vec<ParamInfo>,
    node_name: &str,
    prefix: &str,
    param_map: &serde_yaml::Mapping,
) {
    for (param_name, param_value) in param_map {
        if let Some(param_name_str) = param_name.as_str() {
            let full_param_name = if prefix.is_empty() {
                param_name_str.to_string()
            } else {
                format!("{}.{}", prefix, param_name_str)
            };

            if let Some(nested_map) = param_value.as_mapping() {
                // This is a nested namespace - recurse into it
                crate::debug_log(&format!("Found namespace: {}", full_param_name));
                parse_nested_params(params, node_name, &full_param_name, nested_map);
            } else {
                // This is an actual parameter value
                let (value_str, type_str) = extract_value_and_type(param_value);
                crate::debug_log(&format!(
                    "Parsed param: {} = {} ({})",
                    full_param_name, value_str, type_str
                ));

                params.push(ParamInfo {
                    node_name: node_name.to_string(),
                    param_name: full_param_name,
                    value: Some(value_str),
                    param_type: type_str,
                });
            }
        }
    }
}

/// Detect the element type of an array to match ROS2's individual param get behavior
/// Returns the same type names that ROS2 uses: "Boolean", "Integer", "Double", "String"
pub fn detect_array_element_type(seq: &[Value]) -> String {
    if seq.is_empty() {
        return "Array".to_string();
    }

    // Check the first element to determine array type
    // ROS2 arrays are homogeneous, so all elements should be the same type
    match &seq[0] {
        Value::Bool(_) => "Boolean".to_string(),
        Value::Number(n) => {
            if n.is_i64() {
                "Integer".to_string()
            } else {
                "Double".to_string()
            }
        }
        Value::String(_) => "String".to_string(),
        _ => "Array".to_string(), // Fallback for complex types
    }
}

fn format_array_value(seq: &[Value]) -> String {
    if seq.is_empty() {
        return "[]".to_string();
    }

    // Check if we should truncate for very long arrays
    const MAX_DISPLAY_ITEMS: usize = 8;
    const MAX_TOTAL_LENGTH: usize = 100;

    let mut elements = Vec::new();
    let mut total_length = 2; // For the brackets []
    let mut truncated = false;

    for (i, item) in seq.iter().enumerate() {
        if i >= MAX_DISPLAY_ITEMS {
            truncated = true;
            break;
        }

        let item_str = match item {
            Value::Bool(b) => b.to_string(),
            Value::Number(n) => n.to_string(),
            Value::String(s) => format!("\"{}\"", s),
            _ => format!("{:?}", item),
        };

        // Check if adding this item would exceed our length limit
        let separator_len = if i == 0 { 0 } else { 2 }; // ", "
        if total_length + separator_len + item_str.len() > MAX_TOTAL_LENGTH {
            truncated = true;
            break;
        }

        elements.push(item_str);
        total_length += separator_len + elements.last().unwrap().len();
    }

    let mut result = format!("[{}]", elements.join(", "));
    if truncated {
        // Remove the closing bracket and add ellipsis
        result.pop();
        result.push_str(", ...]");
    }

    result
}

fn extract_value_and_type(value: &Value) -> (String, String) {
    match value {
        Value::Bool(b) => (b.to_string(), "Boolean".to_string()),
        Value::Number(n) => {
            if n.is_i64() {
                (n.as_i64().unwrap().to_string(), "Integer".to_string())
            } else if n.is_f64() {
                let f_val = n.as_f64().unwrap();
                let f_str = f_val.to_string();
                // Ensure doubles always show with decimal point for type clarity
                if f_str.contains('.') {
                    (f_str, "Double".to_string())
                } else {
                    (format!("{}.0", f_str), "Double".to_string())
                }
            } else {
                (n.to_string(), "Number".to_string())
            }
        }
        Value::String(s) => (s.clone(), "String".to_string()),
        Value::Sequence(seq) => {
            let formatted_array = format_array_value(seq);
            let array_type = detect_array_element_type(seq);
            (formatted_array, array_type)
        }
        Value::Mapping(_) => (
            serde_yaml::to_string(value)
                .unwrap_or_default()
                .trim()
                .to_string(),
            "Object".to_string(),
        ),
        Value::Null => ("null".to_string(), "Null".to_string()),
        _ => (format!("{:?}", value), "Unknown".to_string()),
    }
}

// New improved get_param_list_with_values that gets all params with values in fewer ROS calls
pub async fn get_param_list_with_values() -> Result<Vec<ParamInfo>, ParamError> {
    crate::debug_log("Starting improved parameter fetch using ros2 param dump");

    // Step 1: Get list of all nodes
    let nodes = get_node_list().await?;
    crate::debug_log(&format!("Found {} nodes to query", nodes.len()));

    // Step 2: Get parameters for each node using dump
    let mut all_params = Vec::new();

    for node_name in nodes {
        match get_node_params_dump(&node_name).await {
            Ok(mut node_params) => {
                crate::debug_log(&format!(
                    "Node {} has {} parameters",
                    node_name,
                    node_params.len()
                ));
                all_params.append(&mut node_params);
            }
            Err(e) => {
                crate::debug_log(&format!(
                    "Failed to get parameters for node {}: {}",
                    node_name, e
                ));
                // Continue with other nodes
            }
        }
    }

    crate::debug_log(&format!("Total parameters collected: {}", all_params.len()));
    Ok(all_params)
}

/// Get parameters for a single node using ros2 param dump (more efficient than individual gets)
pub async fn get_node_params_with_values(node_name: &str) -> Result<Vec<ParamInfo>, ParamError> {
    crate::debug_log(&format!(
        "Getting parameters for node {} using ros2 param dump",
        node_name
    ));

    // Use ros2 param dump to get all parameters for this specific node
    let command_str = format!("ros2 param dump '{}'", node_name);
    let output = Command::new("bash")
        .args(["-c", &command_str])
        .output()
        .await?;

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);

    if !output.status.success() {
        return Err(ParamError::ParseError(format!(
            "ros2 param dump failed for {}: {}",
            node_name, stderr
        )));
    }

    // Parse the YAML output
    let yaml_value: Value = serde_yaml::from_str(&stdout).map_err(|e| {
        ParamError::ParseError(format!("Failed to parse YAML for {}: {}", node_name, e))
    })?;

    let mut params = Vec::new();

    // Process the YAML structure to extract parameters
    if let Value::Mapping(node_map) = yaml_value {
        if let Some((_, node_params)) = node_map.iter().next() {
            if let Some(nested_map) = node_params.as_mapping() {
                // Check if there's a ros__parameters key (single node dump format)
                if let Some(ros_params) = nested_map.get("ros__parameters") {
                    if let Some(ros_params_map) = ros_params.as_mapping() {
                        // Parse from ros__parameters, stripping the prefix
                        parse_nested_params(&mut params, node_name, "", ros_params_map);
                    }
                } else {
                    // Direct parameter format (multi-node dump format)
                    parse_nested_params(&mut params, node_name, "", nested_map);
                }
            }
        }
    }

    crate::debug_log(&format!(
        "Node {} has {} parameters",
        node_name,
        params.len()
    ));
    Ok(params)
}

/// Get a single parameter value using ros2 param dump (more reliable than ros2 param get)
pub async fn get_single_param_value(
    node_name: &str,
    param_name: &str,
) -> Result<(String, String), ParamError> {
    crate::debug_log(&format!(
        "Getting parameter {}/{} using ros2 param dump",
        node_name, param_name
    ));

    // Get all parameters for the node
    let params = get_node_params_with_values(node_name).await?;

    // Find the specific parameter
    for param in params {
        if param.param_name == param_name {
            if let Some(value) = param.value {
                return Ok((value, param.param_type));
            }
        }
    }

    Err(ParamError::ParseError(format!(
        "Parameter {}/{} not found",
        node_name, param_name
    )))
}