From a1dea118c0760b0cf3ffb0c7d42cc1f5c20e04a5 Mon Sep 17 00:00:00 2001 From: Denis Isidoro Date: Thu, 12 Mar 2020 13:03:04 -0300 Subject: [PATCH] Fix path issues (#231) Fixes #224 and #228 --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/cheat.rs | 16 ++++++---------- src/cmds/home.rs | 2 +- src/cmds/shell.rs | 6 +++++- src/filesystem.rs | 33 ++++++++++++++++++++++++++++++--- 6 files changed, 44 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7926d25..4372aee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -89,7 +89,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "navi" -version = "2.0.4" +version = "2.0.5" dependencies = [ "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "raw_tty 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index f4fd424..2e076d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "navi" -version = "2.0.4" +version = "2.0.5" authors = ["Denis Isidoro "] edition = "2018" diff --git a/src/cheat.rs b/src/cheat.rs index f670214..5789894 100644 --- a/src/cheat.rs +++ b/src/cheat.rs @@ -134,22 +134,18 @@ fn read_file( pub fn read_all(config: &Config, stdin: &mut std::process::ChildStdin) -> HashMap { let mut variables: HashMap = HashMap::new(); - let current_exe = filesystem::exe_path_string(); - let fallback = format!( - "{path}/cheats:{path}/../cheats:{path}/../../cheats:{path}/../libexec/cheats", - path = current_exe - ); + let fallback = filesystem::pathbuf_to_string(filesystem::cheat_pathbuf().unwrap()); let folders_str = config.path.as_ref().unwrap_or(&fallback); let folders = folders_str.split(':'); for folder in folders { if let Ok(paths) = fs::read_dir(folder) { for path in paths { - read_file( - path.unwrap().path().into_os_string().to_str().unwrap(), - &mut variables, - stdin, - ); + let path_os_str = path.unwrap().path().into_os_string(); + let path_str = path_os_str.to_str().unwrap(); + if path_str.ends_with(".cheat") { + read_file(path_str, &mut variables, stdin); + } } } } diff --git a/src/cmds/home.rs b/src/cmds/home.rs index cbcdfb1..a38d825 100644 --- a/src/cmds/home.rs +++ b/src/cmds/home.rs @@ -3,6 +3,6 @@ use std::error::Error; use crate::filesystem; pub fn main() -> Result<(), Box> { - println!("{}", filesystem::exe_path_string()); + println!("{}", filesystem::exe_parent_string()); Ok(()) } diff --git a/src/cmds/shell.rs b/src/cmds/shell.rs index beaa72b..d562bde 100644 --- a/src/cmds/shell.rs +++ b/src/cmds/shell.rs @@ -9,7 +9,11 @@ pub fn main(shell: &str) -> Result<(), Box> { _ => "navi.plugin.bash", }; - println!("{}/shell/{}", filesystem::exe_path_string(), file); + println!( + "{}/{}", + filesystem::pathbuf_to_string(filesystem::shell_pathbuf()), + file + ); Ok(()) } diff --git a/src/filesystem.rs b/src/filesystem.rs index 19c7d01..ed3ecc6 100644 --- a/src/filesystem.rs +++ b/src/filesystem.rs @@ -1,4 +1,5 @@ use std::fs; +use std::fs::metadata; use std::fs::File; use std::io::{self, BufRead, BufReader, Lines}; use std::path::{Path, PathBuf}; @@ -11,6 +12,10 @@ where Ok(io::BufReader::new(file).lines()) } +pub fn pathbuf_to_string(pathbuf: PathBuf) -> String { + pathbuf.as_os_str().to_str().unwrap().to_string() +} + fn follow_symlink(pathbuf: PathBuf) -> PathBuf { let other = fs::read_link(pathbuf.clone()); match other { @@ -34,11 +39,33 @@ fn exe_pathbuf() -> PathBuf { follow_symlink(pathbuf) } -pub fn exe_string() -> String { - exe_pathbuf().as_os_str().to_str().unwrap().to_string() +pub fn cheat_pathbuf() -> Option { + let exe_parent_str = exe_parent_string(); + + let array = ["cheats", "../libexec/cheats", "../cheats", "../../cheats"]; + for elem in &array { + let p = format!("{}/{}", exe_parent_str, elem); + let meta = metadata(&p); + if let Ok(m) = meta { + if m.is_dir() { + return Some(PathBuf::from(p)); + } + } + } + + None } -pub fn exe_path_string() -> String { +pub fn shell_pathbuf() -> PathBuf { + let cheat_path_str = pathbuf_to_string(cheat_pathbuf().unwrap()); + PathBuf::from(format!("{}/../shell", cheat_path_str)) +} + +pub fn exe_string() -> String { + pathbuf_to_string(exe_pathbuf()) +} + +pub fn exe_parent_string() -> String { exe_pathbuf() .parent() .unwrap()