chromacat 0.1.0

A versatile command-line tool for applying color gradients to text output
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
//! Command-line interface for ChromaCat
//!
//! This module provides the command-line argument parsing and validation for ChromaCat.
//! It handles all user input configuration and converts it into the internal configuration
//! types used by the pattern engine and renderer.

use crate::error::{ChromaCatError, Result};
use crate::pattern::{CommonParams, PatternConfig, REGISTRY, ParamType};
use crate::renderer::AnimationConfig;
use crate::themes;
use crate::cli_format::{CliFormat, PadToWidth};

use clap::Parser;
use std::path::PathBuf;
use std::time::Duration;

/// ChromaCat - A versatile command-line tool for applying animated color gradients to text
#[derive(Parser, Debug)]
#[command(
    author, 
    version,
    about = format!("😺 {}Chroma{}Cat{} - Create magical color gradients for your text ✨", 
        CliFormat::TITLE_1, CliFormat::TITLE_2, CliFormat::RESET),
    long_about = None,
    help_template = "{about}\n\nUsage: {usage}\n\n{options}",
    styles = clap::builder::Styles::styled()
        .header(anstyle::AnsiColor::BrightMagenta.on_default())
        .usage(anstyle::AnsiColor::BrightCyan.on_default())
        .literal(anstyle::AnsiColor::BrightYellow.on_default())
)]
pub struct Cli {
    #[arg(
        name = "FILES",
        help_heading = CliFormat::HEADING_INPUT,
        value_name = "FILE",
        help = CliFormat::highlight_description("Input files (reads from stdin if none provided)")
    )]
    pub files: Vec<PathBuf>,

    #[arg(
        short = 'p',
        long,
        default_value = "diagonal",
        help_heading = CliFormat::HEADING_CORE,
        value_name = "TYPE",
        help = CliFormat::highlight_description("Select pattern type for the color gradient")
    )]
    pub pattern: String,

    #[arg(
        short = 't',
        long,
        default_value = "rainbow",
        help_heading = CliFormat::HEADING_CORE,
        value_name = "NAME",
        help = CliFormat::highlight_description("Select color theme (use --list to see available)")
    )]
    pub theme: String,

    #[arg(
        short = 'f',
        long,
        default_value = "1.0",
        help_heading = CliFormat::HEADING_CORE,
        value_name = "NUM",
        help = CliFormat::highlight_description("Base frequency (0.1-10.0)")
    )]
    pub frequency: f64,

    #[arg(
        short = 'm',
        long,
        default_value = "1.0",
        help_heading = CliFormat::HEADING_CORE,
        value_name = "NUM",
        help = CliFormat::highlight_description("Pattern amplitude (0.1-2.0)")
    )]
    pub amplitude: f64,

    #[arg(
        short = 'a',
        long,
        help_heading = CliFormat::HEADING_ANIMATION,
        help = CliFormat::highlight_description("Enable animation mode")
    )]
    pub animate: bool,

    #[arg(
        long,
        default_value = "30",
        help_heading = CliFormat::HEADING_ANIMATION,
        value_name = "NUM",
        help = CliFormat::highlight_description("Frames per second (1-144)")
    )]
    pub fps: u32,

    #[arg(
        long,
        default_value = "0",
        help_heading = CliFormat::HEADING_ANIMATION,
        value_name = "SECS",
        help = CliFormat::highlight_description("Duration in seconds (0 = infinite)")
    )]
    pub duration: u64,

    #[arg(
        short = 's',
        long,
        default_value = "1.0",
        help_heading = CliFormat::HEADING_ANIMATION,
        value_name = "NUM",
        help = CliFormat::highlight_description("Animation speed (0.0-1.0)")
    )]
    pub speed: f64,

    #[arg(
        long,
        help_heading = CliFormat::HEADING_ANIMATION,
        help = CliFormat::highlight_description("Enable smooth transitions")
    )]
    pub smooth: bool,

    #[arg(
        short = 'n',
        long = "no-color",
        help_heading = CliFormat::HEADING_GENERAL,
        help = CliFormat::highlight_description("Disable colored output")
    )]
    pub no_color: bool,

    #[arg(
        short = 'l',
        long = "list",
        help_heading = CliFormat::HEADING_GENERAL,
        help = CliFormat::highlight_description("Show available themes and patterns")
    )]
    pub list_available: bool,

    #[arg(
        long = "theme-file",
        value_name = "FILE",
        help_heading = CliFormat::HEADING_CORE,
        help = CliFormat::highlight_description("Load custom theme from YAML file")
    )]
    pub theme_file: Option<PathBuf>,

    #[arg(
        long = "param",
        value_name = "KEY=VALUE",
        help_heading = CliFormat::HEADING_CORE,
        help = CliFormat::highlight_description("Pattern-specific parameter (can be used multiple times)")
    )]
    pub params: Vec<String>,

    #[arg(
        long = "pattern-help",
        help_heading = CliFormat::HEADING_GENERAL,
        help = CliFormat::highlight_description("Show detailed help for pattern parameters")
    )]
    pub pattern_help: bool,

    #[arg(
        long = "no-aspect-correction",
        help_heading = CliFormat::HEADING_GENERAL,
        help = CliFormat::highlight_description("Disable terminal character aspect ratio correction")
    )]
    pub no_aspect_correction: bool,

    #[arg(
        long = "aspect-ratio",
        value_name = "RATIO",
        default_value = "0.5",
        help_heading = CliFormat::HEADING_GENERAL,
        help = CliFormat::highlight_description("Set terminal character aspect ratio (width/height, default: 0.5)")
    )]
    pub aspect_ratio: f64,

    #[arg(
        long = "buffer-size",
        value_name = "BYTES",
        help_heading = CliFormat::HEADING_CORE,
        help = CliFormat::highlight_description("Set input buffer size for streaming mode (default: 8192)")
    )]
    pub buffer_size: Option<usize>,

    #[arg(
        long = "demo",
        help_heading = CliFormat::HEADING_GENERAL,
        help = CliFormat::highlight_description("Run in demo mode with generated patterns")
    )]
    pub demo: bool,
}

