Fix config path on macOS

Also, add `-c` / `--config` CLI option to specify custom config file.

Priority is:

`-c <PATH>` > `~/.config/xplr/init.lua` > `/etc/xplr/init.lua`.

Fixes: https://github.com/sayanarijit/xplr/issues/230
pull/238/head
Arijit Basu 3 years ago committed by Arijit Basu
parent 1513c325d6
commit f12e1e5290

2
Cargo.lock generated

@ -1115,7 +1115,7 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "xplr"
version = "0.13.7"
version = "0.14.0"
dependencies = [
"ansi-to-tui",
"anyhow",

@ -1,6 +1,6 @@
[package]
name = "xplr"
version = "0.13.7" # Update lua.rs
version = "0.14.0" # Update lua.rs
authors = ["Arijit Basu <sayanarijit@gmail.com>"]
edition = "2018"
description = "A hackable, minimal, fast TUI file explorer"

@ -18,7 +18,7 @@ fn navigation_benchmark(c: &mut Criterion) {
});
let lua = mlua::Lua::new();
let mut app = app::App::create(PWD.into(), &lua).expect("failed to create app");
let mut app = app::App::create(PWD.into(), &lua, None).expect("failed to create app");
app = app
.clone()
@ -96,7 +96,7 @@ fn draw_benchmark(c: &mut Criterion) {
});
let lua = mlua::Lua::new();
let mut app = app::App::create(PWD.into(), &lua).expect("failed to create app");
let mut app = app::App::create(PWD.into(), &lua, None).expect("failed to create app");
app = app
.clone()

@ -1521,17 +1521,19 @@ pub struct App {
}
impl App {
pub fn create(pwd: PathBuf, lua: &mlua::Lua) -> Result<Self> {
pub fn create(pwd: PathBuf, lua: &mlua::Lua, config_file: Option<PathBuf>) -> Result<Self> {
let config = lua::init(lua)?;
let config_dir = dirs::config_dir()
.unwrap_or_else(|| PathBuf::from("."))
.join("xplr");
let lua_script_file = config_dir.join("init.lua");
let config_file = if let Some(path) = config_file {
path
} else if let Some(dir) = dirs::home_dir() {
dir.join(".config").join("xplr").join("init.lua")
} else {
PathBuf::from("/").join("etc").join("xplr").join("init.lua")
};
let (config, load_err) = if lua_script_file.exists() {
match lua::extend(lua, &lua_script_file.to_string_lossy().to_string()) {
let (config, load_err) = if config_file.exists() {
match lua::extend(lua, &config_file.to_string_lossy().to_string()) {
Ok(c) => (c, None),
Err(e) => (config, Some(e.to_string())),
}

@ -11,7 +11,8 @@ use xplr::app;
struct Cli {
version: bool,
help: bool,
path: Option<String>,
path: Option<PathBuf>,
config: Option<PathBuf>,
on_load: Vec<app::ExternalMsg>,
}
@ -41,12 +42,14 @@ impl Cli {
"--" => {
if cli.path.is_none() {
cli.path = args.pop_front();
cli.path = args.pop_front().map(PathBuf::from);
}
return Ok(cli);
}
// Options
"-c" | "--config" => cli.config = args.pop_front().map(PathBuf::from),
"--on-load" => {
while let Some(msg) = args.pop_front() {
if msg.starts_with('-') {
@ -81,13 +84,15 @@ fn main() {
xplr [FLAG]... [OPTION]... [PATH]"###;
let flags = r###"
- Read PATH from stdin
- Read path from stdin
-- End of flags and options
-h, --help Prints help information
-V, --version Prints version information"###;
let options = r###"
--on-load <MESSAGE>... Send messages when xplr loads"###;
-c, --config <PATH> Specifies a custom config file (default is
"$HOME/.config/xplr/init.lua")
--on-load <MESSAGE>... Sends messages when xplr loads"###;
let args = r###"
<PATH> Path to focus on, or enter if directory"###;
@ -108,8 +113,9 @@ fn main() {
} else if cli.version {
println!("xplr {}", xplr::app::VERSION);
} else {
match app::runner(cli.path.as_ref().map(PathBuf::from))
.map(|a| a.with_on_load(cli.on_load))
match app::runner(cli.path.clone())
.map(|a| a.with_on_load(cli.on_load.clone()))
.map(|a| a.with_config(cli.config))
.and_then(|a| a.run())
{
Ok(Some(out)) => print!("{}", out),

@ -131,18 +131,24 @@ mod test {
#[test]
fn test_compatibility() {
assert!(check_version(VERSION, "foo path").is_ok());
assert!(check_version("0.13.0", "foo path").is_ok());
assert!(check_version("0.13.1", "foo path").is_ok());
assert!(check_version("0.13.2", "foo path").is_ok());
assert!(check_version("0.13.3", "foo path").is_ok());
assert!(check_version("0.13.4", "foo path").is_ok());
assert!(check_version("0.13.5", "foo path").is_ok());
assert!(check_version("0.13.6", "foo path").is_ok());
assert!(check_version("0.13.7", "foo path").is_ok());
assert!(check_version("0.13.8", "foo path").is_err());
assert!(check_version("0.14.7", "foo path").is_err());
assert!(check_version("0.11.7", "foo path").is_err());
assert!(check_version("1.13.7", "foo path").is_err());
// Current release if OK
assert!(check_version("0.14.0", "foo path").is_ok());
// Prev major release is ERR
// Prev minor release is ERR (Change when we get to v1)
assert!(check_version("0.13.0", "foo path").is_err());
// Prev bugfix release is OK
// Next major release is ERR
assert!(check_version("1.14.0", "foo path").is_err());
// Next minor release is ERR
assert!(check_version("0.15.0", "foo path").is_err());
// Next bugfix release is ERR (Change when we get to v1)
assert!(check_version("0.14.1", "foo path").is_err());
}
}

@ -90,6 +90,7 @@ fn call(app: &app::App, cmd: app::Command, silent: bool) -> io::Result<ExitStatu
pub struct Runner {
pwd: PathBuf,
focused_path: Option<PathBuf>,
config: Option<PathBuf>,
on_load: Vec<app::ExternalMsg>,
}
@ -106,6 +107,7 @@ impl Runner {
Ok(Self {
pwd,
focused_path,
config: None,
on_load: Default::default(),
})
}
@ -115,9 +117,14 @@ impl Runner {
self
}
pub fn with_config(mut self, config: Option<PathBuf>) -> Self {
self.config = config;
self
}
pub fn run(self) -> Result<Option<String>> {
let lua = mlua::Lua::new();
let mut app = app::App::create(self.pwd, &lua)?;
let mut app = app::App::create(self.pwd, &lua, self.config)?;
fs::create_dir_all(app.session_path())?;

Loading…
Cancel
Save