dotfiles/.local/bin/default-application-launcher

81 lines
3.2 KiB
Bash
Executable File

#!/usr/bin/env zsh
exec </dev/null
exec > >(systemd-cat -t i3-sensible-launcher -p info)
exec 2> >(systemd-cat -t i3-sensible-launcher -p err)
pyenv shell system
current_workspace=$(swaymsg -t get_workspaces | jq '.[] | select(.focused==true).name' -r)
ws_data=$(jq <$HOME/.config/sway/workspaces.json ".contexts | [.[] | values[]] | flatten(1) | .[] | select(.index==${current_workspace})" -c)
program=$(echo $ws_data | jq -r '.exec')
program_name=$(echo $ws_data | jq -r '.program_name')
memory_profile=$(echo $ws_data | jq '.memory_profile' -c)
run_with_systemd=$(echo $ws_data | jq '.systemd' -r)
void_output=$(echo $ws_data | jq '.void_output' -r)
IFS=$'\n' environ=($(echo $ws_data | jq '.environ | to_entries | map("\(.key)=\(.value|tostring)") | .[]' -r 2>/dev/null))
IFS=$'\n' args=($(echo $ws_data | jq '.args | .[]' -r 2>/dev/null))
systemd_run_args=()
if [[ $memory_profile != "null" ]]; then
max_memory=$(echo $memory_profile | jq -r '.max')
high_memory=$(echo $memory_profile | jq -r '.high')
systemd_run_args+=("--property=MemoryAccounting=yes" "--property=MemoryMax=${max_memory}" "--property=MemoryHigh=${high_memory}")
fi
pty=alacritty
function error() {
gxmessage -buttons "OK:0" -name "Program Launch Error" -title "Program Launch Error" "Error: This workspace is not assigned a default program" -borderless -wrap -center -sticky -default OK
}
if [[ $program == "" ]]; then
echo "error: no program for workspace ${current_workspace} is defined (got ${program})" >&2
exec error
fi
echo "Launching application $program"
if [[ $environ != "" ]]; then
for env in $environ; do
echo "setting environment $env"
systemd_run_args+=("--setenv=$env")
done
fi
if [[ $program == "console" ]] && [[ $pty == "alacritty" ]]; then
# Create a new window if an instance is already running, otherwise start the service
echo "Creating new alacritty window"
$pty msg create-window || /usr/bin/systemctl --user start alacritty.service
exit 0
elif [[ $program == "console" ]]; then
program=$pty
fi
if [[ $void_output == "true" ]]; then
# Run it under a scope for resource control, but don't capture stdout/stderr
# This should only be used for programs that either produce no output, or
# produce so much output that it would flood the journal and make it
# difficult to use.
exec >/dev/null
exec 2>/dev/null
exec systemd-run --user $=systemd_run_args --scope --unit="$program_name-$$" --description="$program_name" --slice=gui.slice -- $=program
fi
if [[ $run_with_systemd == "false" ]]; then
# Run it under systemd-cat to capture stdout/stderr to journal, but don't use systemd-run
# so that we don't get resource control
exec systemd-cat -t $program_name -p info -- $=program
fi
# Run programs using systemd-run to allow for resource control and capture of stdout/stderr
# We're using a service instead of a scope because scopes don't capture output, and wrapping
# the call in systemd-cat is more complicated than just using a service.
echo exec systemd-run --user $=systemd_run_args --unit="$program_name-$$" --description="$program_name" --slice=gui.slice -- $program $args
exec systemd-run --user $=systemd_run_args --unit="$program_name-$$" --description="$program_name" --slice=gui.slice -- $program $args