aboutsummaryrefslogtreecommitdiff
path: root/src/config.rs
blob: c30d8d12f93082fd80619b0b7d699ae6f220cbba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// bibiman - a TUI for managing BibLaTeX databases
// Copyright (C) 2024  lukeflo
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
/////

use std::{
    fs::{create_dir_all, File},
    io::{stdin, Write},
    path::PathBuf,
    str::FromStr,
};

use color_eyre::{eyre::Result, owo_colors::OwoColorize};
use figment::{
    providers::{Format, Serialized, Toml},
    Figment,
};
use ratatui::style::Color;
use serde::{Deserialize, Serialize};

use crate::cliargs::CLIArgs;

const DEFAULT_CONFIG: &str = r##"
# [general]
## Default files/dirs which are loaded on startup
## Use absolute paths (~ for HOME works). Otherwise, loading might not work.
# bibfiles = [ "/path/to/bibfile", "path/to/dir/with/bibfiles" ]

## Default editor to use when editing files. Arguments are possible
# editor = "vim" # with args: "vim -y"

## Default app to open PDFs/Epubs
# pdf_opener = "xdg-open"

## Default app to open URLs/DOIs
# url_opener = "xdg-open"

## Prefix which is prepended to the filepath from the `file` field
## Use absolute paths (~ for HOME works). Otherwise, loading might not work.
# file_prefix = "/some/path/prefix"

## Path to folder (with subfolders) containing PDF files with the basename
## of the format "citekey.pdf". Other PDF basenames are not accepted.
## Use absolute paths (~ for HOME works). Otherwise, loading might not work.
# pdf_path = "/path/to/pdf/folder"

## Path to folder (with subfolders) containing note files with the basename of
## the format "citekey.extension". Other basenames are not accepted. The possible
## extensions can be set through the "note_extensions" array.
# note_path = "/path/to/notes/folder"
# note_extensions = [ "md", "txt", "org" ]

# [colors]
## Default values for dark-themed terminal
## Possible values are:
## ANSI color names. E.g. "bright-black"
## 256-colors indices. E.g. "250"
## Hex color codes. E.g. "#3a3a3a"
# main_text_color = "250"
# highlight_text_color = "254"
# entry_color = "36"
# keyword_color = "101"
# info_color = "99"
# confirm_color = "47"
# warn_color = "124"
# bar_bg_color = "234"
# popup_bg_color = "234"
# selected_row_bg_color = "237"
"##;

/// Main struct of the config file. Contains substructs/headings in toml
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct BibiConfig {
    pub general: General,
    pub colors: Colors,
}

/// Substruct [general] in config.toml
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub struct General {
    pub bibfiles: Option<Vec<PathBuf>>,
    pub editor: Option<String>,
    pub pdf_opener: String,
    pub url_opener: String,
    pub file_prefix: Option<PathBuf>,
    pub pdf_path: Option<PathBuf>,
    pub note_path: Option<PathBuf>,
    pub note_extensions: Option<Vec<String>>,
}

/// Substruct [colors] in config.toml
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub struct Colors {
    pub main_text_color: Color,
    pub highlight_text_color: Color,
    pub entry_color: Color,
    pub keyword_color: Color,
    pub info_color: Color,
    pub confirm_color: Color,
    pub warn_color: Color,
    pub bar_bg_color: Color,
    pub popup_bg_color: Color,
    pub selected_row_bg_color: Color,
    pub note_color: Color,
    pub file_color: Color,
    pub link_color: Color,
    pub author_color: Color,
    pub title_color: Color,
    pub year_color: Color,
}

impl Default for BibiConfig {
    fn default() -> Self {
        Self {
            general: General {
                bibfiles: None,
                editor: None,
                pdf_opener: select_opener(),
                url_opener: select_opener(),
                file_prefix: None,
                pdf_path: None,
                note_path: None,
                note_extensions: None,
            },
            colors: Self::dark_colors(),
        }
    }
}

impl BibiConfig {
    pub fn new(args: &CLIArgs) -> Self {
        Self {
            general: General {
                bibfiles: None,
                editor: None,
                pdf_opener: select_opener(),
                url_opener: select_opener(),
                file_prefix: None,
                pdf_path: None,
                note_path: None,
                note_extensions: None,
            },
            colors: if args.light_theme {
                Self::light_colors()
            } else {
                Self::dark_colors()
            },
        }
    }

    pub fn parse_config(args: &CLIArgs) -> Result<BibiConfig> {
        let cfg_file: BibiConfig = if args.cfg_path.as_ref().unwrap().is_file() {
            Figment::from(Serialized::defaults(BibiConfig::new(args)))
                .merge(Toml::file(&args.cfg_path.as_ref().unwrap()))
                .extract()?
        } else {
            BibiConfig::new(args)
        };

        Ok(cfg_file)
    }

    /// overwright config values with values set explicitly through the
    /// command line interface
    pub fn cli_overwrite(&mut self, args: &CLIArgs) {
        if args.pdf_path.is_some() {
            self.general.pdf_path = args.pdf_path.clone();
        }
    }

