Skip to content

Commit

Permalink
Use shell struct in clap args
Browse files Browse the repository at this point in the history
  • Loading branch information
jpikl committed Apr 20, 2024
1 parent 752b90d commit 6401fd9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/commands/x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ struct Args {
/// Shell used to evaluate `{# ...}` expressions.
///
/// Default value: `cmd` on Windows, `sh` everywhere else.
#[arg(short, long, env = "SHELL")]
shell: Option<String>,
#[arg(short, long, env = "SHELL", default_value_t = Shell::default(), hide_default_value = true)]
shell: Shell,

/// Wrap output of every pattern expression in quotes
///
Expand All @@ -189,7 +189,7 @@ fn run(context: &Context, args: &Args) -> Result<()> {
if let Some(pattern) = pattern.try_simplify() {
eval_simple_pattern(context, &pattern)
} else {
eval_pattern(context, &pattern, args.shell.as_deref())
eval_pattern(context, &pattern, args.shell.clone())
}
}

Expand All @@ -210,7 +210,7 @@ fn eval_simple_pattern(context: &Context, pattern: &SimplePattern) -> Result<()>
Ok(())
}

fn eval_pattern(context: &Context, pattern: &Pattern, shell: Option<&str>) -> Result<()> {
fn eval_pattern(context: &Context, pattern: &Pattern, shell: Shell) -> Result<()> {
let mut builder = CommandBuilder::new(context, shell);
let mut children = Vec::new();
let mut items = Vec::new();
Expand Down Expand Up @@ -337,10 +337,10 @@ struct CommandBuilder<'a> {
}

impl<'a> CommandBuilder<'a> {
fn new(context: &'a Context, shell: Option<&'a str>) -> Self {
fn new(context: &'a Context, shell: Shell) -> Self {
Self {
context,
shell: shell.map(Shell::new).unwrap_or_default(),
shell,
stdbuf: StdBuf::default(),
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/shell.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use derive_more::Display;
use std::borrow::Cow;
use std::ffi::OsStr;
use std::ffi::OsString;
use std::fmt;
use std::fmt::Formatter;
use std::path::Path;
use std::process::Command;

Expand Down Expand Up @@ -29,8 +32,15 @@ impl ShellKind {
}
}

#[derive(Clone)]
pub struct Shell(OsString);

impl Display for Shell {
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
write!(fmt, "{}", self.0.to_string_lossy())
}
}

impl Default for Shell {
#[cfg(target_os = "windows")]
fn default() -> Self {
Expand All @@ -43,6 +53,12 @@ impl Default for Shell {
}
}

impl From<&OsStr> for Shell {
fn from(value: &OsStr) -> Self {
Self::new(value)
}
}

impl Shell {
pub fn new(bin: impl Into<OsString>) -> Self {
Self(bin.into())
Expand Down

0 comments on commit 6401fd9

Please sign in to comment.