summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDenis Isidoro <denisidoro@users.noreply.github.com>2020-03-12 13:03:04 -0300
committerGitHub <noreply@github.com>2020-03-12 13:03:04 -0300
commita1dea118c0760b0cf3ffb0c7d42cc1f5c20e04a5 (patch)
tree16cac61edf4c6106afff2be465ec3a53486d0242
parent4dffbe6f2987124aa1c148486a785f66169d905c (diff)
Fix path issues (#231)v2.0.5
Fixes #224 and #228
-rw-r--r--Cargo.lock2
-rw-r--r--Cargo.toml2
-rw-r--r--src/cheat.rs16
-rw-r--r--src/cmds/home.rs2
-rw-r--r--src/cmds/shell.rs6
-rw-r--r--src/filesystem.rs31
6 files changed, 43 insertions, 16 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 <denis_isidoro@live.com>"]
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<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);
+ }
}
}
}
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<dyn Error>> {
- 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<dyn Error>> {
_ => "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 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 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 {
- exe_pathbuf().as_os_str().to_str().unwrap().to_string()
+ pathbuf_to_string(exe_pathbuf())
}
-pub fn exe_path_string() -> String {
+pub fn exe_parent_string() -> String {
exe_pathbuf()
.parent()
.unwrap()