impl Cli {
    /// Creates pattern configuration from CLI arguments
    pub fn create_pattern_config(&self) -> Result<PatternConfig> {
        let common = CommonParams {
            frequency: self.frequency,
            amplitude: self.amplitude,
            speed: self.speed,
            correct_aspect: !self.no_aspect_correction,
            aspect_ratio: self.aspect_ratio,
            theme_name: Some(self.theme.clone()),
        };

        // Get pattern params from registry
        let pattern_params = if self.params.is_empty() {
            // Use default parameters
            REGISTRY.create_pattern_params(&self.pattern)
                .ok_or_else(|| ChromaCatError::PatternError {
                    pattern: self.pattern.clone(),
                    param: String::new(),
                    message: "Unknown pattern type".to_string(),
                })?
        } else {
            // Parse provided parameters
            let params_str = self.params.join(",");
            REGISTRY.parse_params(&self.pattern, &params_str)
                .map_err(|e| ChromaCatError::PatternError {
                    pattern: self.pattern.clone(),
                    param: "params".to_string(),
                    message: e,
                })?
        };

        Ok(PatternConfig {
            common,
            params: pattern_params,
        })
    }

    /// Creates animation configuration from CLI arguments
    pub fn create_animation_config(&self) -> AnimationConfig {
        AnimationConfig {
            fps: self.fps.clamp(1, 144),
            cycle_duration: if self.duration == 0 {
                Duration::from_secs(u64::MAX)
            } else {
                Duration::from_secs(self.duration)
            },
            infinite: self.duration == 0,
            show_progress: true,
            smooth: self.smooth,
        }
    }

    /// Validates the CLI arguments
    pub fn validate(&self) -> Result<()> {
        // Skip validation if just listing options
        if self.list_available {
            return Ok(());
        }

        // Validate animation parameters
        if self.fps < 1 || self.fps > 144 {
            return Err(ChromaCatError::InvalidParameter {
                name: "fps".to_string(),
                value: self.fps as f64,
                min: 1.0,
                max: 144.0,
            });
        }

        // Validate input files exist
        for path in &self.files {
            if !path.exists() {
                return Err(ChromaCatError::InputError(format!(
                    "Input file not found: {}",
                    path.display()
                )));
            }
        }

        // Validate theme exists
        themes::get_theme(&self.theme)?;

        // Validate common parameters
        self.validate_range("frequency", self.frequency, 0.1, 10.0)?;
        self.validate_range("amplitude", self.amplitude, 0.1, 2.0)?;
        self.validate_range("speed", self.speed, 0.0, 1.0)?;

        // Validate pattern exists and its parameters
        if !self.params.is_empty() {
            let params_str = self.params.join(",");
            REGISTRY.validate_params(&self.pattern, &params_str)
                .map_err(|e| ChromaCatError::PatternError {
                    pattern: self.pattern.clone(),
                    param: "params".to_string(),
                    message: e,
                })?;
        }

        // Validate aspect ratio
        self.validate_range("aspect-ratio", self.aspect_ratio, 0.1, 2.0)?;

        Ok(())
    }

    /// Validates a parameter is within the specified range
    fn validate_range(&self, name: &str, value: f64, min: f64, max: f64) -> Result<()> {
        if value < min || value > max {
            return Err(ChromaCatError::InvalidParameter {
                name: name.to_string(),
                value,
                min,
                max,
            });
        }
        Ok(())
    }

