Skip to content
Snippets Groups Projects
Commit 14308c1e authored by stratact's avatar stratact
Browse files

Iterative improvements

parent 615d7b8c
No related branches found
No related tags found
No related merge requests found
......@@ -229,37 +229,28 @@ impl DirectoryStack {
)
}
pub(crate) fn cd<I: IntoIterator>(
pub(crate) fn cd(
&mut self,
args: I,
args: &[small::String],
variables: &Variables,
) -> Result<(), Cow<'static, str>>
where
I::Item: AsRef<str>,
{
match args.into_iter().nth(1) {
) -> Result<(), Cow<'static, str>> {
match args.get(1) {
Some(dir) => {
if let Some(Value::Array(cdpath)) = variables.get_ref("CDPATH") {
let dir = dir.as_ref();
if dir == "-" {
self.switch_to_previous_directory(variables)
} else {
if !cdpath.is_empty() {
for path in cdpath {
let mut path = path.to_string();
path.push('/');
path.push_str(dir);
let res = self.change_and_push_dir(&path, variables);
if res.is_ok() {
self.dirs.remove(1);
return res;
}
}
}
let res = self.change_and_push_dir(dir, variables);
let check_cdpath_first = cdpath
.iter()
.map(|path| {
let path_dir = [path, "/", dir].concat();
self.change_and_push_dir(&path_dir, variables)
})
.find(Result::is_ok)
.unwrap_or_else(|| self.change_and_push_dir(dir, variables));
self.dirs.remove(1);
res
check_cdpath_first
}
} else {
unreachable!();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment