Skip to content

Commit

Permalink
zsh support
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmadigan committed May 8, 2024
1 parent b351eca commit 29d8ce7
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.vscode/launch.json

# Cargo build dir
/target

Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ enum Shell {
#[default]
Bash,
Python,
Zsh,
#[value(skip)]
Custom {
program: String,
Expand All @@ -315,6 +316,7 @@ impl Display for Shell {
match self {
Self::Bash => f.write_str("bash"),
Self::Python => f.write_str("python"),
Self::Zsh => f.write_str("zsh"),
Self::Custom { program, args, .. } => f.write_str(
&iter::once(program)
.chain(args)
Expand Down Expand Up @@ -342,7 +344,7 @@ impl Merge for Shell {
impl Shell {
fn line_split(&self) -> &str {
match self {
Self::Bash | Self::Python => " \\",
Self::Bash | Self::Python | Self::Zsh => " \\",
Self::Custom { line_split, .. } => line_split,
}
}
Expand All @@ -351,6 +353,7 @@ impl Shell {
match self {
Self::Bash => "bash",
Self::Python => "python",
Self::Zsh => "zsh",
Self::Custom { program, .. } => program,
}
}
Expand All @@ -370,6 +373,7 @@ impl Shell {
match self {
Self::Bash => spawn::bash(timeout, environment, width, height),
Self::Python => spawn::python(timeout, environment, width, height),
Self::Zsh => spawn::zsh(timeout, environment, width, height),
Self::Custom {
program,
args,
Expand Down
4 changes: 4 additions & 0 deletions src/config/de/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::config::Shell;
enum Variant {
Bash,
Python,
Zsh,
Custom,
}

Expand Down Expand Up @@ -41,6 +42,7 @@ impl<'de> de::Visitor<'de> for Visitor {
match v {
"bash" | "Bash" => Ok(Shell::Bash),
"python" | "Python" => Ok(Shell::Python),
"zsh" | "Zsh" => Ok(Shell::Zsh),
_ => Err(E::invalid_value(
de::Unexpected::Str(v),
&"supported shell (e.g. bash or python) or a custom shell",
Expand All @@ -57,6 +59,7 @@ impl<'de> de::Visitor<'de> for Visitor {
match tag {
Variant::Bash => variant.unit_variant().map(|_| Shell::Bash),
Variant::Python => variant.unit_variant().map(|_| Shell::Python),
Variant::Zsh => variant.unit_variant().map(|_| Shell::Zsh),
Variant::Custom => variant.struct_variant(CUSTOM_FIELDS, CustomVisitor),
}
}
Expand Down Expand Up @@ -109,6 +112,7 @@ mod tests {
fn visit_str() -> serde_yaml::Result<()> {
assert_eq!(serde_yaml::from_str::<Shell>("bash")?, Shell::Bash);
assert_eq!(serde_yaml::from_str::<Shell>("python")?, Shell::Python);
assert_eq!(serde_yaml::from_str::<Shell>("zsh")?, Shell::Zsh);
assert!(serde_yaml::from_str::<Shell>("custom").is_err());
Ok(())
}
Expand Down
44 changes: 42 additions & 2 deletions src/config/spawn.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{
ffi::OsStr,
io::{self, BufRead, BufReader, Read, Write},
ops::{Deref, DerefMut},
ops::DerefMut,
process::Command,
time::{Duration, Instant},
};
Expand All @@ -19,6 +19,38 @@ use os_str_bytes::OsStrBytes;

use crate::asciicast::Event;

pub(super) fn zsh<I, K, V>(
timeout: Duration,
environment: I,
width: u16,
height: u16,
) -> color_eyre::Result<ShellSession>
where
I: IntoIterator<Item = (K, V)>,
K: AsRef<OsStr>,
V: AsRef<OsStr>,
{
const PROMPT: &str = "AUTOCAST_PROMPT";
const PROMPT_COMMAND: &str =
"PS1=AUTOCAST_PROMPT; unset PROMPT_COMMAND; unset zle_bracketed_paste";

let mut command = Command::new("zsh");
command.arg("--no-rcs");
command
.envs(environment)
.env("PS1", PROMPT)
.env("PROMPT_COMMAND", PROMPT_COMMAND);

ShellSession::spawn(
command,
width,
height,
String::from(PROMPT),
Some(String::from("exit")),
timeout,
)
}

pub(super) fn bash<I, K, V>(
timeout: Duration,
environment: I,
Expand Down Expand Up @@ -256,7 +288,15 @@ pub trait Wait: Process {
#[cfg(unix)]
impl Wait for UnixProcess {
fn wait(&self, _: Duration) -> color_eyre::Result<()> {
self.deref().wait().map(|_| ()).map_err(Into::into)
#[cfg(target_os = "macos")]
{
Ok(())
}

#[cfg(target_os = "linux")]
{
self.deref().wait().map(|_| ()).map_err(Into::into)
}
}
}

Expand Down

0 comments on commit 29d8ce7

Please sign in to comment.