From c066201777d86fcaded434585747b70efc4b475d Mon Sep 17 00:00:00 2001 From: Arijit Basu Date: Thu, 1 Apr 2021 15:28:32 +0530 Subject: [PATCH] Read config from file --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/app.rs | 5 ++--- src/main.rs | 16 +++++++++++++++- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9c9194a..44ada0b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1166,7 +1166,7 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "xplr" -version = "0.2.0" +version = "0.2.1" dependencies = [ "criterion", "crossterm", diff --git a/Cargo.toml b/Cargo.toml index f43a8e2..715606b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "xplr" -version = "0.2.0" # Update app.rs +version = "0.2.1" # Update app.rs authors = ["Arijit Basu "] edition = "2018" description = "An experimental, minimal, configurable TUI file explorer, stealing ideas from nnn and fzf." diff --git a/src/app.rs b/src/app.rs index 86df075..9c6d77c 100644 --- a/src/app.rs +++ b/src/app.rs @@ -10,7 +10,7 @@ use std::collections::HashMap; use std::collections::VecDeque; use std::path::PathBuf; -pub const VERSION: &str = "v0.2.0"; // Update Cargo.toml +pub const VERSION: &str = "v0.2.1"; // Update Cargo.toml pub const TEMPLATE_TABLE_ROW: &str = "TEMPLATE_TABLE_ROW"; @@ -216,8 +216,7 @@ pub struct App { } impl App { - pub fn new(pwd: String) -> Self { - let config = Config::default(); + pub fn new(config: Config, pwd: String) -> Self { let mode = config .modes .get(&"default".to_string()) diff --git a/src/main.rs b/src/main.rs index 56e0ef2..3ad7c2b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ use handlebars::{handlebars_helper, Handlebars}; use shellwords; use std::env; use std::fs; +use std::io; use std::io::prelude::*; use std::path::PathBuf; use std::sync::mpsc; @@ -14,6 +15,7 @@ use termion::get_tty; use tui::backend::CrosstermBackend; use tui::Terminal; use xplr::app; +use xplr::config::Config; use xplr::error::Error; use xplr::explorer; use xplr::input::Key; @@ -38,7 +40,19 @@ fn main() -> Result<(), Error> { let mut last_pwd = pwd.clone(); - let mut app = app::App::new(pwd.clone()); + let config_dir = dirs::config_dir() + .unwrap_or(PathBuf::from(".")) + .join("xplr"); + + let config_file = config_dir.join("config.yml"); + + let config: Config = if config_file.exists() { + serde_yaml::from_reader(io::BufReader::new(&fs::File::open(&config_file)?))? + } else { + Config::default() + }; + + let mut app = app::App::new(config, pwd.clone()); let mut hb = Handlebars::new(); hb.register_helper("shellescape", Box::new(shellescape));