diff options
| author | lukeflo | 2025-02-24 15:29:47 +0000 |
|---|---|---|
| committer | lukeflo | 2025-02-24 15:29:47 +0000 |
| commit | 5da77a2f812a0bb6e0057f7b2e2c642142fca125 (patch) | |
| tree | 6be886693445a54e4cbd7b98151555eec220e863 /src/config.rs | |
| parent | dd8dd9611771491e723a49b41cf27b1e9090664d (diff) | |
| parent | aff7c398da005029a293178e487cf5323e507fb4 (diff) | |
| download | bibiman-5da77a2f812a0bb6e0057f7b2e2c642142fca125.tar.gz bibiman-5da77a2f812a0bb6e0057f7b2e2c642142fca125.zip | |
Merge pull request 'Implement config file' (#15) from implement-config into main
Reviewed-on: https://codeberg.org/lukeflo/bibiman/pulls/15
Diffstat (limited to 'src/config.rs')
| -rw-r--r-- | src/config.rs | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..956b724 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,166 @@ +// 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::path::PathBuf; + +use color_eyre::eyre::Result; +use figment::{ + providers::{Format, Serialized, Toml}, + Figment, +}; +use ratatui::style::Color; +use serde::{Deserialize, Serialize}; + +use crate::cliargs::CLIArgs; + +/// 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, +} + +/// 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, +} + +impl Default for BibiConfig { + fn default() -> Self { + Self { + general: General { + bibfiles: None, + editor: None, + pdf_opener: select_opener(), + url_opener: select_opener(), + }, + 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), + }, + } + } +} + +impl BibiConfig { + pub fn parse_config(args: &CLIArgs) -> Result<BibiConfig> { + let cfg_file: BibiConfig = if args.cfg_path.is_file() { + Figment::from(Serialized::defaults(BibiConfig::default())) + .merge(Toml::file(&args.cfg_path)) + .extract()? + } else { + BibiConfig::default() + }; + + Ok(cfg_file) + } + + /// Activates the default color scheme for light background terminals + pub fn light_colors(&mut self) { + self.colors.main_text_color = Color::Indexed(235); + self.colors.highlight_text_color = Color::Indexed(232); + self.colors.entry_color = Color::Indexed(23); + self.colors.keyword_color = Color::Indexed(58); + self.colors.info_color = Color::Indexed(57); + self.colors.bar_bg_color = Color::Indexed(144); + self.colors.popup_bg_color = Color::Indexed(187); + self.colors.confirm_color = Color::Indexed(22); + self.colors.selected_row_bg_color = Color::Indexed(107); + } +} + +fn select_opener() -> String { + match std::env::consts::OS { + "linux" => String::from("xdg-open"), + "macos" => String::from("open"), + "windows" => String::from("start"), + _ => 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(()) + }); + } +} |
