aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock5
-rw-r--r--Cargo.toml2
-rw-r--r--src/app.rs12
-rw-r--r--src/bibiman.rs6
-rw-r--r--src/config.rs111
-rw-r--r--src/main.rs12
6 files changed, 122 insertions, 26 deletions
diff --git a/Cargo.lock b/Cargo.lock
index f8a062e..342fba4 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -146,6 +146,9 @@ name = "bitflags"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
+dependencies = [
+ "serde",
+]
[[package]]
name = "block2"
@@ -263,6 +266,7 @@ dependencies = [
"itoa",
"rustversion",
"ryu",
+ "serde",
"static_assertions",
]
@@ -1366,6 +1370,7 @@ dependencies = [
"itertools",
"lru",
"paste",
+ "serde",
"strum",
"unicode-segmentation",
"unicode-truncate",
diff --git a/Cargo.toml b/Cargo.toml
index 971df49..c2f42a0 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -27,7 +27,7 @@ rand = "0.8"
itertools = "0.13.0"
lexopt = "0.3.0"
nucleo-matcher = "0.3.1"
-ratatui = { version = "0.29.0", features = ["unstable-rendered-line-info"]}
+ratatui = { version = "0.29.0", features = ["unstable-rendered-line-info", "serde"]}
signal-hook = "0.3.17"
tokio = { version = "1.39.3", features = ["full"] }
tokio-util = "0.7.12"
diff --git a/src/app.rs b/src/app.rs
index 55f49de..b3778af 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -364,11 +364,7 @@ impl App {
pub fn open_connected_file(cfg: &BibiConfig, file: &OsStr) -> Result<()> {
// Build command to execute pdf-reader. 'xdg-open' is Linux standard
- let cmd = match &cfg.general.pdf_opener {
- Some(c) => c,
- None => &select_opener(),
- };
-
+ let cmd = cfg.general.as_ref().unwrap().pdf_opener.as_ref().unwrap();
// If necessary, replace ~ with /home dir
let file = PathBuf::from(file);
@@ -388,11 +384,7 @@ pub fn open_connected_file(cfg: &BibiConfig, file: &OsStr) -> Result<()> {
pub fn open_connected_link(cfg: &BibiConfig, link: &str) -> Result<()> {
// Build command to execute pdf-reader. 'xdg-open' is Linux standard
- let cmd = match &cfg.general.url_opener {
- Some(c) => c,
- None => &select_opener(),
- };
-
+ let cmd = cfg.general.as_ref().unwrap().url_opener.as_ref().unwrap();
// Pass filepath as argument, pipe stdout and stderr to /dev/null
// to keep the TUI clean (where is it piped on Windows???)
let _ = Command::new(cmd)
diff --git a/src/bibiman.rs b/src/bibiman.rs
index ecddc4c..b68b0fa 100644
--- a/src/bibiman.rs
+++ b/src/bibiman.rs
@@ -85,8 +85,8 @@ impl Bibiman {
// Constructs a new instance of [`App`].
pub fn new(args: &mut CLIArgs, cfg: &mut BibiConfig) -> Result<Self> {
let mut main_bibfiles: Vec<PathBuf> = args.pos_args.clone();
- if cfg.general.bibfiles.is_some() {
- main_bibfiles.append(cfg.general.bibfiles.as_mut().unwrap())
+ if cfg.general.as_ref().unwrap().bibfiles.is_some() {
+ main_bibfiles.append(cfg.general.as_mut().unwrap().bibfiles.as_mut().unwrap())
};
let main_bibfiles = cliargs::parse_files(main_bibfiles);
let main_biblio = BibiSetup::new(&main_bibfiles);
@@ -381,7 +381,7 @@ impl Bibiman {
tui.exit()?;
// Use VISUAL or EDITOR. Set "vi" as last fallback
let mut cmd: Command = EditorBuilder::new()
- .source(cfg.general.editor.clone())
+ .source(cfg.general.as_ref().unwrap().editor.clone())
.environment()
.source(Some("vi"))
.build()
diff --git a/src/config.rs b/src/config.rs
index abb610b..22873dd 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -19,6 +19,7 @@ use std::path::PathBuf;
use color_eyre::eyre::Result;
use config::{ConfigError, FileFormat};
+use ratatui::style::Color;
use serde::Deserialize;
use crate::cliargs::CLIArgs;
@@ -26,7 +27,8 @@ use crate::cliargs::CLIArgs;
/// Main struct of the config file. Contains substructs/headings in toml
#[derive(Debug, Clone, Deserialize)]
pub struct BibiConfig {
- pub general: General,
+ pub general: Option<General>,
+ pub colors: Option<Colors>,
}
/// Substruct [general] in config.toml
@@ -38,17 +40,103 @@ pub struct General {
pub url_opener: Option<String>,
}
-impl BibiConfig {
- pub fn default() -> Self {
+/// Substruct [colors] in config.toml
+#[derive(Debug, Clone, Deserialize)]
+pub struct Colors {
+ pub main_text_color: Option<Color>,
+ pub highlight_text_color: Option<Color>,
+ pub entry_color: Option<Color>,
+ pub keyword_color: Option<Color>,
+ pub info_color: Option<Color>,
+ pub confirm_color: Option<Color>,
+ pub warn_color: Option<Color>,
+ pub bar_bg_color: Option<Color>,
+ pub popup_bg_color: Option<Color>,
+ pub selected_row_bg_color: Option<Color>,
+}
+
+impl Default for BibiConfig {
+ fn default() -> Self {
Self {
- general: General {
+ general: Some(General {
bibfiles: None,
editor: None,
- pdf_opener: None,
- url_opener: None,
- },
+ pdf_opener: Some(select_opener()),
+ url_opener: Some(select_opener()),
+ }),
+ colors: Some(Colors {
+ main_text_color: Some(Color::Indexed(250)),
+ highlight_text_color: Some(Color::Indexed(254)),
+ entry_color: Some(Color::Indexed(36)),
+ keyword_color: Some(Color::Indexed(101)),
+ info_color: Some(Color::Indexed(99)),
+ confirm_color: Some(Color::Indexed(47)),
+ warn_color: Some(Color::Indexed(124)),
+ bar_bg_color: Some(Color::Indexed(234)),
+ popup_bg_color: Some(Color::Indexed(234)),
+ selected_row_bg_color: Some(Color::Indexed(237)),
+ }),
}
}
+}
+
+impl BibiConfig {
+ pub fn parse_config(&mut self, args: &CLIArgs) -> Result<()> {
+ if args.cfg_path.is_file() {
+ let cfg_file = Self::parse_cfg_file(args)?;
+
+ if let Some(general) = cfg_file.general {
+ if let Some(bibfiles) = general.bibfiles {
+ self.general.as_mut().unwrap().bibfiles = Some(bibfiles)
+ }
+ if let Some(editor) = general.editor {
+ self.general.as_mut().unwrap().editor = Some(editor)
+ }
+ if let Some(pdf_opener) = general.pdf_opener {
+ self.general.as_mut().unwrap().pdf_opener = Some(pdf_opener)
+ }
+ if let Some(url_opener) = general.url_opener {
+ self.general.as_mut().unwrap().url_opener = Some(url_opener)
+ }
+ }
+
+ if let Some(colors) = cfg_file.colors {
+ if let Some(main_text_color) = colors.main_text_color {
+ self.colors.as_mut().unwrap().main_text_color = Some(main_text_color)
+ }
+ if let Some(highlight_text_color) = colors.highlight_text_color {
+ self.colors.as_mut().unwrap().highlight_text_color = Some(highlight_text_color)
+ }
+ if let Some(entry_color) = colors.entry_color {
+ self.colors.as_mut().unwrap().entry_color = Some(entry_color)
+ }
+ if let Some(keyword_color) = colors.keyword_color {
+ self.colors.as_mut().unwrap().keyword_color = Some(keyword_color)
+ }
+ if let Some(info_color) = colors.info_color {
+ self.colors.as_mut().unwrap().info_color = Some(info_color)
+ }
+ if let Some(confirm_color) = colors.confirm_color {
+ self.colors.as_mut().unwrap().confirm_color = Some(confirm_color)
+ }
+ if let Some(warn_color) = colors.warn_color {
+ self.colors.as_mut().unwrap().warn_color = Some(warn_color)
+ }
+ if let Some(bar_bg_color) = colors.bar_bg_color {
+ self.colors.as_mut().unwrap().bar_bg_color = Some(bar_bg_color)
+ }
+ if let Some(popup_bg_color) = colors.popup_bg_color {
+ self.colors.as_mut().unwrap().popup_bg_color = Some(popup_bg_color)
+ }
+ if let Some(selected_row_bg_color) = colors.selected_row_bg_color {
+ self.colors.as_mut().unwrap().selected_row_bg_color =
+ Some(selected_row_bg_color)
+ }
+ }
+ }
+
+ Ok(())
+ }
pub fn new(args: &CLIArgs) -> Result<Self> {
// let mut cfg = config::Config::builder();
@@ -71,3 +159,12 @@ impl BibiConfig {
cfg.build()?.try_deserialize()
}
}
+
+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"),
+ }
+}
diff --git a/src/main.rs b/src/main.rs
index d53aaab..8ec3b77 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -45,11 +45,13 @@ async fn main() -> Result<()> {
std::process::exit(0);
}
- let mut cfg = if parsed_args.cfg_path.is_file() {
- BibiConfig::new(&parsed_args)?
- } else {
- BibiConfig::default()
- };
+ // let mut cfg = if parsed_args.cfg_path.is_file() {
+ // BibiConfig::new(&parsed_args)?
+ // } else {
+ // BibiConfig::default()
+ // };
+ let mut cfg = BibiConfig::default();
+ cfg.parse_config(&parsed_args)?;
init_error_hooks()?;