    /// Standard color scheme for terminals with dark background (default)
    pub fn dark_colors() -> Colors {
        Colors {
            main_text_color: Color::Indexed(250),
            highlight_text_color: Color::Indexed(254),
            entry_color: Color::Indexed(36),
            keyword_color: Color::Indexed(101),
            info_color: Color::Indexed(99),
            confirm_color: Color::Indexed(47),
            warn_color: Color::Indexed(124),
            bar_bg_color: Color::Indexed(235),
            popup_bg_color: Color::Indexed(234),
            selected_row_bg_color: Color::Indexed(237),
            note_color: Color::Indexed(123),
            file_color: Color::Indexed(209),
            link_color: Color::Indexed(33),
            author_color: Color::Indexed(38),
            title_color: Color::Indexed(37),
            year_color: Color::Indexed(135),
        }
    }

    /// Activates the default color scheme for light background terminals
    pub fn light_colors() -> Colors {
        Colors {
            main_text_color: Color::Indexed(235),
            highlight_text_color: Color::Indexed(232),
            entry_color: Color::Indexed(23),
            keyword_color: Color::Indexed(58),
            info_color: Color::Indexed(57),
            bar_bg_color: Color::Indexed(144),
            popup_bg_color: Color::Indexed(187),
            confirm_color: Color::Indexed(22),
            warn_color: Color::Indexed(124),
            selected_row_bg_color: Color::Indexed(107),
            note_color: Color::Indexed(123),
            file_color: Color::Indexed(209),
            link_color: Color::Indexed(27),
            author_color: Color::Indexed(38),
            title_color: Color::Indexed(37),
            year_color: Color::Indexed(135),
        }
    }

    /// Function which offers the user to create a default config
    /// if no exists at the standard config path.
    pub fn create_default_config(args: &CLIArgs) {
        let path = args.cfg_path.as_ref().unwrap().to_str();
        let mut input_str = String::new();

        match path {
            Some(p) => {
                println!("It seems no config file {} exists.", p.bold());
            }
            None => {
                println!(
                    "Can't parse config file path. Running {} without any config file.",
                    "bibiman".bold()
                );
                return;
            }
        }

        loop {
            println!(
                "\nDo you want to create a default config? {}",
                "[Y|N]".bold()
            );

            stdin()
                .read_line(&mut input_str)
                .expect("Couldn't read input");

            match input_str.trim().to_lowercase().as_str() {
                "y" | "yes" => {
                    break;
                }
                "n" | "no" => {
                    println!("\nNo config file will be created.");
                    return;
                }
                v => {
                    println!("\nInvalid value {}.", v.red());
                    println!("Please type {} or {}.", "[Y]es".bold(), "[N]o".bold());
                    input_str.clear();
                    continue;
                }
            }
        }

        {
            // Ignore any errors of this function, if something goes wrong creating a file will fail too.
            let mut dirpath = PathBuf::from_str(path.unwrap()).unwrap_or_else(|_| PathBuf::new());
            dirpath.pop();
            let _ = create_dir_all(dirpath);
        }

        let cfg_file = File::create_new(path.unwrap());

        match cfg_file {
            Ok(mut file) => {
                file.write_all(DEFAULT_CONFIG.as_bytes()).unwrap();
                println!("\nCreated default config file {}", path.unwrap().bold());
                println!(
                    "Check {} for explanations how to configure it.",
                    "https://codeberg.org/lukeflo/bibiman#configuration".bright_yellow()
                )
            }
            Err(e) => {
                println!(
                    "\nCouldn't create default config due to the following error:\n{}",
                    e.red()
                )
            }
        }
    }
}

fn select_opener() -> String {
    match std::env::consts::OS {
        "linux" => String::from("xdg-open"),
        "macos" | "apple" => String::from("open"),
        "windows" => String::from("start"),
        "freebsd" => String::from("xdg-open"),
        "openbsd" | "netbsd" | "solaris" | "redox" => String::from("open"),
        _ => panic!("Couldn't detect OS for setting correct opener"),
    }
}

#[cfg(test)]
mod tests {
    use figment::{
        providers::{Format, Toml},
        Figment,
    };

    use super::BibiConfig;

    #[test]
    fn parse_default_config() {
        figment::Jail::expect_with(|jail| {
            jail.create_file(
                "bibiman.toml",
                r#"
                    [general]
                    pdf_opener = "xdg-open"
                    url_opener = "xdg-open"

                    [colors]
                    main_text_color = "250"
                    highlight_text_color = "254"
                    entry_color = "36"
                    keyword_color = "101"
                    info_color = "99"
                    confirm_color = "47"
                    warn_color = "124"
                    bar_bg_color = "235"
                    popup_bg_color = "234"
                    selected_row_bg_color = "237"
                "#,
            )?;

            let config: BibiConfig = Figment::new().merge(Toml::file("bibiman.toml")).extract()?;

            let default_config: BibiConfig = BibiConfig::default();

            assert_eq!(config, default_config);
            assert_eq!(config.general.bibfiles, None);
            assert_eq!(config.general.editor, None);

            Ok(())
        });
    }
}