Fix path issues (#231)

Fixes #224 and #228
This commit is contained in:
Denis Isidoro 2020-03-12 13:03:04 -03:00 committed by GitHub
parent 4dffbe6f29
commit a1dea118c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 44 additions and 17 deletions

2
Cargo.lock generated
View file

@ -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)",

View file

@ -1,6 +1,6 @@
[package]
name = "navi"
version = "2.0.4"
version = "2.0.5"
authors = ["Denis Isidoro <denis_isidoro@live.com>"]
edition = "2018"

View file

@ -134,22 +134,18 @@ fn read_file(
pub fn read_all(config: &Config, stdin: &mut std::process::ChildStdin) -> HashMap<String, Value> {
let mut variables: HashMap<String, Value> = 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);
}
}
}
}

View file

@ -3,6 +3,6 @@ use std::error::Error;
use crate::filesystem;
pub fn main() -> Result<(), Box<dyn Error>> {
println!("{}", filesystem::exe_path_string());
println!("{}", filesystem::exe_parent_string());
Ok(())
}

View file

@ -9,7 +9,11 @@ pub fn main(shell: &str) -> Result<(), Box<dyn Error>> {
_ => "navi.plugin.bash",
};
println!("{}/shell/{}", filesystem::exe_path_string(), file);
println!(
"{}/{}",
filesystem::pathbuf_to_string(filesystem::shell_pathbuf()),
file
);
Ok(())
}

View file

@ -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<PathBuf> {
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()