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 = 1000
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 }
79
80 let config_path = config_dir.join("rush.toml");
81 if !config_path.exists() {
82 fs::write(&config_path, DEFAULT_CONFIG)
83 .await
84 .map_err(AppError::Io)?;
85
86 log::info!(
87 "{}",
88 get_translation(
89 "system.config.file_created",
90 &[&config_path.display().to_string()]
91 )
92 );
93 }
94
95 Ok(config_path)
96}
97
98pub fn get_config_paths() -> Vec<PathBuf> {
99 let mut paths = Vec::new();
100 if let Ok(exe_path) = std::env::current_exe() {
101 if let Some(base_dir) = exe_path.parent() {
102 paths.push(base_dir.join(".rss/rush.toml"));
103 paths.push(base_dir.join("rush.toml"));
104 paths.push(base_dir.join("config/rush.toml"));
105 }
106 }
107 #[cfg(debug_assertions)]
108 {
109 paths.push(PathBuf::from("rush.toml"));
110 paths.push(PathBuf::from("src/rush.toml"));
111 }
112 paths
113}