rustfmt'd the codebase
This commit is contained in:
@@ -47,18 +47,26 @@ fn print_dirs() -> ShResult<()> {
|
||||
|
||||
fn change_directory(target: &PathBuf, blame: Span) -> ShResult<()> {
|
||||
if !target.is_dir() {
|
||||
return Err(
|
||||
ShErr::at(ShErrKind::ExecFail, blame, format!("not a directory: '{}'", target.display().fg(next_color())))
|
||||
);
|
||||
return Err(ShErr::at(
|
||||
ShErrKind::ExecFail,
|
||||
blame,
|
||||
format!("not a directory: '{}'", target.display().fg(next_color())),
|
||||
));
|
||||
}
|
||||
|
||||
if let Err(e) = state::change_dir(target) {
|
||||
return Err(
|
||||
ShErr::at(ShErrKind::ExecFail, blame, format!("Failed to change directory: '{}'", e.fg(Color::Red)))
|
||||
);
|
||||
return Err(ShErr::at(
|
||||
ShErrKind::ExecFail,
|
||||
blame,
|
||||
format!("Failed to change directory: '{}'", e.fg(Color::Red)),
|
||||
));
|
||||
}
|
||||
let new_dir = env::current_dir().map_err(|e| {
|
||||
ShErr::at(ShErrKind::ExecFail, blame, format!("Failed to get current directory: '{}'", e.fg(Color::Red)))
|
||||
ShErr::at(
|
||||
ShErrKind::ExecFail,
|
||||
blame,
|
||||
format!("Failed to get current directory: '{}'", e.fg(Color::Red)),
|
||||
)
|
||||
})?;
|
||||
unsafe { env::set_var("PWD", new_dir) };
|
||||
Ok(())
|
||||
@@ -74,24 +82,32 @@ fn parse_stack_idx(arg: &str, blame: Span, cmd: &str) -> ShResult<StackIdx> {
|
||||
};
|
||||
|
||||
if digits.is_empty() {
|
||||
return Err(
|
||||
ShErr::at(ShErrKind::ExecFail, blame, format!(
|
||||
return Err(ShErr::at(
|
||||
ShErrKind::ExecFail,
|
||||
blame,
|
||||
format!(
|
||||
"{cmd}: missing index after '{}'",
|
||||
if from_top { "+" } else { "-" }
|
||||
))
|
||||
);
|
||||
),
|
||||
));
|
||||
}
|
||||
|
||||
for ch in digits.chars() {
|
||||
if !ch.is_ascii_digit() {
|
||||
return Err(
|
||||
ShErr::at(ShErrKind::ExecFail, blame, format!("{cmd}: invalid argument: '{}'",arg.fg(next_color())))
|
||||
);
|
||||
return Err(ShErr::at(
|
||||
ShErrKind::ExecFail,
|
||||
blame,
|
||||
format!("{cmd}: invalid argument: '{}'", arg.fg(next_color())),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
let n = digits.parse::<usize>().map_err(|e| {
|
||||
ShErr::at(ShErrKind::ExecFail, blame, format!("{cmd}: invalid index: '{}'",e.fg(next_color())))
|
||||
ShErr::at(
|
||||
ShErrKind::ExecFail,
|
||||
blame,
|
||||
format!("{cmd}: invalid index: '{}'", e.fg(next_color())),
|
||||
)
|
||||
})?;
|
||||
|
||||
if from_top {
|
||||
@@ -112,7 +128,9 @@ pub fn pushd(node: Node) -> ShResult<()> {
|
||||
};
|
||||
|
||||
let mut argv = prepare_argv(argv)?;
|
||||
if !argv.is_empty() { argv.remove(0); }
|
||||
if !argv.is_empty() {
|
||||
argv.remove(0);
|
||||
}
|
||||
|
||||
let mut dir = None;
|
||||
let mut rotate_idx = None;
|
||||
@@ -126,20 +144,29 @@ pub fn pushd(node: Node) -> ShResult<()> {
|
||||
} else if arg == "-n" {
|
||||
no_cd = true;
|
||||
} else if arg.starts_with('-') {
|
||||
return Err(
|
||||
ShErr::at(ShErrKind::ExecFail, blame, format!("pushd: invalid option: '{}'", arg.fg(next_color())))
|
||||
);
|
||||
return Err(ShErr::at(
|
||||
ShErrKind::ExecFail,
|
||||
blame,
|
||||
format!("pushd: invalid option: '{}'", arg.fg(next_color())),
|
||||
));
|
||||
} else {
|
||||
if dir.is_some() {
|
||||
return Err(
|
||||
ShErr::at(ShErrKind::ExecFail, blame, "pushd: too many arguments")
|
||||
);
|
||||
return Err(ShErr::at(
|
||||
ShErrKind::ExecFail,
|
||||
blame,
|
||||
"pushd: too many arguments",
|
||||
));
|
||||
}
|
||||
let target = PathBuf::from(&arg);
|
||||
if !target.is_dir() {
|
||||
return Err(
|
||||
ShErr::at(ShErrKind::ExecFail, blame, format!("pushd: not a directory: '{}'", target.display().fg(next_color())))
|
||||
);
|
||||
return Err(ShErr::at(
|
||||
ShErrKind::ExecFail,
|
||||
blame,
|
||||
format!(
|
||||
"pushd: not a directory: '{}'",
|
||||
target.display().fg(next_color())
|
||||
),
|
||||
));
|
||||
}
|
||||
dir = Some(target);
|
||||
}
|
||||
@@ -193,7 +220,9 @@ pub fn popd(node: Node) -> ShResult<()> {
|
||||
};
|
||||
|
||||
let mut argv = prepare_argv(argv)?;
|
||||
if !argv.is_empty() { argv.remove(0); }
|
||||
if !argv.is_empty() {
|
||||
argv.remove(0);
|
||||
}
|
||||
|
||||
let mut remove_idx = None;
|
||||
let mut no_cd = false;
|
||||
@@ -206,9 +235,11 @@ pub fn popd(node: Node) -> ShResult<()> {
|
||||
} else if arg == "-n" {
|
||||
no_cd = true;
|
||||
} else if arg.starts_with('-') {
|
||||
return Err(
|
||||
ShErr::at(ShErrKind::ExecFail, blame, format!("popd: invalid option: '{}'", arg.fg(next_color())))
|
||||
);
|
||||
return Err(ShErr::at(
|
||||
ShErrKind::ExecFail,
|
||||
blame,
|
||||
format!("popd: invalid option: '{}'", arg.fg(next_color())),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -221,9 +252,11 @@ pub fn popd(node: Node) -> ShResult<()> {
|
||||
if let Some(dir) = dir {
|
||||
change_directory(&dir, blame.clone())?;
|
||||
} else {
|
||||
return Err(
|
||||
ShErr::at(ShErrKind::ExecFail, blame, "popd: directory stack empty")
|
||||
);
|
||||
return Err(ShErr::at(
|
||||
ShErrKind::ExecFail,
|
||||
blame,
|
||||
"popd: directory stack empty",
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -233,9 +266,11 @@ pub fn popd(node: Node) -> ShResult<()> {
|
||||
let dirs = m.dirs_mut();
|
||||
let idx = n - 1;
|
||||
if idx >= dirs.len() {
|
||||
return Err(
|
||||
ShErr::at(ShErrKind::ExecFail, blame.clone(), format!("popd: directory index out of range: +{n}"))
|
||||
);
|
||||
return Err(ShErr::at(
|
||||
ShErrKind::ExecFail,
|
||||
blame.clone(),
|
||||
format!("popd: directory index out of range: +{n}"),
|
||||
));
|
||||
}
|
||||
dirs.remove(idx);
|
||||
Ok(())
|
||||
@@ -245,7 +280,11 @@ pub fn popd(node: Node) -> ShResult<()> {
|
||||
write_meta(|m| -> ShResult<()> {
|
||||
let dirs = m.dirs_mut();
|
||||
let actual = dirs.len().checked_sub(n + 1).ok_or_else(|| {
|
||||
ShErr::at(ShErrKind::ExecFail, blame.clone(), format!("popd: directory index out of range: -{n}"))
|
||||
ShErr::at(
|
||||
ShErrKind::ExecFail,
|
||||
blame.clone(),
|
||||
format!("popd: directory index out of range: -{n}"),
|
||||
)
|
||||
})?;
|
||||
dirs.remove(actual);
|
||||
Ok(())
|
||||
@@ -265,9 +304,11 @@ pub fn popd(node: Node) -> ShResult<()> {
|
||||
change_directory(&dir, blame.clone())?;
|
||||
print_dirs()?;
|
||||
} else {
|
||||
return Err(
|
||||
ShErr::at(ShErrKind::ExecFail, blame, "popd: directory stack empty")
|
||||
);
|
||||
return Err(ShErr::at(
|
||||
ShErrKind::ExecFail,
|
||||
blame,
|
||||
"popd: directory stack empty",
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -285,7 +326,9 @@ pub fn dirs(node: Node) -> ShResult<()> {
|
||||
};
|
||||
|
||||
let mut argv = prepare_argv(argv)?;
|
||||
if !argv.is_empty() { argv.remove(0); }
|
||||
if !argv.is_empty() {
|
||||
argv.remove(0);
|
||||
}
|
||||
|
||||
let mut abbreviate_home = true;
|
||||
let mut one_per_line = false;
|
||||
@@ -306,14 +349,18 @@ pub fn dirs(node: Node) -> ShResult<()> {
|
||||
target_idx = Some(parse_stack_idx(&arg, blame.clone(), "dirs")?);
|
||||
}
|
||||
_ if arg.starts_with('-') => {
|
||||
return Err(
|
||||
ShErr::at(ShErrKind::ExecFail, blame, format!("dirs: invalid option: '{}'", arg.fg(next_color())))
|
||||
);
|
||||
return Err(ShErr::at(
|
||||
ShErrKind::ExecFail,
|
||||
blame,
|
||||
format!("dirs: invalid option: '{}'", arg.fg(next_color())),
|
||||
));
|
||||
}
|
||||
_ => {
|
||||
return Err(
|
||||
ShErr::at(ShErrKind::ExecFail, blame, format!("dirs: unexpected argument: '{}'", arg.fg(next_color())))
|
||||
);
|
||||
return Err(ShErr::at(
|
||||
ShErrKind::ExecFail,
|
||||
blame,
|
||||
format!("dirs: unexpected argument: '{}'", arg.fg(next_color())),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -358,15 +405,17 @@ pub fn dirs(node: Node) -> ShResult<()> {
|
||||
if let Some(dir) = target {
|
||||
dirs = vec![dir.clone()];
|
||||
} else {
|
||||
return Err(
|
||||
ShErr::at(ShErrKind::ExecFail, blame, format!(
|
||||
return Err(ShErr::at(
|
||||
ShErrKind::ExecFail,
|
||||
blame,
|
||||
format!(
|
||||
"dirs: directory index out of range: {}",
|
||||
match idx {
|
||||
StackIdx::FromTop(n) => format!("+{n}"),
|
||||
StackIdx::FromBottom(n) => format!("-{n}"),
|
||||
}
|
||||
))
|
||||
);
|
||||
),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user