djcev.com

//

Git Repos / dotfiles / bin / xvt.sh

Last update to this file was on 2024-01-02 at 05:12.

Show xvt.sh

#!/bin/sh
#
# Copyright (c) 2015-2021, Cameron Vanderzanden. Available under the
# terms of the 2-clause BSD license. Please see the file "LICENSE" in
# the root dir of this repository for more information.
#
#- xvt.sh 2015/01/14 cev, last updated 2021/12/07.
## Usage: xvt.sh [-hnV] [-d directory] [-t timeout] [-u urgency]
##
## Detect the CWD of the currently selected window and run a new
## terminal window in the same CWD. Requires herbstluftwm.
##
## -d Change to <directory> instead of checking focused win's CWD
## -h Show help
## -n Announce detected parameters via dbus notification
## -t Notification timeout (notify-send -t)
## -u Notification urgency (notify-send -u)
## -V Print version info
##
# This'll break if you run a shell within a shell. It (should) only detect
# the outside shell. Might be fixed by altering the logic to check PPID.

[ -x $HOME/.local/bin/cev_print.sh ] && . $HOME/.local/bin/cev_print.sh

self="${0##*'/'}"
cwd=""
notify=""
os="$(uname -s)"
terminal="/usr/local/bin/alacritty"
timeout=1000
urgency="low"

freebsd_cleanup() {
if [ -n "$envfile" -a -e "$envfile" ]; then rm $envfile; fi
if [ -n "$fdfile" -a -e "$fdfile" ]; then rm $fdfile; fi
}

while getopts "d:hnt:u:V?" option; do
case $option in
d) cwd="$OPTARG" ;;
h) print_help && exit ;;
n) notify="Y" ;;
t) timeout="$OPTARG" ;;
u) urgency="$OPTARG" ;;
V) print_version && exit ;;
"?") print_help && exit ;;
esac
shift $((OPTIND - 1))
done

# read the focused window ID. sh does the hex to dec conversion.
# requires that the window manager set _NET_ACTIVE_WINDOW
id=$(xprop -root | grep '_NET_ACTIVE_WINDOW(WINDOW):' | cut -d'#' -f 2-)
if [ -n "$id" ]; then
id=$(($id))
fi

if [ -n "$cwd" -o -z "$id" ]; then
pid="$$" && exe="$SHELL"
elif [ "$os" = "FreeBSD" ]; then
# store the env of every process whose env contains WINDOWID=$id,
# then look for the first process where COMM matches $SHELL.
trap freebsd_cleanup EXIT
envfile=$(mktemp ${TMPDIR:-/tmp}/${self}.1.XXXXXXXX)
procstat -aeh | grep "WINDOWID=$id" 2>/dev/null 1>$envfile
while read epid ecomm eenv; do
if [ "$(which $ecomm)" = "$SHELL" ]; then
# this is probably the shell running within the focused window.
# read its file descriptor info, pull out cwd, then break.
fdfile=$(mktemp ${TMPDIR:-/tmp}/${self}.2.XXXXXXXX)
procstat -fh $epid 2>/dev/null 1>$fdfile
while read fpid fcomm ffd ft fv fflags fref foffset fpro fname; do
if [ "$ffd" = "cwd" ]; then
pid="$epid" && exe="$ecomm" && cwd="$fname"
fi
done < $fdfile
rm $fdfile
break
fi
done < $envfile
rm $envfile
elif [ "$os" = "Linux" ]; then
# grab the PID of every process whose environment contains WINDOWID=id
pids=$(grep "WID=$id" /proc/[0-9]*/environ 2>/dev/null | tr -d '[:alpha:]/')
# iterate; assume the first process whose exe matches the user's shell
# is the currently active shell in the window, then read and store its
# current working directory.
for xpid in $pids; do
if [ "/proc/$xpid/exe" -ef "$SHELL" ]; then
# assume the program we pass cwd to will dereference the symlink
pid="$xpid" && exe="$SHELL" && cwd="/proc/$xpid/cwd"
break
fi
done
fi

# If we found a directory above, run your terminal in that dir.
if [ -z "$cwd" ]; then pid="$$" && exe="$SHELL" && cwd="$HOME"; fi
print_self
printf "%brefpid %s, exe %s, cwd %s%b\n" "$_norm" "$pid" "$exe" "$cwd" "$_clear"
if [ -n "$notify" ]; then
notify-send -t "$timeout" -u "$urgency" -a "$self" "$cwd"
fi
cd "$cwd"
exec $terminal $*

Return to the top of this page or return to the overview of this repo.

Log xvt.sh

Date Commit Message Author + -
2024-01-02 Change from FreeBSD to Linux, other minor changes cev +1 -1
2021-12-13 Expanded redshift background.sh, misc. changes cev +1 -1
2021-12-08 Initial commit. Dotfiles, scripts, basic Makefile. cev +105  

Return to the top of this page or return to the overview of this repo.