Skip to content

Commit

Permalink
support tilde for home dir in fs path
Browse files Browse the repository at this point in the history
  • Loading branch information
emanueldima committed Mar 13, 2024
1 parent 865daff commit f6378e8
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 4 deletions.
70 changes: 70 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ crate-type = ["lib", "cdylib"]
[dependencies]
anyhow = { version = "1", features = ["backtrace"] }
clap = { version = "4.4", features = ["suggestions", "derive"] }
dirs = "5.0"
indexmap = "2.1"
linkme = "0.3"
nom = "7.1"
Expand Down
2 changes: 2 additions & 0 deletions hial.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include <stdint.h>

#define DISPLAY_BYTES_VALUE_LEN 72

extern Language tree_sitter_javascript(void);

extern Language tree_sitter_rust(void);
Expand Down
17 changes: 14 additions & 3 deletions src/interpretations/fs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
borrow::Borrow,
borrow::{Borrow, Cow},
cell::OnceCell,
cmp::Ordering,
ffi::OsString,
Expand Down Expand Up @@ -246,16 +246,27 @@ impl Cell {
pub(crate) fn from_cell(cell: Xell, _: &str, params: &ElevateParams) -> Res<Xell> {
let r = cell.read();
let path = r.as_file_path()?;
let file_cell = Self::make_file_cell(path)?;
let path = Self::shell_tilde(path);
let file_cell = Self::make_file_cell(path.as_ref())?;
Ok(Xell::new_from(DynCell::from(file_cell), Some(cell)))
}

pub(crate) fn from_str_path(path: impl Borrow<str>) -> Res<Xell> {
let path = Path::new(path.borrow());
let file_cell = Self::make_file_cell(path)?;
let path = Self::shell_tilde(path);
let file_cell = Self::make_file_cell(path.as_ref())?;
Ok(Xell::new_from(DynCell::from(file_cell), None))
}

fn shell_tilde(path: &Path) -> Cow<Path> {
if path.starts_with("~") {
let home = dirs::home_dir().unwrap_or_default();
home.join(path.strip_prefix("~").unwrap_or(path)).into()
} else {
path.to_path_buf().into()
}
}

fn make_file_cell(path: &Path) -> Res<Cell> {
let indexmap = [read_file(path)]
.into_iter()
Expand Down
2 changes: 1 addition & 1 deletion src/search/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn path_start_file(input: &str) -> NomRes<&str, PathStart> {
context(
"path_start_file",
tuple((
alt((tag("/"), tag("."))),
alt((tag("/"), tag("."), tag("~"))),
many0(terminated(path_code_points, tag("/"))),
opt(path_code_points),
)),
Expand Down
8 changes: 8 additions & 0 deletions src/tests/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ fn test_files() -> Res<()> {
Ok(())
}

#[test]
fn test_file_path_tilde() -> Res<()> {
crate::utils::log::set_verbose(true);
let home = Xell::from("~").be("path").be("fs");
assert!(home.sub().len()? > 0);
Ok(())
}

#[test]
fn test_fs() -> Res<()> {
crate::utils::log::set_verbose(true);
Expand Down

0 comments on commit f6378e8

Please sign in to comment.