Skip to main content

j_agent/
theme_name.rs

1//! 主题名称定义(不含 ratatui 依赖)
2//!
3//! Theme 结构体(含 ratatui Color)留在 j-cli 的 theme.rs 中。
4
5use serde::{Deserialize, Serialize};
6
7/// 主题名称枚举
8#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
9pub enum ThemeName {
10    #[serde(rename = "dark")]
11    Dark,
12    #[serde(rename = "light")]
13    Light,
14    #[serde(rename = "midnight")]
15    #[default]
16    Midnight,
17    #[serde(rename = "nord")]
18    Nord,
19    #[serde(rename = "monokai")]
20    Monokai,
21    #[serde(rename = "anthropic_light")]
22    AnthropicLight,
23    #[serde(rename = "anthropic_dark")]
24    AnthropicDark,
25}
26
27impl std::str::FromStr for ThemeName {
28    type Err = ();
29
30    fn from_str(s: &str) -> Result<Self, Self::Err> {
31        match s.to_lowercase().as_str() {
32            "dark" => Ok(ThemeName::Dark),
33            "light" => Ok(ThemeName::Light),
34            "midnight" => Ok(ThemeName::Midnight),
35            "nord" => Ok(ThemeName::Nord),
36            "monokai" => Ok(ThemeName::Monokai),
37            "anthropic_light" => Ok(ThemeName::AnthropicLight),
38            "anthropic_dark" => Ok(ThemeName::AnthropicDark),
39            _ => Ok(ThemeName::default()),
40        }
41    }
42}
43
44impl std::fmt::Display for ThemeName {
45    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46        match self {
47            ThemeName::Dark => write!(f, "dark"),
48            ThemeName::Light => write!(f, "light"),
49            ThemeName::Midnight => write!(f, "midnight"),
50            ThemeName::Nord => write!(f, "nord"),
51            ThemeName::Monokai => write!(f, "monokai"),
52            ThemeName::AnthropicLight => write!(f, "anthropic_light"),
53            ThemeName::AnthropicDark => write!(f, "anthropic_dark"),
54        }
55    }
56}
57
58impl ThemeName {
59    /// 获取所有主题名称列表
60    pub fn all() -> &'static [ThemeName] {
61        &[
62            ThemeName::Dark,
63            ThemeName::Light,
64            ThemeName::Midnight,
65            ThemeName::Nord,
66            ThemeName::Monokai,
67            ThemeName::AnthropicLight,
68            ThemeName::AnthropicDark,
69        ]
70    }
71
72    /// 切换到下一个主题
73    pub fn next(&self) -> ThemeName {
74        match self {
75            ThemeName::Dark => ThemeName::Light,
76            ThemeName::Light => ThemeName::Midnight,
77            ThemeName::Midnight => ThemeName::Nord,
78            ThemeName::Nord => ThemeName::Monokai,
79            ThemeName::Monokai => ThemeName::AnthropicLight,
80            ThemeName::AnthropicLight => ThemeName::AnthropicDark,
81            ThemeName::AnthropicDark => ThemeName::Dark,
82        }
83    }
84
85    /// 显示名称
86    pub fn display_name(&self) -> &'static str {
87        match self {
88            ThemeName::Dark => "Dark",
89            ThemeName::Light => "Light",
90            ThemeName::Midnight => "Midnight(默认)",
91            ThemeName::Nord => "Nord",
92            ThemeName::Monokai => "Monokai",
93            ThemeName::AnthropicLight => "Anthropic Light(米白赭陶)",
94            ThemeName::AnthropicDark => "Anthropic Dark(深夜月蓝)",
95        }
96    }
97
98    /// 从字符串解析
99    pub fn parse(s: &str) -> ThemeName {
100        s.parse().unwrap_or_default()
101    }
102
103    /// 转为字符串
104    pub fn to_str(&self) -> &'static str {
105        match self {
106            ThemeName::Dark => "dark",
107            ThemeName::Light => "light",
108            ThemeName::Midnight => "midnight",
109            ThemeName::Nord => "nord",
110            ThemeName::Monokai => "monokai",
111            ThemeName::AnthropicLight => "anthropic_light",
112            ThemeName::AnthropicDark => "anthropic_dark",
113        }
114    }
115}