use std::io::Write;
use std::path::PathBuf;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum OscEvent {
PromptStart,
PromptEnd,
CommandStart,
CommandEnd(Option<i32>),
Cwd(PathBuf, Option<String>),
Notify(String),
Progress(u8, u8),
InlineImage(Vec<u8>),
}
#[derive(Default)]
pub struct OscSniffer {
carry: Vec<u8>,
kitty: Option<KittyNote>,
}
#[derive(Default)]
struct KittyNote {
id: String,
title: KittyField,
body: KittyField,
}
#[derive(Default, Clone)]
struct KittyField {
bytes: Vec<u8>,
pending: Vec<u8>,
}
const KITTY_B64: base64::engine::GeneralPurpose = base64::engine::GeneralPurpose::new(
&base64::alphabet::STANDARD,
base64::engine::GeneralPurposeConfig::new()
.with_decode_padding_mode(base64::engine::DecodePaddingMode::Indifferent),
);
impl KittyField {
fn push(&mut self, payload: &str, b64: bool) -> Option<()> {
use base64::Engine;
if !b64 {
self.finish()?;
self.bytes.extend_from_slice(payload.as_bytes());
return Some(());
}
self.pending.extend_from_slice(payload.trim().as_bytes());
let n = if self.pending.contains(&b'=') {
self.pending.len()
} else {
self.pending.len() / 4 * 4
};
let raw = KITTY_B64.decode(&self.pending[..n]).ok()?;
self.bytes.extend_from_slice(&raw);
self.pending.drain(..n);
Some(())
}
fn finish(&mut self) -> Option<()> {
use base64::Engine;
if !self.pending.is_empty() {
let raw = KITTY_B64.decode(&self.pending).ok()?;
self.bytes.extend_from_slice(&raw);
self.pending.clear();
}
Some(())
}
fn len(&self) -> usize {
self.bytes.len() + self.pending.len()
}
fn text(&self) -> String {
String::from_utf8_lossy(&self.bytes).into_owned()
}
}
pub type SniffedEvent = (usize, OscEvent);
const CARRY_MAX: usize = 4096;
const KITTY_CARRY_MAX: usize = CARRY_MAX + 1024;
const IMAGE_CARRY_MAX: usize = 8 * 1024 * 1024;
#[derive(Clone, Copy, PartialEq, Eq)]
enum OscKind {
SemanticPrompt, Cwd, Notify, UrxvtNotify, KittyNotify, ITerm2File, }
enum Classify {
No,
NeedMore,
Ours(OscKind, usize),
}
enum Terminator {
Done(usize, usize),
NeedMore,
Abort(usize),
}
const OSC_PREFIXES: &[(&[u8], OscKind)] = &[
(b"]1337;", OscKind::ITerm2File),
(b"]133;", OscKind::SemanticPrompt),
(b"]777;", OscKind::UrxvtNotify),
(b"]7;", OscKind::Cwd),
(b"]99;", OscKind::KittyNotify),
(b"]9;", OscKind::Notify),
];
fn note_text(title: &str, body: &str) -> Option<String> {
match (title.trim().is_empty(), body.trim().is_empty()) {
(true, true) => None,
(false, true) => Some(title.to_string()),
(true, false) => Some(body.to_string()),
(false, false) => Some(format!("{title}: {body}")),
}
}
fn classify(bytes: &[u8]) -> Classify {
for (prefix, kind) in OSC_PREFIXES {
let avail = &bytes[1..];
let n = avail.len().min(prefix.len());
if avail[..n] == prefix[..n] {
if n < prefix.len() {
return Classify::NeedMore;
}
return Classify::Ours(*kind, 1 + prefix.len());
}
}
Classify::No
}
fn find_terminator(bytes: &[u8], body_start: usize) -> Terminator {
let mut i = body_start;
while i < bytes.len() {
match bytes[i] {
0x07 => return Terminator::Done(i, i + 1),
0x1b => {
if i + 1 >= bytes.len() {
return Terminator::NeedMore;
}
if bytes[i + 1] == b'\\' {
return Terminator::Done(i, i + 2);
}
return Terminator::Abort(i);
}
_ => i += 1,
}
}
Terminator::NeedMore
}
pub(crate) fn percent_decode(bytes: &[u8]) -> Vec<u8> {
let mut out = Vec::with_capacity(bytes.len());
let mut i = 0;
while i < bytes.len() {
if bytes[i] == b'%'
&& i + 2 < bytes.len()
&& let (Some(hi), Some(lo)) = (
(bytes[i + 1] as char).to_digit(16),
(bytes[i + 2] as char).to_digit(16),
)
{
out.push((hi * 16 + lo) as u8);
i += 3;
} else {
out.push(bytes[i]);
i += 1;
}
}
out
}
fn parse_event(kind: OscKind, body: &[u8]) -> Option<OscEvent> {
match kind {
OscKind::SemanticPrompt => match body.first()? {
b'A' => Some(OscEvent::PromptStart),
b'B' => Some(OscEvent::PromptEnd),
b'C' => Some(OscEvent::CommandStart),
b'D' => {
let exit = (body.len() > 2 && body[1] == b';')
.then(|| std::str::from_utf8(&body[2..]).ok()?.parse::<i32>().ok())
.flatten();
Some(OscEvent::CommandEnd(exit))
}
_ => None,
},
OscKind::Cwd => {
let (path, host) = if let Some(rest) = body.strip_prefix(b"file://") {
let slash = rest.iter().position(|&b| b == b'/')?;
let host =
(slash > 0).then(|| String::from_utf8_lossy(&rest[..slash]).into_owned());
(&rest[slash..], host)
} else if body.first() == Some(&b'/') {
(body, None)
} else {
return None;
};
let decoded = percent_decode(path);
Some(OscEvent::Cwd(
PathBuf::from(String::from_utf8_lossy(&decoded).into_owned()),
host,
))
}
OscKind::Notify => {
if let Some(rest) = body.strip_prefix(b"4;") {
let mut parts = rest.split(|&b| b == b';');
let state = std::str::from_utf8(parts.next().unwrap_or_default())
.ok()?
.parse::<u8>()
.ok()
.filter(|s| *s <= 4)?;
let percent = parts
.next()
.and_then(|p| std::str::from_utf8(p).ok())
.and_then(|p| p.parse::<u32>().ok())
.unwrap_or(0)
.min(100) as u8;
return Some(OscEvent::Progress(state, percent));
}
Some(OscEvent::Notify(String::from_utf8_lossy(body).into_owned()))
}
OscKind::UrxvtNotify => {
let rest = String::from_utf8_lossy(body.strip_prefix(b"notify;")?).into_owned();
let (title, text) = rest.split_once(';').unwrap_or((rest.as_str(), ""));
note_text(title, text).map(OscEvent::Notify)
}
OscKind::KittyNotify => None,
OscKind::ITerm2File => {
let rest = body.strip_prefix(b"File=")?;
let colon = rest.iter().position(|&b| b == b':')?;
let inline = rest[..colon]
.split(|&b| b == b';')
.any(|p| p == b"inline=1");
if !inline {
return None;
}
use base64::Engine;
let b64: Vec<u8> = rest[colon + 1..]
.iter()
.copied()
.filter(|b| !b.is_ascii_whitespace())
.collect();
let data = base64::engine::general_purpose::STANDARD
.decode(&b64)
.ok()?;
(!data.is_empty()).then_some(OscEvent::InlineImage(data))
}
}
}
impl OscSniffer {
pub fn scan(&mut self, chunk: &[u8]) -> Vec<SniffedEvent> {
let mut out = Vec::new();
let owned: Vec<u8>;
let (buf, base) = if self.carry.is_empty() {
(chunk, 0usize)
} else {
let mut v = std::mem::take(&mut self.carry);
v.extend_from_slice(chunk);
owned = v;
let b = owned.len() - chunk.len();
(owned.as_slice(), b)
};
let mut i = 0;
while i < buf.len() {
let Some(rel) = buf[i..].iter().position(|&b| b == 0x1b) else {
break;
};
let esc = i + rel;
match classify(&buf[esc..]) {
Classify::No => i = esc + 1,
Classify::NeedMore => {
self.hold(&buf[esc..]);
return out;
}
Classify::Ours(kind, body_start) => {
match find_terminator(&buf[esc..], body_start) {
Terminator::Done(body_end, seq_end) => {
let body = &buf[esc + body_start..esc + body_end];
let ev = if kind == OscKind::KittyNotify {
self.kitty_chunk(body)
} else {
parse_event(kind, body)
};
if let Some(ev) = ev {
out.push((esc + seq_end - base, ev));
}
i = esc + seq_end;
}
Terminator::NeedMore => {
self.hold(&buf[esc..]);
return out;
}
Terminator::Abort(off) => i = esc + off,
}
}
}
}
out
}
fn kitty_chunk(&mut self, body: &[u8]) -> Option<OscEvent> {
let body = String::from_utf8_lossy(body);
let (meta, payload) = body.split_once(';')?;
let (mut id, mut done, mut kind, mut b64) = ("", true, "title", false);
for pair in meta.split(':') {
match pair.split_once('=') {
Some(("i", v)) => id = v,
Some(("d", v)) => done = v != "0",
Some(("p", v)) => kind = v,
Some(("e", v)) => b64 = v == "1",
_ => {}
}
}
let mut note = match self.kitty.take() {
Some(n) if n.id == id => n,
_ => KittyNote {
id: id.to_string(),
..KittyNote::default()
},
};
let field = match kind {
"title" => Some(&mut note.title),
"body" => Some(&mut note.body),
_ => None,
};
if let Some(field) = field
&& field.len() + payload.len() <= CARRY_MAX
{
let mut next = field.clone();
if next.push(payload, b64).is_none() {
if !done {
self.kitty = Some(note);
}
return None;
}
*field = next;
}
if !done {
self.kitty = Some(note);
return None;
}
if note.title.finish().is_none() || note.body.finish().is_none() {
return None;
}
note_text(¬e.title.text(), ¬e.body.text()).map(OscEvent::Notify)
}
fn hold(&mut self, partial: &[u8]) {
let cap = match classify(partial) {
Classify::Ours(OscKind::ITerm2File, _) => IMAGE_CARRY_MAX,
Classify::Ours(OscKind::KittyNotify, _) => KITTY_CARRY_MAX,
_ => CARRY_MAX,
};
if partial.len() > cap {
self.carry.clear();
} else {
self.carry = partial.to_vec();
}
}
}
const ZSH_SHIM_ZSHENV: &str = r#"# croft shell integration bootstrap (auto-generated; do not edit)
_croft_shim_zdotdir="$ZDOTDIR"
# A CROFT_USER_ZDOTDIR pointing back at the shim is a poisoned inheritance
# (croft launched from a croft pane); the user's real dotfiles live in HOME.
if [[ -n "$CROFT_USER_ZDOTDIR" && "$CROFT_USER_ZDOTDIR" == "$_croft_shim_zdotdir" ]]; then
export CROFT_USER_ZDOTDIR="$HOME"
fi
ZDOTDIR="${CROFT_USER_ZDOTDIR:-$HOME}"
[[ "$ZDOTDIR" != "$_croft_shim_zdotdir" && -f "$ZDOTDIR/.zshenv" ]] && builtin source "$ZDOTDIR/.zshenv"
# The sourced file may be another terminal's injection shim (Ghostty, kitty
# point ZDOTDIR at their own dir when launching croft as their command);
# their .zshenv restores the REAL ZDOTDIR when sourced, so whatever it holds
# now is the user's true dotfile dir. A user .zshenv that re-roots ZDOTDIR
# (XDG setups) is honoured the same way.
if [[ -z "$ZDOTDIR" || "$ZDOTDIR" == "$_croft_shim_zdotdir" ]]; then
export CROFT_USER_ZDOTDIR="$HOME"
else
export CROFT_USER_ZDOTDIR="$ZDOTDIR"
fi
ZDOTDIR="$_croft_shim_zdotdir"
"#;
const ZSH_SHIM_ZPROFILE: &str = r#"# croft shell integration (auto-generated; do not edit)
_croft_user="${CROFT_USER_ZDOTDIR:-$HOME}"
[[ "$_croft_user" != "$ZDOTDIR" && -f "$_croft_user/.zprofile" ]] && ZDOTDIR="$_croft_user" builtin source "$_croft_user/.zprofile"
"#;
const ZSH_SHIM_ZLOGIN: &str = r#"# croft shell integration (auto-generated; do not edit)
_croft_user="${CROFT_USER_ZDOTDIR:-$HOME}"
[[ "$_croft_user" != "$ZDOTDIR" && -f "$_croft_user/.zlogin" ]] && ZDOTDIR="$_croft_user" builtin source "$_croft_user/.zlogin"
"#;
const ZSH_SHIM_ZSHRC: &str = r#"# croft shell integration (auto-generated; do not edit)
_croft_user="${CROFT_USER_ZDOTDIR:-$HOME}"
[[ "$_croft_user" != "$ZDOTDIR" && -f "$_croft_user/.zshrc" ]] && ZDOTDIR="$_croft_user" builtin source "$_croft_user/.zshrc"
# FinalTerm / OSC 133 semantic prompt marks + OSC 7 cwd, the protocol VS
# Code, Ghostty, iTerm2 and WezTerm build command navigation on.
if [[ -o interactive ]] && (( ! ${+_croft_si_installed} )); then
typeset -g _croft_si_installed=1
typeset -g _croft_b_mark=$'%{\e]133;B\a%}'
builtin autoload -Uz add-zsh-hook
_croft_preexec() {
typeset -g _croft_cmd_inflight=1
builtin printf '\033]133;C\007'
}
_croft_precmd() {
local _croft_ec=$?
if (( ${+_croft_cmd_inflight} )); then
builtin unset _croft_cmd_inflight
builtin printf '\033]133;D;%d\007' "$_croft_ec"
fi
# `%` percent-escaped: the sniffer percent-decodes every OSC 7 path,
# so a raw `%41` in a real directory name would decode to `A`.
builtin printf '\033]7;file://%s%s\007' "${HOST:-localhost}" "${PWD//\%/%25}"
builtin printf '\033]133;A\007'
# Mark where the typed command starts (133;B). Themes rebuild PS1 from
# their own precmd (which ran before this one), so the zero-width
# suffix is re-asserted at every prompt.
if [[ "$PS1" != *"$_croft_b_mark" ]]; then
PS1+="$_croft_b_mark"
fi
}
add-zsh-hook preexec _croft_preexec
add-zsh-hook precmd _croft_precmd
fi
# Restore the user's ZDOTDIR so nested shells and tools see their own.
if [[ -n "$CROFT_USER_ZDOTDIR" && "$CROFT_USER_ZDOTDIR" != "$HOME" ]]; then
ZDOTDIR="$CROFT_USER_ZDOTDIR"
else
builtin unset ZDOTDIR
fi
"#;
pub fn resolve_user_zdotdir(
inherited: Option<&std::path::Path>,
shim_root: &std::path::Path,
home: &std::path::Path,
) -> PathBuf {
match inherited {
Some(z) if !z.starts_with(shim_root) => z.to_path_buf(),
_ => home.to_path_buf(),
}
}
const BASH_SHIM: &str = r#"# croft shell integration bootstrap (auto-generated; do not edit)
[[ "$-" != *i* ]] && builtin return
if [[ -n "$CROFT_BASH_INJECT" ]]; then
builtin unset CROFT_BASH_INJECT
builtin set +o posix
builtin shopt -u inherit_errexit 2>/dev/null
# Restore the user's own $ENV (rarely set for bash) or drop croft's.
if [[ -n "$CROFT_BASH_ENV" ]]; then
builtin export ENV="$CROFT_BASH_ENV"
builtin unset CROFT_BASH_ENV
else
builtin unset ENV
fi
# Posix mode defaulted HISTFILE to ~/.sh_history; croft pre-seeded the
# bash default and flagged it so children do not inherit the export.
if [[ -n "$CROFT_BASH_UNEXPORT_HISTFILE" ]]; then
builtin export -n HISTFILE
builtin unset CROFT_BASH_UNEXPORT_HISTFILE
fi
# Replay the startup files posix mode skipped, exactly as bash itself
# would have chosen them.
if builtin shopt -q login_shell; then
[[ -r /etc/profile ]] && builtin source /etc/profile
for _croft_rc in "$HOME/.bash_profile" "$HOME/.bash_login" "$HOME/.profile"; do
if [[ -r "$_croft_rc" ]]; then
builtin source "$_croft_rc"
break
fi
done
else
for _croft_rc in /etc/bash.bashrc /etc/bash/bashrc /etc/bashrc; do
if [[ -r "$_croft_rc" ]]; then
builtin source "$_croft_rc"
break
fi
done
[[ -r "$HOME/.bashrc" ]] && builtin source "$HOME/.bashrc"
fi
builtin unset _croft_rc
fi
# FinalTerm / OSC 133 semantic prompt marks + OSC 7 cwd, the protocol VS
# Code, Ghostty, iTerm2 and WezTerm build command navigation on.
if [[ -z "$_croft_si_installed" ]]; then
_croft_si_installed=1
_croft_b_mark='\[\e]133;B\a\]'
_croft_preexec() {
_croft_cmd_inflight=1
builtin printf '\033]133;C\007'
}
_croft_precmd() {
local _croft_ec=$?
if [[ -n "$_croft_cmd_inflight" ]]; then
builtin unset _croft_cmd_inflight
builtin printf '\033]133;D;%d\007' "$_croft_ec"
fi
# `%` percent-escaped: the sniffer percent-decodes every OSC 7 path.
builtin printf '\033]7;file://%s%s\007' "${HOSTNAME:-localhost}" "${PWD//%/%25}"
builtin printf '\033]133;A\007'
# Prompt themes rewrite PS1 from their own PROMPT_COMMAND entries, so
# the zero-width input-start mark is re-asserted at every prompt.
if [[ "$PS1" != *"$_croft_b_mark" ]]; then
PS1+="$_croft_b_mark"
fi
}
# DEBUG fires for every simple command, including PROMPT_COMMAND's own
# entries; only the first one after the prompt was armed — and not part
# of PROMPT_COMMAND itself (an empty Enter goes straight back to it) —
# is the user's command. bash-preexec's exact discipline.
_croft_debug_trap() {
[[ -n "$COMP_LINE" ]] && builtin return
[[ -z "$_croft_at_prompt" ]] && builtin return
_croft_at_prompt=
# `[*]` joins with the first char of IFS: scope it to `;` here or an
# ARRAY PROMPT_COMMAND joins with spaces and its own entries never
# match, fabricating a command on every empty Enter.
local IFS=';'
case ";${PROMPT_COMMAND[*]};" in
*";$BASH_COMMAND;"*) builtin return ;;
esac
_croft_preexec
}
_croft_arm() { _croft_at_prompt=1; }
if [[ -n "${bash_preexec_imported:-${__bp_imported:-}}" ]]; then
# bash-preexec (iTerm2 integration, Atuin, ...) owns the DEBUG trap;
# join its cooperative hook arrays instead of fighting over it.
precmd_functions+=(_croft_precmd)
preexec_functions+=(_croft_preexec)
else
if ((BASH_VERSINFO[0] > 5 || (BASH_VERSINFO[0] == 5 && BASH_VERSINFO[1] >= 3))); then
# bash 5.3 funsub runs in the current shell: a trap-free preexec.
PS0='${ _croft_preexec; }'"$PS0"
elif [[ -z "$(trap -p DEBUG)" ]]; then
builtin trap '_croft_debug_trap' DEBUG
fi
# An existing DEBUG trap (a hand-rolled preexec) is left alone; marks
# degrade to prompt + cwd only.
# Precmd runs FIRST so nothing clobbers $? before the exit capture;
# the arm runs LAST so user entries never read as the next command.
if [[ "$(declare -p PROMPT_COMMAND 2>/dev/null)" == "declare -a"* ]]; then
PROMPT_COMMAND=(_croft_precmd "${PROMPT_COMMAND[@]}" _croft_arm)
else
PROMPT_COMMAND="_croft_precmd${PROMPT_COMMAND:+;$PROMPT_COMMAND};_croft_arm"
fi
fi
fi
"#;
pub fn ensure_bash_shim(config_dir: &std::path::Path) -> std::io::Result<PathBuf> {
let dir = config_dir.join("shell-integration").join("bash");
std::fs::create_dir_all(&dir)?;
let path = dir.join("croft.bash");
if std::fs::read_to_string(&path).ok().as_deref() != Some(BASH_SHIM) {
let mut f = std::fs::File::create(&path)?;
f.write_all(BASH_SHIM.as_bytes())?;
}
Ok(path)
}
const FISH_INTEGRATION: &str = r#"# croft shell integration (auto-generated; do not edit)
# First: put XDG_DATA_DIRS back so child processes never see croft's
# injection dir.
if set -q CROFT_FISH_XDG_DATA_DIR
if set -q XDG_DATA_DIRS
set --local _croft_i (contains --index -- "$CROFT_FISH_XDG_DATA_DIR" $XDG_DATA_DIRS)
and set --erase XDG_DATA_DIRS[$_croft_i]
if not set -q XDG_DATA_DIRS[1]
set --erase XDG_DATA_DIRS
else
set --global --export --path XDG_DATA_DIRS $XDG_DATA_DIRS
end
end
set --erase CROFT_FISH_XDG_DATA_DIR
end
if status is-interactive
# fish 4.0+ marks prompts natively (OSC 133 + OSC 7 for every
# terminal); emitting again would double-decorate. Old fish (<= 3.7)
# lacks the forward-char-passive binding and needs croft's hooks.
if not bind --function-names 2>/dev/null | string match -q forward-char-passive
function _croft_mark_prompt --on-event fish_prompt --on-event fish_cancel --on-event fish_posterror
if test "$_croft_prompt_state" = command
# the started command never reported back: close the cycle
printf '\e]133;D\a'
end
set --global _croft_prompt_state prompt
printf '\e]7;file://%s%s\a' "$hostname" (string escape --style=url -- "$PWD")
printf '\e]133;A\a'
_croft_wrap_prompt
end
# The end of the prompt (133;B) is where the typed command starts;
# without it croft cannot tell the command from the prompt, and
# records nothing (#1514). fish has no event for it, so croft wraps
# the user's fish_prompt. That is defined after this file runs (by
# config.fish or a prompt plugin, maybe redefined later), so the
# wrap happens on each fish_prompt event, which fires just before
# fish draws the prompt, whenever fish_prompt is not croft's wrapper.
function _croft_prompt_end
printf '\e]133;B\a'
end
function _croft_wrap_prompt
functions -q fish_prompt; or return
functions fish_prompt | string match -q '*_croft_prompt_end*'; and return
functions --erase _croft_user_prompt
functions --copy fish_prompt _croft_user_prompt
function fish_prompt
_croft_user_prompt
_croft_prompt_end
end
end
# Saving or editing fish_prompt (funcsave, `fish_config prompt
# save`, which calls funcsave, and funced) must act on the user's
# prompt, not croft's wrapper: a saved wrapper would call helpers
# that exist only under croft, leaving fish elsewhere without a
# prompt. Unwrap first; the next prompt event wraps again.
function _croft_unwrap_prompt
functions -q _croft_user_prompt; or return 0
functions fish_prompt 2>/dev/null | string match -q '*_croft_prompt_end*'; or return 0
functions --erase fish_prompt
functions --copy _croft_user_prompt fish_prompt
end
if functions -q funcsave; and not functions -q _croft_funcsave
functions --copy funcsave _croft_funcsave
function funcsave
_croft_unwrap_prompt
_croft_funcsave $argv
end
end
if functions -q funced; and not functions -q _croft_funced
functions --copy funced _croft_funced
function funced
_croft_unwrap_prompt
_croft_funced $argv
end
end
function _croft_mark_output_start --on-event fish_preexec
set --global _croft_prompt_state command
printf '\e]133;C\a'
end
function _croft_mark_output_end --on-event fish_postexec
set --local _croft_s $status
set --global _croft_prompt_state finished
printf '\e]133;D;%s\a' $_croft_s
end
end
end
"#;
pub fn ensure_fish_integration(config_dir: &std::path::Path) -> std::io::Result<PathBuf> {
let dir = config_dir.join("shell-integration").join("fish");
let conf_dir = dir.join("fish").join("vendor_conf.d");
std::fs::create_dir_all(&conf_dir)?;
let path = conf_dir.join("croft.fish");
if std::fs::read_to_string(&path).ok().as_deref() != Some(FISH_INTEGRATION) {
let mut f = std::fs::File::create(&path)?;
f.write_all(FISH_INTEGRATION.as_bytes())?;
}
Ok(dir)
}
pub fn fish_xdg_data_dirs(data_dir: &std::path::Path, inherited: Option<&str>) -> String {
const XDG_DEFAULT: &str = "/usr/local/share:/usr/share";
let rest = match inherited {
Some(x) if !x.is_empty() => x,
_ => XDG_DEFAULT,
};
format!("{}:{rest}", data_dir.display())
}
pub fn ensure_zsh_shim(config_dir: &std::path::Path) -> std::io::Result<PathBuf> {
let dir = config_dir.join("shell-integration").join("zsh");
std::fs::create_dir_all(&dir)?;
for (name, content) in [
(".zshenv", ZSH_SHIM_ZSHENV),
(".zprofile", ZSH_SHIM_ZPROFILE),
(".zshrc", ZSH_SHIM_ZSHRC),
(".zlogin", ZSH_SHIM_ZLOGIN),
] {
let path = dir.join(name);
if std::fs::read_to_string(&path).ok().as_deref() != Some(content) {
let mut f = std::fs::File::create(&path)?;
f.write_all(content.as_bytes())?;
}
}
Ok(dir)
}
#[cfg(test)]
mod tests {
use super::*;
fn scan_all(sniffer: &mut OscSniffer, chunks: &[&[u8]]) -> Vec<OscEvent> {
let mut out = Vec::new();
for c in chunks {
out.extend(sniffer.scan(c).into_iter().map(|(_, e)| e));
}
out
}
#[test]
fn sniffs_the_four_osc133_marks_with_both_terminators() {
let mut s = OscSniffer::default();
let events = scan_all(
&mut s,
&[b"\x1b]133;A\x07out\x1b]133;B\x1b\\\x1b]133;C\x07\x1b]133;D;1\x07"],
);
assert_eq!(
events,
vec![
OscEvent::PromptStart,
OscEvent::PromptEnd,
OscEvent::CommandStart,
OscEvent::CommandEnd(Some(1)),
]
);
}
#[test]
fn inline_image_osc_1337_is_captured_across_chunks() {
use base64::Engine;
let payload: Vec<u8> = (0u32..600).map(|i| (i % 251) as u8).collect();
let b64 = base64::engine::general_purpose::STANDARD.encode(&payload);
let seq = format!("\x1b]1337;File=name=eC5wbmc=;size=600;inline=1:{b64}\x07");
let bytes = seq.as_bytes();
let mut s = OscSniffer::default();
let events = scan_all(&mut s, &[&bytes[..20], &bytes[20..300], &bytes[300..]]);
assert_eq!(events, vec![OscEvent::InlineImage(payload.clone())]);
let seq = format!("\x1b]1337;File=name=eC5wbmc=:{b64}\x07");
assert!(scan_all(&mut s, &[seq.as_bytes()]).is_empty());
assert_eq!(
scan_all(&mut s, &[b"\x1b]133;A\x07"]),
vec![OscEvent::PromptStart]
);
}
#[test]
fn runaway_inline_image_payloads_are_abandoned() {
let mut s = OscSniffer::default();
assert!(s.scan(b"\x1b]1337;File=inline=1:").is_empty());
let filler = vec![b'A'; IMAGE_CARRY_MAX + 4096];
for chunk in filler.chunks(65536) {
assert!(s.scan(chunk).is_empty());
}
assert!(
s.scan(b"\x07").is_empty(),
"the oversized image never fires"
);
assert_eq!(
scan_all(&mut s, &[b"\x1b]133;C\x07"]),
vec![OscEvent::CommandStart]
);
}
#[test]
fn command_end_without_exit_code_yields_none() {
let mut s = OscSniffer::default();
let events = scan_all(&mut s, &[b"\x1b]133;D\x07"]);
assert_eq!(events, vec![OscEvent::CommandEnd(None)]);
}
#[test]
fn end_offsets_point_just_past_each_terminator() {
let mut s = OscSniffer::default();
let chunk = b"ab\x1b]133;A\x07cd";
let got = s.scan(chunk);
assert_eq!(got, vec![(10, OscEvent::PromptStart)]);
assert_eq!(&chunk[10..], b"cd");
}
#[test]
fn sequences_split_across_chunks_are_reassembled() {
let mut s = OscSniffer::default();
assert!(s.scan(b"foo\x1b]13").is_empty());
assert!(s.scan(b"3;D;12").is_empty());
let got = s.scan(b"7\x07bar");
assert_eq!(got, vec![(2, OscEvent::CommandEnd(Some(127)))]);
}
#[test]
fn a_lone_trailing_escape_is_carried_not_dropped() {
let mut s = OscSniffer::default();
assert!(s.scan(b"output\x1b").is_empty());
let got = s.scan(b"]133;A\x07");
assert_eq!(got, vec![(7, OscEvent::PromptStart)]);
}
#[test]
fn osc7_cwd_is_decoded_from_the_file_url() {
let mut s = OscSniffer::default();
let events = scan_all(
&mut s,
&[b"\x1b]7;file://mac.local/Users/me/dir%20with%20space\x07"],
);
assert_eq!(
events,
vec![OscEvent::Cwd(
PathBuf::from("/Users/me/dir with space"),
Some(String::from("mac.local"))
)]
);
}
#[test]
fn osc7_carries_the_reporting_host() {
let mut s = OscSniffer::default();
let events = scan_all(&mut s, &[b"\x1b]7;file://prod-db-1/var/lib\x07"]);
assert_eq!(
events,
vec![OscEvent::Cwd(
PathBuf::from("/var/lib"),
Some(String::from("prod-db-1"))
)]
);
let events = scan_all(&mut s, &[b"\x1b]7;file:///tmp\x07\x1b]7;/opt\x07"]);
assert_eq!(
events,
vec![
OscEvent::Cwd(PathBuf::from("/tmp"), None),
OscEvent::Cwd(PathBuf::from("/opt"), None),
]
);
}
#[test]
fn osc9_notification_payload_is_extracted() {
let mut s = OscSniffer::default();
let events = scan_all(&mut s, &[b"\x1b]9;Build finished\x07"]);
assert_eq!(
events,
vec![OscEvent::Notify(String::from("Build finished"))]
);
}
#[test]
fn urxvt_osc_777_notify_carries_its_title_and_body() {
let mut s = OscSniffer::default();
assert_eq!(
scan_all(&mut s, &[b"\x1b]777;notify;Build;finished OK\x07"]),
vec![OscEvent::Notify(String::from("Build: finished OK"))]
);
assert_eq!(
scan_all(
&mut s,
&[b"\x1b]777;notify;Tests;3 passed; 1 failed\x1b\\\x1b]777;notify;Only title\x07"]
),
vec![
OscEvent::Notify(String::from("Tests: 3 passed; 1 failed")),
OscEvent::Notify(String::from("Only title")),
]
);
assert_eq!(
scan_all(&mut s, &[b"\x1b]77", b"7;notify;;just a body\x07"]),
vec![OscEvent::Notify(String::from("just a body"))]
);
}
#[test]
fn other_osc_777_commands_and_an_empty_notify_are_not_notifications() {
let mut s = OscSniffer::default();
let events = scan_all(
&mut s,
&[b"\x1b]777;preexec\x07\x1b]777;precmd\x07\x1b]777;notify;;\x07\x1b]777;notify\x07"],
);
assert!(events.is_empty(), "got: {events:?}");
assert_eq!(
scan_all(&mut s, &[b"\x1b]7;/srv\x07"]),
vec![OscEvent::Cwd(PathBuf::from("/srv"), None)]
);
}
#[test]
fn kitty_osc_99_notifications_are_read() {
let mut s = OscSniffer::default();
assert_eq!(
scan_all(&mut s, &[b"\x1b]99;;kitty-style note\x1b\\"]),
vec![OscEvent::Notify(String::from("kitty-style note"))]
);
assert_eq!(scan_all(&mut s, &[b"\x1b]99;i=7:d=0;Build\x1b\\"]), vec![]);
assert_eq!(
scan_all(&mut s, &[b"\x1b]99;i=7:p=body;finished OK\x1b\\"]),
vec![OscEvent::Notify(String::from("Build: finished OK"))]
);
assert_eq!(
scan_all(&mut s, &[b"\x1b]99;e=1:p=body;aGkgdGhlcmU=\x07"]),
vec![OscEvent::Notify(String::from("hi there"))]
);
assert_eq!(
scan_all(&mut s, &[b"\x1b]9;plain\x07"]),
vec![OscEvent::Notify(String::from("plain"))]
);
}
#[test]
fn kitty_base64_is_one_stream_across_chunks() {
let mut s = OscSniffer::default();
assert!(scan_all(&mut s, &[b"\x1b]99;i=1:d=0:e=1;aM\x07"]).is_empty());
assert!(scan_all(&mut s, &[b"\x1b]99;i=1:d=0:e=1;Op\x07"]).is_empty());
assert_eq!(
scan_all(&mut s, &[b"\x1b]99;i=1:e=1;bGxv\x07"]),
vec![OscEvent::Notify(String::from("h\u{e9}llo"))]
);
assert!(scan_all(&mut s, &[b"\x1b]99;i=2:d=0:e=1;aA==\x07"]).is_empty());
assert_eq!(
scan_all(&mut s, &[b"\x1b]99;i=2:e=1;aQ\x07"]),
vec![OscEvent::Notify(String::from("hi"))]
);
}
#[test]
fn kitty_full_size_payload_survives_a_split_read() {
use base64::Engine;
let mut s = OscSniffer::default();
let text = "n".repeat(3072);
let b64 = base64::engine::general_purpose::STANDARD.encode(&text);
assert_eq!(b64.len(), 4096);
let seq = format!("\x1b]99;i=long:p=title:e=1;{b64}");
assert_eq!(
scan_all(&mut s, &[seq.as_bytes(), b"\x1b\\"]),
vec![OscEvent::Notify(text)]
);
}
#[test]
fn kitty_chunks_croft_does_not_show_send_nothing() {
let mut s = OscSniffer::default();
let events = scan_all(
&mut s,
&[b"\x1b]99;i=1:p=icon;abc\x07\x1b]99;i=1:p=?;\x07\x1b]99;i=1:p=close;\x07\x1b]99;;\x07\x1b]99;e=1;@@@\x07\x1b]99;no-semicolon\x07"],
);
assert!(events.is_empty(), "got: {events:?}");
assert!(scan_all(&mut s, &[b"\x1b]99;i=a:d=0;stale\x07"]).is_empty());
assert_eq!(
scan_all(&mut s, &[b"\x1b]99;i=b;fresh\x07"]),
vec![OscEvent::Notify(String::from("fresh"))]
);
}
#[test]
fn osc_9_4_progress_is_parsed_and_plain_osc_9_stays_notify() {
let mut s = OscSniffer::default();
assert_eq!(
scan_all(&mut s, &[b"\x1b]9;4;1;46\x07"]),
vec![OscEvent::Progress(1, 46)]
);
assert_eq!(
scan_all(&mut s, &[b"\x1b]9;4", b";0\x07"]),
vec![OscEvent::Progress(0, 0)]
);
assert_eq!(
scan_all(
&mut s,
&[b"\x1b]9;4;2;63\x07\x1b]9;4;1;250\x07\x1b]9;4;x;5\x07"]
),
vec![OscEvent::Progress(2, 63), OscEvent::Progress(1, 100)]
);
assert_eq!(
scan_all(&mut s, &[b"\x1b]9;Build finished\x07"]),
vec![OscEvent::Notify(String::from("Build finished"))]
);
assert_eq!(
scan_all(&mut s, &[b"\x1b]9;4 done\x07"]),
vec![OscEvent::Notify(String::from("4 done"))]
);
}
#[test]
fn unrelated_osc_and_plain_output_are_ignored() {
let mut s = OscSniffer::default();
let events = scan_all(
&mut s,
&[b"\x1b]0;title\x07\x1b]52;c;Zm9v\x07\x1b[31mred\x1b[0m plain"],
);
assert!(events.is_empty(), "got: {events:?}");
}
#[test]
fn a_runaway_unterminated_sequence_is_abandoned() {
let mut s = OscSniffer::default();
assert!(s.scan(b"\x1b]133;D;").is_empty());
for _ in 0..10 {
assert!(s.scan(&[b'x'; 1024]).is_empty());
}
let got = s.scan(b"\x1b]133;A\x07");
assert_eq!(got, vec![(8, OscEvent::PromptStart)]);
}
#[test]
fn resolve_user_zdotdir_rejects_croft_shim_paths() {
let shim_root = std::path::Path::new("/cfg/croft/shell-integration");
let home = std::path::Path::new("/Users/me");
assert_eq!(
resolve_user_zdotdir(Some(&shim_root.join("zsh")), shim_root, home),
home
);
let custom = std::path::Path::new("/Users/me/.config/zsh");
assert_eq!(resolve_user_zdotdir(Some(custom), shim_root, home), custom);
assert_eq!(resolve_user_zdotdir(None, shim_root, home), home);
}
#[test]
fn zsh_shim_writes_all_four_dotfiles_sourcing_the_user_rc() {
let tmp = tempfile::tempdir().unwrap();
let dir = ensure_zsh_shim(tmp.path()).expect("shim must be written");
for f in [".zshenv", ".zprofile", ".zshrc", ".zlogin"] {
let p = dir.join(f);
assert!(p.is_file(), "{f} must exist");
}
let rc = std::fs::read_to_string(dir.join(".zshrc")).unwrap();
assert!(rc.contains("133;A"), "precmd must emit the prompt mark");
assert!(rc.contains("133;C"), "preexec must emit the command mark");
assert!(rc.contains("133;D"), "precmd must emit the finished mark");
assert!(
rc.contains("]7;file://"),
"precmd must report cwd via OSC 7"
);
assert!(
rc.contains("CROFT_USER_ZDOTDIR"),
"the shim must source the user's own rc files"
);
ensure_zsh_shim(tmp.path()).expect("shim rewrite must succeed");
}
#[test]
fn fish_native_mark_dialect_is_parsed() {
let mut s = OscSniffer::default();
let events = scan_all(
&mut s,
&[b"\x1b]133;A;click_events=1\x1b\\> \x1b]133;B\x1b\\cmd\x1b]133;C;cmdline_url=cmd%20--flag\x1b\\out\x1b]133;D;2\x1b\\"],
);
assert_eq!(
events,
vec![
OscEvent::PromptStart,
OscEvent::PromptEnd,
OscEvent::CommandStart,
OscEvent::CommandEnd(Some(2)),
]
);
let bare = scan_all(&mut s, &[b"\x1b]133;D\x1b\\"]);
assert_eq!(bare, vec![OscEvent::CommandEnd(None)]);
}
#[test]
fn bash_shim_backs_out_of_posix_mode_and_installs_hooks() {
let tmp = tempfile::tempdir().unwrap();
let file = ensure_bash_shim(tmp.path()).expect("shim must be written");
assert!(file.is_file(), "croft.bash must exist");
let s = std::fs::read_to_string(&file).unwrap();
for needle in [
"set +o posix",
"login_shell",
".bash_profile",
".bashrc",
"PROMPT_COMMAND",
"133;A",
"133;B",
"133;C",
"133;D",
"]7;file://",
"HISTFILE",
] {
assert!(s.contains(needle), "bash shim must contain {needle:?}");
}
ensure_bash_shim(tmp.path()).expect("shim rewrite must succeed");
}
#[test]
fn fish_integration_writes_a_vendor_conf_that_defers_to_native_marks() {
let tmp = tempfile::tempdir().unwrap();
let data_dir = ensure_fish_integration(tmp.path()).expect("must be written");
let f = data_dir
.join("fish")
.join("vendor_conf.d")
.join("croft.fish");
assert!(
f.is_file(),
"croft.fish must exist under fish/vendor_conf.d"
);
let s = std::fs::read_to_string(&f).unwrap();
for needle in [
"CROFT_FISH_XDG_DATA_DIR",
"forward-char-passive",
"fish_preexec",
"fish_postexec",
"functions --copy fish_prompt",
"133;A",
"133;B",
"133;C",
"133;D",
"]7;file://",
] {
assert!(
s.contains(needle),
"fish integration must contain {needle:?}"
);
}
ensure_fish_integration(tmp.path()).expect("rewrite must succeed");
}
#[test]
fn fish_xdg_data_dirs_prepends_and_keeps_the_spec_default() {
let d = std::path::Path::new("/cfg/shell-integration/fish");
assert_eq!(
fish_xdg_data_dirs(d, Some("/a:/b")),
"/cfg/shell-integration/fish:/a:/b"
);
for inherited in [None, Some("")] {
assert_eq!(
fish_xdg_data_dirs(d, inherited),
"/cfg/shell-integration/fish:/usr/local/share:/usr/share"
);
}
}
}