rush_sync_server/setup/
setup_toml.rs1use crate::core::prelude::*;
6use std::path::PathBuf;
7use tokio::fs;
8
9const DEFAULT_CONFIG: &str = r#"[general]
11max_messages = 100
12typewriter_delay = 5
13input_max_length = 100
14max_history = 30
15poll_rate = 16
16log_level = "info"
17current_theme = "dark"
18
19[language]
20current = "en"
21
22[theme.dark]
23output_bg = "Black"
24output_text = "White"
25output_cursor = "PIPE"
26output_cursor_color = "White"
27input_bg = "White"
28input_text = "Black"
29input_cursor_prefix = "/// "
30input_cursor = "PIPE"
31input_cursor_color = "Black"
32
33[theme.light]
34output_bg = "White"
35output_text = "Black"
36output_cursor = "PIPE"
37output_cursor_color = "Black"
38input_bg = "Black"
39input_text = "White"
40input_cursor_prefix = "/// "
41input_cursor = "PIPE"
42input_cursor_color = "White"
43
44[theme.green]
45output_bg = "Black"
46output_text = "Green"
47output_cursor = "BLOCK"
48output_cursor_color = "Green"
49input_bg = "LightGreen"
50input_text = "Black"
51input_cursor_prefix = "$ "
52input_cursor = "BLOCK"
53input_cursor_color = "Black"
54
55[theme.blue]
56output_bg = "White"
57output_text = "LightBlue"
58output_cursor = "UNDERSCORE"
59output_cursor_color = "Blue"
60input_bg = "Blue"
61input_text = "White"
62input_cursor_prefix = "> "
63input_cursor = "UNDERSCORE"
64input_cursor_color = "White"
65"#;
66
67pub async fn ensure_config_exists() -> Result<PathBuf> {
68 let exe_path = std::env::current_exe().map_err(AppError::Io)?;
69 let base_dir = exe_path
70 .parent()
71 .ok_or_else(|| AppError::Validation(get_translation("system.config.dir_error", &[])))?;
72
73 let config_dir = base_dir.join(".rss");
74 if !config_dir.exists() {
75 fs::create_dir_all(&config_dir)
76 .await
77 .map_err(AppError::Io)?;
78 log::debug!(
79 "{}",
80 get_translation(
81 "system.config.dir_created",
82 &[&config_dir.display().to_string()]
83 )
84 );
85 }
86
87 let config_path = config_dir.join("rush.toml");
88 if !config_path.exists() {
89 fs::write(&config_path, DEFAULT_CONFIG)
90 .await
91 .map_err(AppError::Io)?;
92
93 log::info!(
94 "{}",
95 get_translation(
96 "system.config.file_created",
97 &[&config_path.display().to_string()]
98 )
99 );
100 }
101
102 Ok(config_path)
103}
104
105pub fn get_config_paths() -> Vec<PathBuf> {
106 let mut paths = Vec::new();
107 if let Ok(exe_path) = std::env::current_exe() {
108 if let Some(base_dir) = exe_path.parent() {
109 paths.push(base_dir.join(".rss/rush.toml"));
110 paths.push(base_dir.join("rush.toml"));
111 paths.push(base_dir.join("config/rush.toml"));
112 }
113 }
114 #[cfg(debug_assertions)]
115 {
116 paths.push(PathBuf::from("rush.toml"));
117 paths.push(PathBuf::from("src/rush.toml"));
118 }
119 paths
120}