Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions app/assets/bundled/bootstrap/zsh_body.sh
Original file line number Diff line number Diff line change
Expand Up @@ -709,15 +709,31 @@ if [[ -z $WARP_BOOTSTRAPPED ]]; then
BUFFER=""
}
zle -N warp_report_input
function warp_select_fzf_history_entry () {
emulate -L zsh
setopt extendedglob pipefail

local selected=""
selected="$(fc -rl 1 \
| command -p awk '{ cmd=$0; sub(/^[ \t]*[0-9]+\**[ \t]+/, "", cmd); if (!seen[cmd]++) print $0 }' \
| fzf --nth=2.. --with-nth=2.. --scheme=history --tiebreak=index +m)"
local ret=$?
local -a mbegin mend match
REPLY=""
if [[ "$selected" == [[:blank:]]#(#b)(<->)(#B)( |\* )* ]] \
&& (( ${+history[${match[1]}]} )); then
REPLY="${history[${match[1]}]}"
fi
return $ret
}

# Runs the shell's own ctrl-r history widget as a foreground command.
function warp_run_external_ctrl_r_widget () {
local result=""
case "$_WARP_EXTERNAL_CTRL_R_WIDGET" in
fzf-history-widget)
result="$(fc -rl 1 \
| command -p awk '{ cmd=$0; sub(/^[ \t]*[0-9]+\**[ \t]+/, "", cmd); if (!seen[cmd]++) print cmd }' \
| fzf --scheme=history --tiebreak=index +m)"
warp_select_fzf_history_entry
result="$REPLY"
;;
atuin-search|atuin-search-viins|atuin-search-vicmd|_atuin_search_widget)
# atuin writes its TUI to stdout; under plain command substitution that's a pipe, and
Expand Down
72 changes: 72 additions & 0 deletions crates/warp_terminal/src/bootstrap_tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(unix)]
use command::blocking::Command;

use super::*;

struct TestAssetProvider;
Expand Down Expand Up @@ -59,6 +62,75 @@ fn test_trims_powershell_specifics() {
);
}

#[cfg(unix)]
#[test]
fn test_zsh_fzf_history_selection_restores_multiline_entry() {
assert_eq!(
run_zsh_fzf_history_selection(
"echo CORE3811_FIRST\necho CORE3811_SECOND",
r" 42 echo CORE3811_FIRST\necho CORE3811_SECOND",
),
b"echo CORE3811_FIRST\necho CORE3811_SECOND"
);
}

#[cfg(unix)]
#[test]
fn test_zsh_fzf_history_selection_preserves_literal_backslash_n() {
assert_eq!(
run_zsh_fzf_history_selection(r"printf '%s\n' hi", r" 42 printf '%s\n' hi",),
br"printf '%s\n' hi"
);
}

#[cfg(unix)]
fn run_zsh_fzf_history_selection(canonical_history: &str, listed_history: &str) -> Vec<u8> {
const ZSH_BODY: &str = include_str!("../../../app/assets/bundled/bootstrap/zsh_body.sh");
const FUNCTION_START: &str = " function warp_select_fzf_history_entry () {";
const FUNCTION_END: &str = "\n }\n\n # Runs the shell's own ctrl-r";

let function_start = ZSH_BODY
.find(FUNCTION_START)
.expect("fzf history selection function should exist");
let function_body = &ZSH_BODY[function_start..];
let function_end = function_body
.find(FUNCTION_END)
.expect("fzf history selection function should have a closing brace")
+ "\n }".len();
let function_body = &function_body[..function_end];
let script = format!(
r#"
{function_body}
function fc() {{
print -r -- "$LISTED_HISTORY"
}}
function fzf() {{
command cat
}}
function run_test() {{
local -A history
history[42]="$CANONICAL_HISTORY"
warp_select_fzf_history_entry
print -rn -- "$REPLY"
}}
run_test
"#
);

let output = Command::new("zsh")
.args(["-dfc", &script])
.env("CANONICAL_HISTORY", canonical_history)
.env("LISTED_HISTORY", listed_history)
.output()
.expect("zsh should run the fzf history selection fixture");
assert!(
output.status.success(),
"zsh fixture failed: {}",
String::from_utf8_lossy(&output.stderr)
);
output.stdout
}

fn decode_script(bytes: &[u8]) -> &str {
std::str::from_utf8(bytes).expect("should not fail to decode")
}
Loading