From 47a02be2cd4c19bd4fa0dc2cf07d81c9744edaf2 Mon Sep 17 00:00:00 2001 From: Peder Bergebakken Sundt Date: Sun, 16 Mar 2025 23:42:43 +0100 Subject: [PATCH] lkjdsalkjdsalkjdsalk --- .../home/profiles/bashrc.d/zeditor-remote.sh | 156 +++++++++++------- 1 file changed, 98 insertions(+), 58 deletions(-) diff --git a/users/pbsds/home/profiles/bashrc.d/zeditor-remote.sh b/users/pbsds/home/profiles/bashrc.d/zeditor-remote.sh index 2ae5025..3fc0548 100644 --- a/users/pbsds/home/profiles/bashrc.d/zeditor-remote.sh +++ b/users/pbsds/home/profiles/bashrc.d/zeditor-remote.sh @@ -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 /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 }