Skip to content

Commit

Permalink
support change to previous directory
Browse files Browse the repository at this point in the history
  • Loading branch information
nbittich committed Jun 11, 2023
1 parent 22ca265 commit 7ea334e
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 40 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 = "adana"
version = "0.13.29"
version = "0.13.30"
edition = "2021"
authors = ["Nordine Bittich"]
license = "MIT"
Expand Down
8 changes: 7 additions & 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 { has_tilde: bool, path: Option<&'a str> },
Cd(ChangeDirectoryType<'a>),
Using(&'a str),
Dump(Option<&'a str>),
Clear,
Expand All @@ -67,6 +67,12 @@ pub enum CacheCommand<'a> {
PrintAst(&'a str),
Help,
}
#[derive(Debug)]
pub enum ChangeDirectoryType<'a> {
HomeDirectory(Option<&'a str>),
Path(&'a str),
Previous,
}

impl CacheCommand<'_> {
pub const fn doc() -> &'static [(&'static [&'static str], &'static str)] {
Expand Down
63 changes: 40 additions & 23 deletions src/cache_command/parser.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::prelude::*;

use super::{constants::*, CacheCommand};
use super::{constants::*, CacheCommand, ChangeDirectoryType};

fn add_command(command: &str) -> Res<CacheCommand> {
map(
Expand Down Expand Up @@ -131,32 +131,49 @@ fn cd_command(command: &str) -> Res<CacheCommand> {
map(
preceded(
tag_no_case(CD),
opt(preceded(
multispace1,
pair(
map(
opt(preceded(
multispace0,
terminated(tag("~"), opt(tag("/"))),
)),
|h| h.is_some(),
alt((
map(
verify(rest.map(|r: &str| r.trim()), |s: &str| {
s.is_empty()
}),
|_| ChangeDirectoryType::HomeDirectory(None),
),
map(preceded(multispace1, tag("-")), |_| {
ChangeDirectoryType::Previous
}),
map(
preceded(
multispace1,
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(),
),
)),
),
),
opt(preceded(
multispace0,
verify(rest.map(|r: &str| r.trim()), |s: &str| {
!s.is_empty()
}),
)),
|(has_tilde, path)| {
if has_tilde {
ChangeDirectoryType::HomeDirectory(path)
} else if let Some(path) = path {
ChangeDirectoryType::Path(path)
} else {
ChangeDirectoryType::HomeDirectory(None)
}
},
),
)),
),
|op| {
if let Some((has_tilde, path)) = op {
CacheCommand::Cd { has_tilde, path }
} else {
CacheCommand::Cd { has_tilde: false, path: None }
}
},
CacheCommand::Cd,
)(command)
}

Expand Down
33 changes: 21 additions & 12 deletions src/cache_command/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub fn process_command(
db: &mut impl DbOp<String, String>,
script_context: &mut BTreeMap<String, RefPrimitive>,
current_cache: &mut String,
previous_dir: &mut PathBuf,
line: &str,
) -> anyhow::Result<()> {
match parse_command(line) {
Expand Down Expand Up @@ -230,20 +231,28 @@ pub fn process_command(
);
}
}
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))
CacheCommand::Cd(cdt) => {
let path_buf = {
match cdt {
super::ChangeDirectoryType::HomeDirectory(path) => {
path.and_then(|p| {
dirs::home_dir().map(|hd| hd.join(p))
})
.or_else(dirs::home_dir)
.context(
"could not change directory. path {path:?} not found!",
)?
}
super::ChangeDirectoryType::Path(path) => {
PathBuf::from(path)
}
})
.or_else(dirs::home_dir)
.context(
"could not change directory. path {path:?} not found!",
)?;
super::ChangeDirectoryType::Previous => {
previous_dir.clone()
},
}
};
if path_buf.exists() {
*previous_dir = std::env::current_dir()?;
std::env::set_current_dir(path_buf.as_path())?;
} else {
return Err(anyhow::Error::msg(format!(
Expand Down
7 changes: 5 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,16 @@ fn start_app(
};
let mut rl = editor::build_editor(history_path);
let mut script_context = BTreeMap::new();
let mut previous_dir = std::env::current_dir()?;

if let Some(dc) = default_cache {
process_command(
db,
&mut script_context,
&mut current_cache,
&mut previous_dir,
&format!("use {dc}"),
)
.unwrap();
)?;
}
loop {
let readline = editor::read_line(&mut rl, &current_cache);
Expand All @@ -121,6 +123,7 @@ fn start_app(
db,
&mut script_context,
&mut current_cache,
&mut previous_dir,
&line,
) {
Ok(_) => (),
Expand Down

0 comments on commit 7ea334e

Please sign in to comment.