From 6c484ec186834026cd23f04e9c7e4ecc3920bbfb Mon Sep 17 00:00:00 2001 From: Brendan Falk Date: Tue, 27 Apr 2021 18:09:40 -0700 Subject: [PATCH] Update remote_cwd.sh I haven't tested this live. But I believe it should work fine! On the macOS end you will need to update the fact this this script can now return "error" and if so, we should abstain from sending ssh events to autocomplete as we don't know the working directory. We should also signal this in the debugger --- tools/remote_cwd.sh | 73 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 56 insertions(+), 17 deletions(-) diff --git a/tools/remote_cwd.sh b/tools/remote_cwd.sh index 60ca898..b1f1564 100644 --- a/tools/remote_cwd.sh +++ b/tools/remote_cwd.sh @@ -6,25 +6,64 @@ # Created by Matt Schrage on 1/7/21. # Copyright © 2021 Matt Schrage. All rights reserved. + +# Notes: +# This script either outputs "error" or it outputs the working directory of the user's remote shell +# The script has some Fig Settings passed in. These are +# * get_process_from_top -> if exists, get pid from the top of process list not bottom + # Do not delete the following comment: # FIG_SETTINGS + +# OLD WAY OF DOING IT # Heuristic for getting user's pid. Find all processes with a TTY and pick most recent. -pid=$(ps -e -o pid,tty,comm | grep -v "?" | grep 'zsh\|bash\|fish' | tail -n -1 | xargs | cut -d ' ' -f 1); - -# https://stackoverflow.com/a/8597411/926887 -if [[ "$OSTYPE" == "linux-gnu"* ]]; then - readlink /proc/$pid/cwd -elif [[ "$OSTYPE" == "darwin"* ]]; then - lsof -p $pid | grep cwd | awk '{print $9}' -elif [[ "$OSTYPE" == "cygwin" ]]; then - pwd # not gonna handle this right now -elif [[ "$OSTYPE" == "msys" ]]; then - pwd # not gonna handle this right now -elif [[ "$OSTYPE" == "win32" ]]; then - pwd # not gonna handle this right now -elif [[ "$OSTYPE" == "freebsd"* ]]; then - procstat -f $pid | grep cwd | awk '{print $10}' -else - pwd # Unknown. +# pid=$(ps -e -o pid,tty,comm | grep -v "?" | grep 'zsh\|bash\|fish' | tail -n -1 | xargs | cut -d ' ' -f 1); + + + +# Steps (comment out all the code blocks when ready) + +# 1. get the list of all processes +shell_processes=$(ps -e -o pid,tty,comm | grep -v "?" | grep 'zsh\|bash\|fish') + +# 2. Work out how many TTYs there are on the remote machine +# NOTE: Do NOT delete the double quotes around "$shell_processes". The passing is wrong if we do +total_ttys=$(echo "$shell_processes" | awk '{print $2}' | sort | uniq | wc -l | awk '{print $1}') + +# 3. Check how many TTYs are connected +# If more than 1, return some error code +if (( $total_ttys > 1 )); +then + echo "error" + +# Otherwise, get the pid of the user's shell depending on the env variables / fig settings fig passed in +else + # If the user is using something like powerlevel10k or the train tracks prompt + if [[ ! -z "$get_process_from_top" ]]; + then + pid=$(echo "$shell_processes" | head -n 1 | xargs | cut -d ' ' -f 1); + + # Otherwise get last process in list + else + pid=$(echo "$shell_processes" | tail -n -1 | xargs | cut -d ' ' -f 1); + fi + + + # https://stackoverflow.com/a/8597411/926887 + if [[ "$OSTYPE" == "linux-gnu"* ]]; then + readlink /proc/$pid/cwd + elif [[ "$OSTYPE" == "darwin"* ]]; then + lsof -p $pid | grep cwd | awk '{print $9}' + elif [[ "$OSTYPE" == "cygwin" ]]; then + pwd # not gonna handle this right now + elif [[ "$OSTYPE" == "msys" ]]; then + pwd # not gonna handle this right now + elif [[ "$OSTYPE" == "win32" ]]; then + pwd # not gonna handle this right now + elif [[ "$OSTYPE" == "freebsd"* ]]; then + procstat -f $pid | grep cwd | awk '{print $10}' + else + echo "error" # Unknown. + fi fi