nerve/cli/
output.rs

1//! Output Module
2//!
3//! Provides output formatting utilities for CLI commands.
4
5use crate::cli::CliConfig;
6
7/// Output formatter for CLI commands
8pub struct OutputFormatter {
9    config: CliConfig,
10}
11
12impl OutputFormatter {
13    /// Create a new output formatter
14    pub fn new(config: CliConfig) -> Self {
15        Self { config }
16    }
17
18    /// Format output based on configuration
19    pub fn format_output<T: serde::Serialize>(&self, data: &T) -> Result<String, Box<dyn std::error::Error>> {
20        match self.config.format.as_str() {
21            "json" => self.format_json(data),
22            "yaml" => self.format_yaml(data),
23            "text" => self.format_text(data),
24            _ => self.format_text(data),
25        }
26    }
27
28    /// Format as JSON
29    fn format_json<T: serde::Serialize>(&self, data: &T) -> Result<String, Box<dyn std::error::Error>> {
30        let json = serde_json::to_string_pretty(data)?;
31        Ok(json)
32    }
33
34    /// Format as YAML
35    fn format_yaml<T: serde::Serialize>(&self, data: &T) -> Result<String, Box<dyn std::error::Error>> {
36        let yaml = serde_yaml::to_string(data)?;
37        Ok(yaml)
38    }
39
40    /// Format as text (default)
41    fn format_text<T: serde::Serialize>(&self, _data: &T) -> Result<String, Box<dyn std::error::Error>> {
42        // For text format, we'll rely on the command-specific text output
43        // This is a placeholder that could be enhanced with custom text formatting
44        Ok("Text format output".to_string())
45    }
46
47    /// Print formatted output
48    pub fn print_output<T: serde::Serialize>(&self, data: &T) -> Result<(), Box<dyn std::error::Error>> {
49        let output = self.format_output(data)?;
50        println!("{}", output);
51        Ok(())
52    }
53}
54
55/// Helper functions for common output patterns
56pub mod helpers {
57    use std::fmt::Display;
58
59    /// Print a section header
60    pub fn print_section_header(title: &str) {
61        println!("\n{}", title);
62        println!("{}", "=".repeat(title.len()));
63        println!();
64    }
65
66    /// Print a key-value pair
67    pub fn print_key_value<T: Display>(key: &str, value: T) {
68        println!("  {}: {}", key, value);
69    }
70
71    /// Print a list item
72    pub fn print_list_item<T: Display>(item: T) {
73        println!("  • {}", item);
74    }
75
76    /// Print a warning message
77    pub fn print_warning(message: &str) {
78        println!("⚠️  {}", message);
79    }
80
81    /// Print an error message
82    pub fn print_error(message: &str) {
83        println!("❌ {}", message);
84    }
85
86    /// Print a success message
87    pub fn print_success(message: &str) {
88        println!("✅ {}", message);
89    }
90
91    /// Print an info message
92    pub fn print_info(message: &str) {
93        println!("ℹ️  {}", message);
94    }
95
96    /// Print a performance metric
97    pub fn print_performance_metric(metric: &str, value: &str, unit: &str) {
98        println!("  {}: {} {}", metric, value, unit);
99    }
100
101    /// Print a health status
102    pub fn print_health_status(status: &str) {
103        let icon = match status {
104            "Healthy" => "🟢",
105            "Warning" => "🟡",
106            "Critical" => "🔴",
107            _ => "⚪",
108        };
109        println!("{} {}", icon, status);
110    }
111}