    /// Prints available themes and patterns
    pub fn print_available_options() {
        // Title and introduction
        println!("\n{}", CliFormat::wrap(CliFormat::TITLE_1, "✨ ChromaCat Help ✨"));
        println!("{}", CliFormat::separator(&"═".repeat(90)));
        println!("\n{}", CliFormat::highlight_description(
            "ChromaCat is a command-line tool that adds beautiful color gradients to text output. \
            It supports various patterns, themes, and animated effects to make your terminal more colorful."
        ));

        // Patterns section
        println!("\n{}", CliFormat::core("Available Patterns:"));
        println!("{}", CliFormat::separator(&"─".repeat(85)));
        
        for pattern_id in REGISTRY.list_patterns() {
            if let Some(metadata) = REGISTRY.get_pattern(pattern_id) {
                println!("  {} {}",
                    CliFormat::param(&format!("{:<12}", metadata.name)),
                    CliFormat::description(metadata.description)
                );
            }
        }

        println!("\n{}", CliFormat::general("Use --pattern-help for detailed pattern parameters"));

        Self::print_themes();
        Self::print_usage_examples();
    }

    pub fn print_pattern_help() {
        // Title and introduction
        println!("\n{}", CliFormat::wrap(CliFormat::TITLE_1, "✨ ChromaCat Pattern Reference ✨"));
        println!("{}", CliFormat::separator(&"═".repeat(90)));
        println!("\n{}", CliFormat::highlight_description(
            "Each pattern supports specific parameters that can be customized using the --param flag. \
            Multiple parameters can be specified using comma separation: --param key1=value1,key2=value2"
        ));

        for pattern_id in REGISTRY.list_patterns() {
            if let Some(metadata) = REGISTRY.get_pattern(pattern_id) {
                // Pattern header
                println!("\n{} {}",
                    CliFormat::core(&format!("▶ {}", metadata.name)),
                    CliFormat::description(metadata.description)
                );

                // Parameter table
                let params = metadata.params().sub_params();
                if !params.is_empty() {
                    println!("{}", CliFormat::separator(&"─".repeat(85)));
                    println!("  {}  {}  {}",
                        CliFormat::param(&"Parameter".pad_to_width(20)),
                        CliFormat::param_value(&"Value Range".pad_to_width(20)),
                        CliFormat::param("Description")
                    );
                    println!("{}", CliFormat::separator(&"".repeat(85)));

                    for param in params {
                        let range = match param.param_type() {
                            ParamType::Number { min, max } => format!("{} to {}", min, max),
                            ParamType::Boolean => "true/false".to_string(),
                            ParamType::Enum { options } => options.join(", "),
                            _ => String::new(),
                        };

                        println!("  {}  {}  {}",
                            CliFormat::param(&format!("{}=", param.name()).pad_to_width(20)),
                            CliFormat::param_value(&range.pad_to_width(20)),
                            CliFormat::description(param.description())
                        );
                    }
                }

                // Example usage
                println!("\n  {} {}",
                    CliFormat::param("Example:"),
                    CliFormat::param_value(&format!("chromacat -p {} --param frequency=1.5 input.txt", pattern_id))
                );
                println!("{}", CliFormat::separator(&"─".repeat(85)));
            }
        }
    }

    fn print_themes() {
        println!("\n{}", CliFormat::core("🎨 Available Themes"));
        println!("{}", CliFormat::separator(&"─".repeat(85)));

        for category in themes::list_categories() {
            println!("\n  {}", CliFormat::param(&category));
            if let Some(theme_names) = themes::list_category(&category) {
                for name in theme_names {
                    if let Ok(theme) = themes::get_theme(&name) {
                        let preview = Self::create_theme_preview(&theme);
                        println!(
                            "    {} {} {}",
                            CliFormat::param_value(&format!("{:<15}", name)),
                            preview,
                            CliFormat::description(&theme.desc)
                        );
                    }
                }
            }
        }
    }

    fn create_theme_preview(theme: &themes::ThemeDefinition) -> String {
        if let Ok(gradient) = theme.create_gradient() {
            let mut preview = String::new();
            for i in 0..30 {
                let t = i as f32 / 29.0;
                let color = gradient.at(t);
                let r = (color.r * 255.0) as u8;
                let g = (color.g * 255.0) as u8;
                let b = (color.b * 255.0) as u8;
                preview.push_str(&format!("\x1b[48;2;{};{};{}m \x1b[0m", r, g, b));
            }
            preview
        } else {
            " ".repeat(30)
        }
    }

    fn print_usage_examples() {
        println!("\n{}", CliFormat::core("📚 Usage Examples"));
        println!("{}", CliFormat::separator(&"─".repeat(85)));

        let examples = [
            ("Basic file colorization:", "chromacat input.txt"),
            ("Using a specific theme:", "chromacat -t ocean input.txt"),
            ("Animated output:", "chromacat -a --fps 60 input.txt"),
            ("Pipe from another command:", "ls -la | chromacat -t neon"),
            ("Pattern with parameters:", "chromacat -p wave --param amplitude=1.5,frequency=2.0 input.txt"),
            ("Multiple files:", "chromacat -a *.txt"),
            ("Custom diagonal gradient:", "chromacat -p diagonal --param angle=45,speed=0.8 input.txt"),
            ("Interactive plasma:", "chromacat -p plasma --param complexity=3.0,scale=1.5 -a input.txt"),
        ];

        for (desc, cmd) in examples {
            println!("  {} {}",
                CliFormat::param(&format!("{:<25}", desc)),
                CliFormat::param_value(cmd)
            );
        }
    }
}