Skip to main content

lx_cli/
config.rs

1use crate::sort::{SortField, SortOptions, SortOrder};
2use colored::Color;
3use serde::Deserialize;
4use std::fs;
5use std::path::PathBuf;
6
7#[derive(Debug, Deserialize, Clone, Default)]
8pub struct Config {
9    #[serde(default)]
10    pub colors: ColorConfig,
11    #[serde(default)]
12    pub icons: IconConfig,
13    #[serde(default)]
14    pub display: DisplayConfig,
15}
16
17#[derive(Debug, Deserialize, Clone)]
18pub struct ColorConfig {
19    #[serde(default = "default_directory_color")]
20    pub directory: String,
21    #[serde(default = "default_executable_color")]
22    pub executable: String,
23    #[serde(default = "default_regular_color")]
24    pub regular: String,
25    #[serde(default = "default_hidden_color")]
26    pub hidden: String,
27}
28
29#[derive(Debug, Deserialize, Clone)]
30pub struct IconConfig {
31    #[serde(default = "default_directory_icon")]
32    pub directory: String,
33    #[serde(default = "default_executable_icon")]
34    pub executable: String,
35    #[serde(default = "default_regular_icon")]
36    pub regular: String,
37    #[serde(default)]
38    pub colors: IconColorConfig,
39}
40
41#[derive(Debug, Deserialize, Clone)]
42pub struct IconColorConfig {
43    #[serde(default = "default_directory_icon_color")]
44    pub directory: String,
45    #[serde(default = "default_executable_icon_color")]
46    pub executable: String,
47    #[serde(default = "default_regular_icon_color")]
48    pub regular: String,
49    #[serde(default = "default_hidden_icon_color")]
50    pub hidden: String,
51}
52
53#[derive(Debug, Deserialize, Clone)]
54pub struct DisplayConfig {
55    #[serde(default = "default_column_spacing")]
56    pub column_spacing: usize,
57    #[serde(default = "default_max_rows")]
58    pub max_rows: usize,
59    #[serde(default = "default_compact_max_rows")]
60    pub compact_max_rows: usize,
61    #[serde(default)]
62    pub sort_field: Option<SortField>,
63    #[serde(default)]
64    pub sort_order: SortOrder,
65    #[serde(default = "default_datetime_format")]
66    pub datetime_format: String,
67    #[serde(default)]
68    pub tree: TreeConfig,
69    #[serde(default = "default_long_format_fields")]
70    pub long_format_fields: Vec<String>,
71    #[serde(default)]
72    pub long_format_titles: bool,
73    #[serde(default = "default_long_format_title_color")]
74    pub long_format_title_color: String,
75}
76
77#[derive(Debug, Deserialize, Clone)]
78pub struct TreeConfig {
79    #[serde(default = "default_tree_style")]
80    pub style: String,
81}
82
83impl Default for TreeConfig {
84    fn default() -> Self {
85        TreeConfig {
86            style: default_tree_style(),
87        }
88    }
89}
90
91impl Default for IconConfig {
92    fn default() -> Self {
93        IconConfig {
94            directory: default_directory_icon(),
95            executable: default_executable_icon(),
96            regular: default_regular_icon(),
97            colors: IconColorConfig::default(),
98        }
99    }
100}
101
102impl Default for IconColorConfig {
103    fn default() -> Self {
104        IconColorConfig {
105            directory: default_directory_icon_color(),
106            executable: default_executable_icon_color(),
107            regular: default_regular_icon_color(),
108            hidden: default_hidden_icon_color(),
109        }
110    }
111}
112
113impl Default for ColorConfig {
114    fn default() -> Self {
115        ColorConfig {
116            directory: default_directory_color(),
117            executable: default_executable_color(),
118            regular: default_regular_color(),
119            hidden: default_hidden_color(),
120        }
121    }
122}
123
124impl Default for DisplayConfig {
125    fn default() -> Self {
126        DisplayConfig {
127            column_spacing: default_column_spacing(),
128            max_rows: default_max_rows(),
129            compact_max_rows: default_compact_max_rows(),
130            sort_field: None,
131            sort_order: SortOrder::Asc,
132            datetime_format: default_datetime_format(),
133            tree: TreeConfig::default(),
134            long_format_fields: default_long_format_fields(),
135            long_format_titles: false,
136            long_format_title_color: default_long_format_title_color(),
137        }
138    }
139}
140
141fn default_directory_color() -> String {
142    "blue".to_string()
143}
144
145fn default_executable_color() -> String {
146    "red".to_string()
147}
148
149fn default_regular_color() -> String {
150    "white".to_string()
151}
152
153fn default_hidden_color() -> String {
154    "bright_black".to_string()
155}
156
157fn default_column_spacing() -> usize {
158    2
159}
160
161fn default_max_rows() -> usize {
162    0 // 0 means no limit
163}
164
165fn default_compact_max_rows() -> usize {
166    5
167}
168
169fn default_datetime_format() -> String {
170    "%Y-%m-%d %H:%M:%S".to_string()
171}
172
173fn default_tree_style() -> String {
174    "ascii".to_string()
175}
176
177fn default_long_format_fields() -> Vec<String> {
178    vec![
179        "permissions".to_string(),
180        "nlink".to_string(),
181        "owner".to_string(),
182        "group".to_string(),
183        "size".to_string(),
184        "modified".to_string(),
185        "icon".to_string(),
186        "filename".to_string(),
187    ]
188}
189
190fn default_long_format_title_color() -> String {
191    "bright_black".to_string()
192}
193
194fn default_directory_icon() -> String {
195    "".to_string()
196}
197
198fn default_executable_icon() -> String {
199    "".to_string()
200}
201
202fn default_regular_icon() -> String {
203    "".to_string()
204}
205
206fn default_directory_icon_color() -> String {
207    "blue".to_string()
208}
209
210fn default_executable_icon_color() -> String {
211    "red".to_string()
212}
213
214fn default_regular_icon_color() -> String {
215    "white".to_string()
216}
217
218fn default_hidden_icon_color() -> String {
219    "bright_black".to_string()
220}
221
222impl ColorConfig {
223    pub fn get_directory_color(&self) -> Color {
224        parse_color(&self.directory)
225    }
226
227    pub fn get_executable_color(&self) -> Color {
228        parse_color(&self.executable)
229    }
230
231    pub fn get_regular_color(&self) -> Color {
232        parse_color(&self.regular)
233    }
234
235    pub fn get_hidden_color(&self) -> Color {
236        parse_color(&self.hidden)
237    }
238}
239
240impl IconColorConfig {
241    pub fn get_directory_color(&self) -> Color {
242        parse_color(&self.directory)
243    }
244
245    pub fn get_executable_color(&self) -> Color {
246        parse_color(&self.executable)
247    }
248
249    pub fn get_regular_color(&self) -> Color {
250        parse_color(&self.regular)
251    }
252
253    pub fn get_hidden_color(&self) -> Color {
254        parse_color(&self.hidden)
255    }
256}
257
258impl IconConfig {
259    pub fn get_directory_icon(&self) -> String {
260        self.directory.clone()
261    }
262
263    pub fn get_executable_icon(&self) -> String {
264        self.executable.clone()
265    }
266
267    pub fn get_regular_icon(&self) -> String {
268        self.regular.clone()
269    }
270}
271
272impl DisplayConfig {
273    pub fn get_long_format_title_color(&self) -> Color {
274        parse_color(&self.long_format_title_color)
275    }
276
277    pub fn default_sort(&self) -> SortOptions {
278        SortOptions::new(self.sort_field, self.sort_order)
279    }
280}
281
282fn parse_color(color_str: &str) -> Color {
283    match color_str.to_lowercase().as_str() {
284        "black" => Color::Black,
285        "red" => Color::Red,
286        "green" => Color::Green,
287        "yellow" => Color::Yellow,
288        "blue" => Color::Blue,
289        "magenta" => Color::Magenta,
290        "cyan" => Color::Cyan,
291        "white" => Color::White,
292        "bright_black" => Color::BrightBlack,
293        "bright_red" => Color::BrightRed,
294        "bright_green" => Color::BrightGreen,
295        "bright_yellow" => Color::BrightYellow,
296        "bright_blue" => Color::BrightBlue,
297        "bright_magenta" => Color::BrightMagenta,
298        "bright_cyan" => Color::BrightCyan,
299        "bright_white" => Color::BrightWhite,
300        _ => Color::White, // Default fallback
301    }
302}
303
304pub fn load_config() -> Config {
305    let config_path = get_config_path();
306
307    if !config_path.exists() {
308        return Config::default();
309    }
310
311    match fs::read_to_string(&config_path) {
312        Ok(contents) => match toml::from_str(&contents) {
313            Ok(config) => config,
314            Err(e) => {
315                eprintln!("Warning: Failed to parse config file: {}", e);
316                Config::default()
317            }
318        },
319        Err(_) => Config::default(),
320    }
321}
322
323fn get_config_path() -> PathBuf {
324    let mut path = dirs::home_dir().unwrap_or_else(|| PathBuf::from("."));
325    path.push(".config");
326    path.push("lx");
327    path.push("config");
328    path
329}