thag_rs 0.2.0

A versatile cross-platform playground and REPL for Rust snippets, expressions and programs. Accepts a script file or dynamic options.
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
/*[toml]
[dependencies]
thag_styling = { version = "0.2, thag-auto", features = ["image_themes", "inquire_theming"] }
*/

#![allow(clippy::uninlined_format_args)]
/// Generate `thag_styling` themes from image files using file navigator.
///
/// This tool analyzes image files to extract dominant colors and generates
/// `thag_styling`-compatible theme files. Supports auto-detection of theme type
/// (light/dark) and customizable color extraction parameters.
//# Purpose: Generate custom `thag_styling` themes from images
//# Categories: color, styling, terminal, theming, tools
use inquire::set_global_render_config; // For optional theming of `inquire`
use std::error::Error;
use std::fs;
use thag_styling::{
    file_navigator, sprtln, theme_to_toml, themed_inquire_config, ImageThemeConfig,
    ImageThemeGenerator, Role, Styleable, StylingError, StylingResult, TermBgLuma, Theme,
};

file_navigator! {}

fn main() -> Result<(), Box<dyn Error>> {
    println!(
        "🖼️  {} - Image to Theme Generator",
        "thag_image_to_theme".info()
    );
    println!("{}", "=".repeat(60));
    println!();

    set_global_render_config(themed_inquire_config());

    // Initialize file navigator
    let mut navigator = FileNavigator::new();

    // Configure supported image extensions
    let _image_extensions = vec![
        "png", "jpg", "jpeg", "gif", "bmp", "tiff", "tif", "webp", "PNG", "JPG", "JPEG", "GIF",
        "BMP", "TIFF", "TIF", "WEBP",
    ];

    println!("📁 Select an image file to analyze:");
    println!("   Supported formats: PNG, JPEG, GIF, BMP, TIFF, WebP");
    println!();

    // Use file navigator to select image file
    let Ok(image_path) = select_file(
        &mut navigator,
        Some("png,jpg,jpeg,gif,bmp,tiff,tif,webp"),
        false,
    ) else {
        println!("\n❌ No image file selected. Exiting.");
        return Ok(());
    };

    println!(
        "📷 Selected image: {}",
        image_path.display().to_string().success()
    );
    println!();

    // Get configuration from user
    let config = get_theme_config()?;

    // Generate theme from image
    println!("🎨 Analyzing image and generating theme...");
    let generator = ImageThemeGenerator::with_config(config);

    let mut theme = match generator.generate_from_file(&image_path) {
        Ok(theme) => theme,
        Err(e) => {
            sprtln!(Role::Error, "❌ Failed to generate theme: {}", e);
            return Err(e.into());
        }
    };

    let image_name = image_path
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or("image");

    let theme_type_suffix = match theme.term_bg_luma {
        TermBgLuma::Light => "light",
        TermBgLuma::Dark => "dark",
        TermBgLuma::Undetermined => "",
    };

    // Set the theme name
    let default_name = format!(
        "thag-{}-{theme_type_suffix}",
        image_name.to_lowercase().replace(' ', "-"),
    );
    theme.name = Text::new("Enter theme name:")
        .with_default(&default_name)
        .prompt()?;

    // Display theme information
    display_theme_info(&theme);

    // Save theme to file
    save_theme_file(&theme, &mut navigator)?;

    println!("\n🎉 Theme generation completed successfully!");
    Ok(())
}

