rush_sync_server/commands/theme/
command.rs

1use super::ThemeSystem;
2use crate::commands::command::Command;
3use crate::core::prelude::*;
4use std::future::Future;
5use std::pin::Pin;
6
7#[derive(Debug)]
8pub struct ThemeCommand {
9    theme_system: std::sync::Mutex<Option<ThemeSystem>>,
10}
11
12impl ThemeCommand {
13    pub fn new() -> Self {
14        Self {
15            theme_system: std::sync::Mutex::new(None),
16        }
17    }
18
19    fn get_or_init_theme_system(&self) -> Result<std::sync::MutexGuard<Option<ThemeSystem>>> {
20        let mut guard = self.theme_system.lock().unwrap();
21        if guard.is_none() {
22            *guard = Some(ThemeSystem::load()?);
23        }
24        Ok(guard)
25    }
26}
27
28impl Command for ThemeCommand {
29    fn name(&self) -> &'static str {
30        "theme"
31    }
32
33    fn description(&self) -> &'static str {
34        "Change application theme (live update without restart, loaded from TOML)"
35    }
36
37    fn matches(&self, command: &str) -> bool {
38        command.trim().to_lowercase().starts_with("theme")
39    }
40
41    fn execute_sync(&self, args: &[&str]) -> Result<String> {
42        let mut guard = self.get_or_init_theme_system()?;
43        let theme_system = guard.as_mut().unwrap();
44
45        match args.first() {
46            None => Ok(theme_system.show_status()),
47            Some(&"--help" | &"-h") => Ok(Self::create_help_text(theme_system)),
48            Some(&"preview") => match args.get(1) {
49                Some(&theme_name) => theme_system.preview_theme(theme_name),
50                None => Ok("❌ Theme name missing. Usage: theme preview <name>".to_string()),
51            },
52            Some(&theme_name) => theme_system.change_theme(theme_name),
53        }
54    }
55
56    fn execute_async<'a>(
57        &'a self,
58        args: &'a [&'a str],
59    ) -> Pin<Box<dyn Future<Output = Result<String>> + Send + 'a>> {
60        Box::pin(async move { self.execute_sync(args) })
61    }
62
63    fn supports_async(&self) -> bool {
64        true
65    }
66
67    fn priority(&self) -> u8 {
68        65
69    }
70}
71
72impl ThemeCommand {
73    fn create_help_text(theme_system: &ThemeSystem) -> String {
74        let available_themes = theme_system.get_available_names();
75
76        if available_themes.is_empty() {
77            return "❌ Keine Themes verfügbar!\n\n📝 Füge [theme.xyz] Sektionen zur rush.toml hinzu:\n\n[theme.mein_theme]\ninput_text = \"White\"\ninput_bg = \"Black\"\ncursor = \"Green\"\noutput_text = \"Gray\"\noutput_bg = \"Black\"\nprompt_text = \">> \"\nprompt_color = \"Cyan\"\noutput_cursor = \"BLOCK\"    # ✅ NEU!\noutput_color = \"LightGreen\" # ✅ NEU!".to_string();
78        }
79
80        let themes_list = available_themes.join(", ");
81
82        format!(
83            "🎨 TOML-Theme Commands (Live Update - Geladen aus rush.toml!):\n\
84            theme                Show available TOML-themes\n\
85            theme <name>         Select theme: {}\n\
86            theme preview <name> Preview theme colors + cursor config ✅ NEW!\n\
87            theme -h             Show this help\n\n\
88            ✨ Alle Themes werden LIVE aus [theme.*] Sektionen der rush.toml geladen!\n\
89            🎯 NEU: Cursor-Konfiguration per output_cursor + output_color!\n\
90            📁 Füge beliebige [theme.dein_name] Sektionen hinzu für neue Themes\n\
91            🔄 Änderungen werden sofort angewendet (kein Restart nötig)\n\n\
92            🎛️ Cursor-Optionen:\n\
93            • output_cursor: DEFAULT, BLOCK, PIPE, UNDERSCORE\n\
94            • output_color: Jede unterstützte Farbe (White, Green, etc.)",
95            themes_list
96        )
97    }
98}
99
100impl Default for ThemeCommand {
101    fn default() -> Self {
102        Self::new()
103    }
104}