aboutsummaryrefslogtreecommitdiffstats
path: root/src/run
diff options
context:
space:
mode:
Diffstat (limited to 'src/run')
-rw-r--r--src/run/builtin.rs22
-rw-r--r--src/run/mod.rs23
2 files changed, 21 insertions, 24 deletions
diff --git a/src/run/builtin.rs b/src/run/builtin.rs
index 24badd0..99e0103 100644
--- a/src/run/builtin.rs
+++ b/src/run/builtin.rs
@@ -34,13 +34,13 @@ fn read_args<T: ArgParse>(args: &[BString], w: &mut dyn Write) -> std::result::R
w.write_all(b"\n")?;
}
ArgParseError::MissingArg(arg) => {
- write!(w, "argument `{arg}` is missing\n")?;
+ writeln!(w, "argument `{arg}` is missing")?;
}
ArgParseError::MissingArgValue(arg) => {
- write!(w, "argument `{arg}` is missing its value\n")?;
+ writeln!(w, "argument `{arg}` is missing its value")?;
}
ArgParseError::ArgValueParseError(arg, err) => {
- write!(w, "failed to parse value of `{arg}`: {err}")?;
+ writeln!(w, "failed to parse value of `{arg}`: {err}")?;
}
}
@@ -66,7 +66,7 @@ impl Builtin for cd {
std::mem::swap(&mut dir, &mut se.lock().unwrap().prev_path);
- let target_path: BString = match args.get(0).map(|v| &v[..]) {
+ let target_path: BString = match args.first().map(|v| &v[..]) {
Some(b"-") => dir,
Some(path) => path.to_vec(),
None => {
@@ -79,7 +79,7 @@ impl Builtin for cd {
}
};
- if let Err(_) = set_current_dir(OsStr::from_bytes(&target_path)) {
+ if set_current_dir(OsStr::from_bytes(&target_path)).is_err() {
write!(stdout, "failed to cd into ")?;
stdout.write_all(&target_path)?;
writeln!(stdout, "\n")?;
@@ -149,7 +149,7 @@ impl Builtin for Sink {
stdin: &mut dyn Read,
_stdout: &mut dyn Write,
) -> Result {
- let Some(path) = args.get(0) else {
+ let Some(path) = args.first() else {
return Err(Error::Exit(1));
};
let path = PathBuf::from(OsStr::from_bytes(path));
@@ -180,7 +180,7 @@ impl Builtin for from {
_stdin: &mut dyn Read,
stdout: &mut dyn Write,
) -> Result {
- let Some(path) = args.get(0) else {
+ let Some(path) = args.first() else {
return Err(Error::Exit(1));
};
let path = PathBuf::from(OsStr::from_bytes(path));
@@ -289,10 +289,8 @@ impl Builtin for history {
let path_prefix = if args.here {
Some(current_dir()?.as_os_str().as_bytes().to_vec())
- } else if let Some(path) = args.at {
- Some(path.as_os_str().as_bytes().to_vec())
} else {
- None
+ args.at.map(|path| path.as_os_str().as_bytes().to_vec())
};
let min_time = None;
@@ -312,12 +310,12 @@ impl Builtin for history {
)
} else {
let Ok(hist) = crate::history::HistoryQueryer::new() else {
- write!(stdout, "error opening global history file\n")?;
+ writeln!(stdout, "error opening global history file")?;
return Err(Error::Exit(-1));
};
let Ok(it) = hist.query(min_time, max_time, path_prefix.as_deref(), args.strict) else {
- write!(stdout, "error querying global history\n")?;
+ writeln!(stdout, "error querying global history")?;
return Err(Error::Exit(-1));
};
diff --git a/src/run/mod.rs b/src/run/mod.rs
index 6234b96..5666574 100644
--- a/src/run/mod.rs
+++ b/src/run/mod.rs
@@ -25,7 +25,7 @@ impl ExecError {
pub fn error_message(&self) -> String {
match self {
ExecError::UnknownVariable(items) => {
- format!("unknown variable: {}", String::from_utf8_lossy(&items))
+ format!("unknown variable: {}", String::from_utf8_lossy(items))
}
ExecError::ExecError(exit_code) => format!("{exit_code}"),
ExecError::SpawnIO(cmd, error) => match error.kind() {
@@ -43,8 +43,8 @@ impl ExecError {
io::ErrorKind::InvalidFilename => {
format!("{cmd} is not a valid file name")
}
- io::ErrorKind::ArgumentListTooLong => format!("too many arguments"),
- io::ErrorKind::Interrupted => format!("got interrupted"),
+ io::ErrorKind::ArgumentListTooLong => String::from("too many arguments"),
+ io::ErrorKind::Interrupted => String::from("got interrupted"),
io::ErrorKind::Unsupported => format!("{cmd} is not supported"),
e => format!("I am surprised you can get this error here: {e:?}"),
},
@@ -335,18 +335,17 @@ impl parse::Expander for Executor {
return Err(ExecError::UnknownVariable(var));
}
- if var[0].is_ascii_digit() {
- if let Some(x) = String::from_utf8(var.clone())
+ if var[0].is_ascii_digit()
+ && let Some(x) = String::from_utf8(var.clone())
.ok()
.and_then(|x| x.parse::<usize>().ok())
- {
- if let Some(args) = &self.args {
- if x < args.len() {
- return Ok(args[x].clone());
- }
- } else if let Some(arg) = std::env::args_os().skip(x).next() {
- return Ok(arg.into_encoded_bytes());
+ {
+ if let Some(args) = &self.args {
+ if x < args.len() {
+ return Ok(args[x].clone());
}
+ } else if let Some(arg) = std::env::args_os().nth(x) {
+ return Ok(arg.into_encoded_bytes());
}
}