/// Get theme configuration from user input
fn get_theme_config() -> Result<ImageThemeConfig, Box<dyn Error>> {
    use inquire::{Confirm, CustomType, Select};

    // Theme type detection
    let theme_type_options = vec![
        "Auto-detect from image brightness",
        "Force light theme",
        "Force dark theme",
    ];

    let theme_type_choice = Select::new("Theme type:", theme_type_options).prompt()?;

    let (auto_detect, force_type) = match theme_type_choice {
        "Force light theme" => (false, Some(TermBgLuma::Light)),
        "Force dark theme" => (false, Some(TermBgLuma::Dark)),
        _ => (true, None),
    };

    // Color count
    let color_count: usize = CustomType::new("Number of colors to extract:")
        .with_default(16)
        .with_help_message("Recommended: 8-32 colors")
        .prompt()?;

    let color_count = color_count.clamp(8, 64);

    // Advanced options
    let show_advanced = Confirm::new("Configure advanced options?")
        .with_default(false)
        .prompt()?;

    let (
        light_threshold,
        saturation_threshold,
        saturation_multiplier,
        lightness_adjustment,
        contrast_multiplier,
    ) = if show_advanced {
        let light_thresh: f32 = CustomType::new("Light threshold (0.0-1.0):")
            .with_default(0.7)
            .with_help_message("Higher values = more strict light theme detection")
            .prompt()?;

        let sat_thresh: f32 = CustomType::new("Saturation threshold (0.0-1.0):")
            .with_default(0.3)
            .with_help_message("Higher values = more saturated colors required")
            .prompt()?;

        println!();
        println!("🎨 Fine-tuning Controls:");

        let sat_mult: f32 = CustomType::new("Saturation multiplier (0.5-2.0):")
            .with_default(1.0)
            .with_help_message("1.0=natural, <1.0=muted, >1.0=vivid")
            .prompt()?;

        let light_adj: f32 = CustomType::new("Lightness adjustment (-0.3 to +0.3):")
            .with_default(0.0)
            .with_help_message("Negative=darker, positive=brighter")
            .prompt()?;

        let contrast_mult: f32 = CustomType::new("Contrast multiplier (0.5-1.5):")
            .with_default(1.0)
            .with_help_message("1.0=balanced, <1.0=subtle, >1.0=high contrast")
            .prompt()?;

        (
            light_thresh.clamp(0.0, 1.0),
            sat_thresh.clamp(0.0, 1.0),
            sat_mult.clamp(0.5, 2.0),
            light_adj.clamp(-0.3, 0.3),
            contrast_mult.clamp(0.5, 1.5),
        )
    } else {
        (0.7, 0.3, 1.0, 0.0, 1.0)
    };

    Ok(ImageThemeConfig {
        color_count,
        light_threshold,
        saturation_threshold,
        auto_detect_theme_type: auto_detect,
        force_theme_type: force_type,
        theme_name_prefix: None,
        saturation_multiplier,
        lightness_adjustment,
        contrast_multiplier,
    })
}

/// Display comprehensive theme information
fn display_theme_info(theme: &Theme) {
    println!("{} generated successfully!", "Theme".success());
    println!();

    println!("📋 {} Information:", "Theme".info());
    println!("   Name: {}", theme.name.info());
    println!("   Description: {}", theme.description);
    println!("   Type: {:?}", theme.term_bg_luma);
    println!("   Color Support: {:?}", theme.min_color_support);
    println!("   Backgrounds: {:?}", theme.bg_rgbs);
    println!();

    println!("🎨 {} Preview:", "Color Palette".info());
    display_color_palette(theme);
}

/// Display color palette with visual preview
fn display_color_palette(theme: &Theme) {
    let palette_items = [
        ("Normal", &theme.palette.normal),
        ("Subtle", &theme.palette.subtle),
        ("Emphasis", &theme.palette.emphasis),
        ("Heading1", &theme.palette.heading1),
        ("Heading2", &theme.palette.heading2),
        ("Heading3", &theme.palette.heading3),
        ("Error", &theme.palette.error),
        ("Warning", &theme.palette.warning),
        ("Success", &theme.palette.success),
        ("Info", &theme.palette.info),
        ("Code", &theme.palette.code),
        ("Hint", &theme.palette.hint),
        ("Debug", &theme.palette.debug),
        ("Link", &theme.palette.link),
        ("Quote", &theme.palette.quote),
        ("Commentary", &theme.palette.commentary),
    ];

    for (name, style) in palette_items {
        let styled_name = style.paint(format!("{:>12}", name));
        let rgb_info = extract_rgb_info(style);
        println!("   {styled_name} {rgb_info}");
    }

    // Show background colors
    if let Some([r, g, b]) = theme.bg_rgbs.first() {
        println!();
        print!("   Background: ");
        // Create background color preview
        for _ in 0..12 {
            print!("\x1b[48;2;{r};{g};{b}m \x1b[0m");
        }
        println!(" RGB({r}, {g}, {b})");
    }
    println!();
}

/// Extract RGB information from a style for display
fn extract_rgb_info(style: &thag_styling::Style) -> String {
    style.foreground.as_ref().map_or_else(
        || "No color".to_string(),
        |color_info| match &color_info.value {
            thag_styling::ColorValue::TrueColor { rgb } => {
                format!(
                    "#{:02x}{:02x}{:02x} RGB({}, {}, {})",
                    rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2]
                )
            }
            thag_styling::ColorValue::Color256 { color256 } => {
                format!("Color256({})", color256)
            }
            thag_styling::ColorValue::Basic { index, .. } => {
                format!("ANSI({})", index)
            }
        },
    )
}

