dotfiles/i3/terminal.sh

37 lines
868 B
Bash
Raw Normal View History

2018-04-03 15:55:41 +02:00
#!/bin/bash
2022-06-11 20:00:32 +02:00
set -uo pipefail
# A failure in this script should not prevent the terminal from opening
set +e
#CMD=i3-sensible-terminal
CMD=alacritty
#CMD=xfce4-terminal
2018-04-30 17:19:00 +02:00
# If a window with a path in its title is focused, open a terminal with that
# location as working directory. Like this, terminals are opened in the same
2022-06-11 20:00:32 +02:00
# directory as the currently focused one.
sway_focused_window() {
swaymsg -t get_tree | jq -r '.. | select(.focused?) | .name'
}
title="$(sway_focused_window || xtitle)"
path=""
2018-04-03 15:55:41 +02:00
2022-06-11 20:00:32 +02:00
if [[ $title =~ (in|^.*@$(hostname):)(~?)(/.*) ]]; then
2018-04-03 15:55:41 +02:00
path="${BASH_REMATCH[3]}"
[[ ${BASH_REMATCH[2]} == "~" ]] && path="$HOME$path"
while [[ $path != / && -n $path && ! -d $path ]]; do
path="$(dirname "$path")"
done
fi
# Open terminal at specified path
2022-06-11 20:00:32 +02:00
if [[ -n $path ]] && [[ -r $path ]]; then
exec env -C "$path" $CMD "$@"
2018-04-03 15:55:41 +02:00
else
2022-06-11 20:00:32 +02:00
exec $CMD "$@"
2018-04-03 15:55:41 +02:00
fi