diff options
| -rw-r--r-- | README.md | 2 | ||||
| -rw-r--r-- | example-config.toml | 2 | ||||
| -rw-r--r-- | src/app.rs | 55 | ||||
| -rw-r--r-- | src/bibiman.rs | 10 | ||||
| -rw-r--r-- | src/config.rs | 26 | ||||
| -rw-r--r-- | src/main.rs | 2 |
6 files changed, 69 insertions, 28 deletions
@@ -56,6 +56,8 @@ POSITIONAL ARGS: FLAGS: -h, --help Show this help and exit -v, --version Show the version and exit + -c, --config-file Path to config file for current session + Takes precedence over standard config file --light-terminal Enable color mode for light terminal background ``` diff --git a/example-config.toml b/example-config.toml index 989350e..9d110ed 100644 --- a/example-config.toml +++ b/example-config.toml @@ -1,3 +1,5 @@ [general] bibfiles = ["./tests/biblatex-test.bib"] # files and dirs are possible editor = "vim" # arguments are possible: "vim -y" +# pdf_opener = "xdg-open" +# url_opener = "xdg-open" @@ -275,7 +275,7 @@ impl App { if let Some(PopupKind::Help) = self.bibiman.popup_area.popup_kind { self.bibiman.close_popup(); } else if let Some(PopupKind::OpenRes) = self.bibiman.popup_area.popup_kind { - self.bibiman.open_connected_res()?; + self.bibiman.open_connected_res(cfg)?; } else if let Some(PopupKind::AppendToFile) = self.bibiman.popup_area.popup_kind { self.bibiman.append_entry_to_file(args)? @@ -362,16 +362,20 @@ impl App { } } -pub fn open_connected_file(file: &OsStr) -> Result<()> { +pub fn open_connected_file(cfg: &BibiConfig, file: &OsStr) -> Result<()> { // Build command to execute pdf-reader. 'xdg-open' is Linux standard // TODO: make custom opener command possible through config - let cmd = { - 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"), - } + // let cmd = { + // 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"), + // } + // }; + let cmd = match &cfg.general.pdf_opener { + Some(c) => c, + None => &select_opener(), }; // If necessary, replace ~ with /home dir @@ -381,7 +385,7 @@ pub fn open_connected_file(file: &OsStr) -> Result<()> { // 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) + let _ = Command::new(cmd) .arg(file) .stdout(Stdio::null()) .stderr(Stdio::null()) @@ -391,21 +395,25 @@ pub fn open_connected_file(file: &OsStr) -> Result<()> { Ok(()) } -pub fn open_connected_link(link: &str) -> Result<()> { +pub fn open_connected_link(cfg: &BibiConfig, link: &str) -> Result<()> { // Build command to execute pdf-reader. 'xdg-open' is Linux standard // TODO: make custom opener command possible through config - let cmd = { - 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"), - } + // let cmd = { + // 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"), + // } + // }; + let cmd = match &cfg.general.url_opener { + Some(c) => c, + None => &select_opener(), }; // 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) + let _ = Command::new(cmd) .arg(link) .stdout(Stdio::null()) .stderr(Stdio::null()) @@ -437,6 +445,15 @@ fn expand_home(path: &PathBuf) -> PathBuf { } } +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 test { use super::*; diff --git a/src/bibiman.rs b/src/bibiman.rs index 20cdfc6..ecddc4c 100644 --- a/src/bibiman.rs +++ b/src/bibiman.rs @@ -85,7 +85,9 @@ 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(); - main_bibfiles.append(&mut cfg.general.bibfiles); + if cfg.general.bibfiles.is_some() { + main_bibfiles.append(cfg.general.bibfiles.as_mut().unwrap()) + }; let main_bibfiles = cliargs::parse_files(main_bibfiles); let main_biblio = BibiSetup::new(&main_bibfiles); let tag_list = TagList::new(main_biblio.keyword_list.clone()); @@ -542,7 +544,7 @@ impl Bibiman { Ok(()) } - pub fn open_connected_res(&mut self) -> Result<()> { + pub fn open_connected_res(&mut self, cfg: &BibiConfig) -> Result<()> { // Index of selected entry let entry_idx = self.entry_table.entry_table_state.selected().unwrap(); @@ -553,10 +555,10 @@ impl Bibiman { if self.popup_area.popup_list[popup_idx].contains("Weblink") { let object = self.entry_table.entry_table_items[entry_idx].doi_url(); let url = app::prepare_weblink(object); - app::open_connected_link(&url)?; + app::open_connected_link(cfg, &url)?; } else if self.popup_area.popup_list[popup_idx].contains("File") { let object = self.entry_table.entry_table_items[entry_idx].filepath(); - app::open_connected_file(object)?; + app::open_connected_file(cfg, object)?; } else { eprintln!("Unable to find ressource to open"); }; diff --git a/src/config.rs b/src/config.rs index a80cc13..abb610b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -17,33 +17,51 @@ use std::path::PathBuf; +use color_eyre::eyre::Result; use config::{ConfigError, FileFormat}; use serde::Deserialize; 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, } +/// Substruct [general] in config.toml #[derive(Debug, Clone, Deserialize)] pub struct General { - pub bibfiles: Vec<PathBuf>, + pub bibfiles: Option<Vec<PathBuf>>, pub editor: Option<String>, + pub pdf_opener: Option<String>, + pub url_opener: Option<String>, } impl BibiConfig { - pub fn default(args: &CLIArgs) -> Self { + pub fn default() -> Self { Self { general: General { - bibfiles: args.pos_args.clone(), + bibfiles: None, editor: None, + pdf_opener: None, + url_opener: None, }, } } - pub fn new(args: &CLIArgs) -> Result<Self, ConfigError> { + pub fn new(args: &CLIArgs) -> Result<Self> { + // let mut cfg = config::Config::builder(); + // cfg = cfg.add_source( + // config::File::from(args.cfg_path.clone()) + // .format(FileFormat::Toml) + // .required(false), + // ); + // cfg.build()?.try_deserialize() + Ok(Self::parse_cfg_file(args)?) + } + + fn parse_cfg_file(args: &CLIArgs) -> Result<Self, ConfigError> { let mut cfg = config::Config::builder(); cfg = cfg.add_source( config::File::from(args.cfg_path.clone()) diff --git a/src/main.rs b/src/main.rs index 37bead0..d53aaab 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,7 +48,7 @@ async fn main() -> Result<()> { let mut cfg = if parsed_args.cfg_path.is_file() { BibiConfig::new(&parsed_args)? } else { - BibiConfig::default(&parsed_args) + BibiConfig::default() }; init_error_hooks()?; |