/// Save theme to a TOML file using file navigator
fn save_theme_file(theme: &Theme, navigator: &mut FileNavigator) -> StylingResult<()> {
    use inquire::{Confirm, Text};

    // Ask if user wants to save the theme
    let should_save = Confirm::new("Save theme to file?")
        .with_default(true)
        .prompt()
        .map_err(|e| StylingError::FromStr(format!("Input error: {}", e)))?;

    if !should_save {
        println!("Theme not saved.");
        return Ok(());
    }

    // Get output directory
    println!("\n📁 Select directory to save theme file:");
    let Ok(output_dir) = select_directory(navigator, true) else {
        println!("❌ No directory selected. Theme not saved.");
        return Ok(());
    };

    // Get filename
    let default_filename = format!("{}.toml", theme.name);
    let filename = Text::new("Theme filename:")
        .with_default(&default_filename)
        .with_help_message("Will be saved with .toml extension")
        .prompt()
        .map_err(|e| StylingError::FromStr(format!("Input error: {}", e)))?;

    let filename = if std::path::Path::new(&filename)
        .extension()
        .is_some_and(|ext| ext.eq_ignore_ascii_case("toml"))
    {
        filename
    } else {
        format!("{}.toml", filename)
    };

    let output_path = output_dir.join(&filename);

    // Check if file exists
    if output_path.exists() {
        let overwrite = Confirm::new(&format!("File '{}' already exists. Overwrite?", filename))
            .with_default(false)
            .prompt()
            .map_err(|e| StylingError::FromStr(format!("Input error: {}", e)))?;

        if !overwrite {
            println!("Theme not saved.");
            return Ok(());
        }
    }

    // Generate TOML content
    let toml_content = theme_to_toml(theme)
        .map_err(|e| StylingError::FromStr(format!("TOML generation failed: {}", e)))?;

    // Write to file
    fs::write(&output_path, &toml_content)
        .map_err(|e| StylingError::FromStr(format!("Failed to write theme file: {}", e)))?;

    println!(
        "💾 Theme saved to: {}",
        output_path.display().to_string().success()
    );

    // Ask if user wants to view the TOML content
    let show_toml = Confirm::new("Display TOML content?")
        .with_default(false)
        .prompt()
        .map_err(|e| StylingError::FromStr(format!("Input error: {}", e)))?;

    if show_toml {
        println!();
        println!("📄 {} Content:", "TOML".info());
        println!("{}", "".repeat(60));
        println!("{}", toml_content);
    }

    println!();
    println!("💡 {} To use this theme:", "Tip:".warning());
    println!("   • Copy to your thag themes directory");
    println!("   • Use with thag_gen_terminal_themes to export to terminal formats");
    println!("   • Reference in your thag configuration");

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::PathBuf;
    use thag_styling::{ColorSupport, Palette, TermBgLuma};

    fn create_test_theme() -> Theme {
        Theme {
            name: "Test Theme".to_string(),
            filename: PathBuf::from("test_theme.toml"),
            is_builtin: false,
            term_bg_luma: TermBgLuma::Dark,
            min_color_support: ColorSupport::TrueColor,
            palette: Palette::default(),
            backgrounds: vec!["#2a2a2a".to_string()],
            bg_rgbs: vec![[42, 42, 42]],
            description: "Test theme for unit tests".to_string(),
            base_colors: None,
        }
    }

    #[test]
    fn test_extract_rgb_info() {
        use thag_styling::{ColorInfo, Style};

        let style = Style::fg(ColorInfo::rgb(255, 128, 64));
        let info = extract_rgb_info(&style);
        assert!(info.contains("ff8040"));
        assert!(info.contains("255, 128, 64"));
    }

    #[test]
    fn test_theme_creation() {
        let theme = create_test_theme();
        assert_eq!(theme.name, "Test Theme");
        assert!(!theme.bg_rgbs.is_empty());
    }

    #[test]
    fn test_image_theme_config_defaults() {
        let config = ImageThemeConfig::default();
        assert_eq!(config.color_count, 16);
        assert!(config.auto_detect_theme_type);
        assert!(config.force_theme_type.is_none());
    }
}