Skip to content

Commit

Permalink
support tilde when changing directory
Browse files Browse the repository at this point in the history
  • Loading branch information
nbittich committed Jun 11, 2023
1 parent f9a1720 commit 05652b9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "adana"
version = "0.13.27"
version = "0.13.28"
edition = "2021"
authors = ["Nordine Bittich"]
license = "MIT"
Expand All @@ -26,7 +26,7 @@ nom = { version = "7.1.3" }
nu-ansi-term = "0.48.0"
rustyline = "11.0.0"
rustyline-derive = "0.8.0"
serde = { version = "1.0.163", features = ['serde_derive', 'rc'] }
serde = { version = "1.0.164", features = ['serde_derive', 'rc'] }
serde_json = "1.0.96"
slab_tree = "0.3.2"
strum = { version = "0.24.1", features = ["derive"] }
Expand Down
2 changes: 1 addition & 1 deletion src/cache_command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub enum CacheCommand<'a> {
Del(&'a str),
Get(&'a str),
Exec { key: &'a str, args: Option<&'a str> },
Cd(Option<&'a str>),
Cd { has_tilde: bool, path: Option<&'a str> },
Using(&'a str),
Dump(Option<&'a str>),
Clear,
Expand Down
22 changes: 18 additions & 4 deletions src/cache_command/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,26 @@ fn cd_command(command: &str) -> Res<CacheCommand> {
map(
preceded(
tag_no_case(CD),
opt(preceded(
preceded(
multispace1,
verify(rest.map(|r: &str| r.trim()), |s: &str| !s.is_empty()),
)),
pair(
map(
opt(preceded(
multispace0,
terminated(tag("~"), opt(tag("/"))),
)),
|h| h.is_some(),
),
opt(preceded(
multispace0,
verify(rest.map(|r: &str| r.trim()), |s: &str| {
!s.is_empty()
}),
)),
),
),
),
CacheCommand::Cd,
|(has_tilde, path)| CacheCommand::Cd { has_tilde, path },
)(command)
}

Expand Down
14 changes: 11 additions & 3 deletions src/cache_command/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,17 @@ pub fn process_command(
);
}
}
CacheCommand::Cd(path) => {
let path_buf =
path.map(PathBuf::from).or_else(dirs::home_dir).context(
CacheCommand::Cd { path, has_tilde } => {
let path_buf = path
.and_then(|p| {
if has_tilde {
dirs::home_dir().map(|hd| hd.join(p))
} else {
Some(PathBuf::from(p))
}
})
.or_else(dirs::home_dir)
.context(
"could not change directory. path {path:?} not found!",
)?;
if path_buf.exists() {
Expand Down

0 comments on commit 05652b9

Please sign in to comment.