aboutsummaryrefslogtreecommitdiff
path: root/src/config.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs115
1 files changed, 111 insertions, 4 deletions
diff --git a/src/config.rs b/src/config.rs
index f1ac3ca..cd55b5c 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -15,9 +15,13 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
/////
-use std::path::PathBuf;
+use std::{
+ fs::File,
+ io::{stdin, Write},
+ path::PathBuf,
+};
-use color_eyre::eyre::Result;
+use color_eyre::{eyre::Result, owo_colors::OwoColorize};
use figment::{
providers::{Format, Serialized, Toml},
Figment,
@@ -27,6 +31,43 @@ 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"
+
+# [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 {
@@ -87,9 +128,9 @@ impl Default for BibiConfig {
impl BibiConfig {
pub fn parse_config(args: &CLIArgs) -> Result<BibiConfig> {
- let cfg_file: BibiConfig = if args.cfg_path.is_file() {
+ let cfg_file: BibiConfig = if args.cfg_path.as_ref().unwrap().is_file() {
Figment::from(Serialized::defaults(BibiConfig::default()))
- .merge(Toml::file(&args.cfg_path))
+ .merge(Toml::file(&args.cfg_path.as_ref().unwrap()))
.extract()?
} else {
BibiConfig::default()
@@ -110,6 +151,72 @@ impl BibiConfig {
self.colors.confirm_color = Color::Indexed(22);
self.colors.selected_row_bg_color = Color::Indexed(107);
}
+
+ /// 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;
+ }
+ }
+ }
+
+ 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 {