Skip to content

Commit

Permalink
Fix path issues (#231)
Browse files Browse the repository at this point in the history
Fixes #224 and #228
  • Loading branch information
denisidoro authored Mar 12, 2020
1 parent 4dffbe6 commit a1dea11
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "navi"
version = "2.0.4"
version = "2.0.5"
authors = ["Denis Isidoro <[email protected]>"]
edition = "2018"

Expand Down
16 changes: 6 additions & 10 deletions src/cheat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cmds/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
6 changes: 5 additions & 1 deletion src/cmds/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
31 changes: 29 additions & 2 deletions src/filesystem.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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 {
Expand All @@ -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()
Expand Down

0 comments on commit a1dea11

Please sign in to comment.