lkjdsalkjdsalkjdsalk

This commit is contained in:
2025-03-16 23:42:43 +01:00
parent c7ae43d81a
commit 47a02be2cd

View File

@@ -1,65 +1,105 @@
zeditor-remote() {
if printf '%s\n' "$(gum --version)" "gum version 0.15.1" | sort -C -V; then
local maybe_spin="" # doesn't forward stdin
else
local maybe_spin="gum spin --show-output --show-error --"
fi
# echo maybe_spin="$maybe_spin"
local host=$(
{
echo localhost
tailscale status --json | jq .Peer[].HostName -r | grep -v '^localhost$' | sort -u
} | fzf --multi --reverse --bind 'ctrl-a:toggle-all' --height=25 --cycle \
| sed -e 's/%/%%/g'
)
if [[ -z "$host" ]]; then
return
fi
local repos=$(
worker() {
local host="$1"
fdargs=(repos/ --hidden --max-depth 5)
sshargs=(-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null)
if [[ "$host" = localhost ]]; then
cd "$HOME"
fd "^\.git$" "${fdargs[@]}" -x printf '~/%q\n' "{//}"
else
timeout 5 ssh "${sshargs[@]}" 2>/dev/null "$host" \
fd "^\.git$" "${fdargs[@]}" -x printf "'ssh://$host/~/%q\n'" "'{//}'"
fi
}
$maybe_spin xe <<<"$host" -j"$(wc -l <<<"$host")" -s "$(declare -f worker); worker \"\$@\"" 2>/dev/null ||:
)
if [[ -z "${repos}" ]]; then
echo >&2 "no repos found!"
ensure() {
local error
for name in "$@"; do
if ! command -v "${name}" >/dev/null; then
printf >&2 '%s\n' "ERROR: '$name' not found in PATH"
error=1
fi
done
if [[ -n "$error" ]]; then
return 1
fi
}
ensure xe fd fzf gum || return 1
local choice=$(sort <<<"$repos" | fzf --reverse --height=25)
if [[ -z "${choice}" ]]; then
return
fi
local zed_cmd="zeditor ${choice}"
local ssh_cd_cmd=$(
rg <<<"$choice" '^ssh://([^/]*)/(~/.*)$' -r 'ssh -t "$1" "cd $2; $$SHELL -l"' ||:
)
[[ -z "$zed_cmd" ]] || echo "$zed_cmd"
[[ -z "$ssh_cd_cmd" ]] || echo "$ssh_cd_cmd"
if [[ -n "$ssh_cd_cmd" ]] && gum confirm "SSH in there?" --default=yes; then
# history -s "${zed_cmd}"
eval ${zed_cmd}
# TODO: find a way to not push the rest of the unpushed history
history -s "${ssh_cd_cmd}"
history -a
eval ${ssh_cd_cmd}
local spin=""
# doesn't forward stdin
if command -v gum >/dev/null; then
if printf '%s\n' "$(gum --version)" "gum version 0.15.1" | sort -C -V; then
: # older version don't forward stdin
else
# history -s "${zed_cmd}"
eval ${zed_cmd}
cd "${choice}"
spin="gum spin --show-output --show-error --"
fi
fi
local host=$(
{
printf '%s\n' localhost
{
# ask tailscale
if command -v tailscale >/dev/null; then
tailscale status --json | jq .Peer[].HostName -r;
fi
# search .ssh/known_hosts, strip DNS search domains to deduplicate
if [[ -r /etc/resolv.conf && -r "$HOME"/.ssh/known_hosts ]]; then
local -a hosts
readarray -d $'\n' -t hosts < <(
cut <"$HOME"/.ssh/known_hosts -d' ' -f1 | sort -u \
| grep -E "(\\.$(hostname --domain)|^[^.]*)$"
)
local -a domains
readarray -d ' ' -t domains < <(
grep </etc/resolv.conf '^search ' | cut -d' ' -f2-
)
printf '%s\n' "${hosts[@]}" | sed -E "s/\\.($(IFS='|'; printf "%s" "${domains[*]}"))\$//g"
fi
} | grep -v '^localhost$' | sort -u
} | fzf --multi --reverse --bind 'ctrl-a:toggle-all' --height=25 --cycle \
| sed -e 's/%/%%/g'
)
if [[ -z "$host" ]]; then
return 1
fi
local repos=$(
worker() {
local host="$1"
# TODO: this assumes remote hosts only code in 'repos/'
# TODO: this assumes remote hosts has 'fd', use 'find' instead?
fdargs=(--hidden --max-depth 5)
# accept all, persist nothing, to scan hosts we've yet to trust,
# zed will still complain however
sshargs=(-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null)
if [[ "$host" = localhost ]]; then
cd "$HOME"
fd "^\.git$" repos/ "${fdargs[@]}" -x printf '~/%q\n' "{//}"
else
# TODO: report timeouts?
timeout 5 ssh "${sshargs[@]}" 2>/dev/null "$host" \
fd "^\.git$" repos/ "${fdargs[@]}" -x printf "'ssh://%s/~/%q\n'" "'$host'" "'{//}'"
fi
}
$spin xe <<<"$host" -j"$(wc -l <<<"$host")" -s "$(declare -f worker); worker \"\$@\"" 2>/dev/null ||:
)
if [[ -z "${repos}" ]]; then
printf >&2 '%s\n' "ERROR: no repos found!"
return 1
fi
local choice=$(sort <<<"$repos" | fzf --reverse --cycle --height=25)
if [[ -z "${choice}" ]]; then
return
fi
local zed_cmd="zeditor ${choice}"
local ssh_cd_cmd=$(
rg <<<"$choice" '^ssh://([^/]*)/(~/.*)$' -r 'ssh -t "$1" "cd $2; $$SHELL -l"' ||:
)
if [[ -n "$ssh_cd_cmd" ]] && gum confirm "SSH in there now?" --default=yes; then
# history -s "${zed_cmd}"
eval ${zed_cmd}
# TODO: find a way to not push the rest of the unpushed history
history -s "${ssh_cd_cmd}"
history -a
printf >&2 "%s\n" "+ ${ssh_cd_cmd}"
eval ${ssh_cd_cmd}
else
# history -s "${zed_cmd}"
eval ${zed_cmd}
printf >&2 "%s\n" "+$(printf " %q" cd "${choice}")"
eval cd "${choice}" # 'eval' to expand the tilde
fi
}