Last active
September 8, 2024 04:09
-
-
Save andelink/00af4840d16038371d2e6ce78c704c66 to your computer and use it in GitHub Desktop.
Zed "Filter Through Command" - Context: https://github.com/zed-industries/zed/issues/12598
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
[ | |
{ | |
"label": "filter-through-command", | |
"command": "~/bin/zed-filter-through-command", | |
"reveal": "always", | |
"hide": "on_success", | |
"shell": { "program": "sh" } | |
}, | |
{ | |
"label": "filter-through-jq", | |
"command": "~/bin/zed-filter-through-command", | |
"args": ["jq", "-S"], // same as above but defaulting to jq -S | |
"reveal": "always", | |
"hide": "on_success", | |
"shell": { "program": "sh" } | |
} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env zsh -f | |
setopt errexit | |
setopt pipefail | |
# Context: https://github.com/zed-industries/zed/issues/12598 | |
if [[ ! $(command -v fzf) ]] { | |
echo "\e[31;1mfzf not found\e[m: brew install fzf" >&2 | |
exit 1 | |
} | |
# Export for FZF child processes | |
export OUTFILE=${ZED_FILE} | |
if [[ -n ${ZED_SELECTED_TEXT} ]] { | |
OUTFILE="$(mktemp -p /tmp "${${(%):-%N}:t:r}.XXXXX")" | |
} | |
HEADER="${ZED_FILE}${ZED_SELECTED_TEXT:+:${ZED_ROW}:${ZED_COLUMN}}" | |
BORDER_LABEL=' ENTER write output / CTRL-Y copy output / CTRL-T copy command / CTRL-Q clear command / ESC or CTRL-C abort ' | |
PREVIEW_LABEL=' Preview ([alt] up/down, page up/down) ' | |
FZFCMD='{ [[ -n \${ZED_SELECTED_TEXT} ]] && <<<\${ZED_SELECTED_TEXT} || <<<\$(<${ZED_FILE}) } | ${(@)${=${(Q):-{q}}}}' | |
{ [[ -n ${ZED_SELECTED_TEXT} ]] && <<<"${ZED_SELECTED_TEXT}" || <<<$(<${ZED_FILE}) } | fzf \ | |
--wrap \ | |
--read0 \ | |
--no-info \ | |
--no-bold \ | |
--no-mouse \ | |
--disabled \ | |
--reverse \ | |
--no-separator \ | |
--header-first \ | |
--pointer '' \ | |
--border bottom \ | |
--border-label-pos 0:bottom \ | |
--preview-window right,60%,wrap \ | |
--query "${*:-cat}" \ | |
--header "${HEADER}" \ | |
--border-label "${BORDER_LABEL}" \ | |
--preview-label "${PREVIEW_LABEL}" \ | |
--preview "eval \"${FZFCMD}\"" \ | |
--bind "ctrl-t:execute-silent:pbcopy <<< {q}" \ | |
--bind "ctrl-y:execute-silent:eval \"${FZFCMD} | pbcopy\"" \ | |
--bind "enter:execute-silent:eval \"${FZFCMD} >${OUTFILE}\"" \ | |
--bind enter:+accept-or-print-query \ | |
--bind ctrl-q:clear-query \ | |
--bind up:preview-up \ | |
--bind down:preview-down \ | |
--bind scroll-up:preview-up \ | |
--bind scroll-down:preview-down \ | |
--bind page-up:preview-page-up \ | |
--bind page-down:preview-page-down \ | |
--bind alt-up:preview-top \ | |
--bind alt-down:preview-bottom \ | |
--color header:italic,label:italic,bg:-1,bg+:-1,fg:-1,fg+:-1,border:-1 \ | |
> /dev/null || RC=$? | |
# Code 130: FZF was aborted | |
# No selection: FZF wrote to the output file | |
[[ ${RC:-0} -eq 130 || -z ${ZED_SELECTED_TEXT} ]] && exit | |
# Assume if the user got to this point, they intended to write something | |
if [[ ! -s ${OUTFILE} ]] { | |
echo "\e[31;1mNo output to write\e[m" >&2 | |
exit 1 | |
} | |
# Overwrite selection with command output | |
PERLCMD=( | |
'BEGIN{$col=$ENV{ZED_COLUMN}-1; undef $/ if $ENV{ZED_ROW}==1}' | |
'undef $/ if $.==$ENV{ZED_ROW}-1;' | |
'$.==$ENV{ZED_ROW} and s|(?<=.{$col})\Q$ENV{ZED_SELECTED_TEXT}|$repl|s' | |
) | |
perl -spi \ | |
-e "${PERLCMD[*]}" \ | |
-- -repl="$(<${OUTFILE})" \ | |
${ZED_FILE} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment