Skip to content

Instantly share code, notes, and snippets.

@sebastiancarlos
Last active November 10, 2023 16:47
Show Gist options
  • Save sebastiancarlos/515eb7ec704968e691a721e67cfa65f8 to your computer and use it in GitHub Desktop.
Save sebastiancarlos/515eb7ec704968e691a721e67cfa65f8 to your computer and use it in GitHub Desktop.
sway-use-workspace-cwd
#!/usr/bin/env bash
# All my gist code is licensed under the MIT license.
# Add this to your PATH
# if calling without arguments or with -h or --help, print usage
if [[ $# -eq 0 ]] || [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then
echo "Usage: get-terminal-cwd <terminal-pid>"
echo " - return the CWD of the terminal with the given PID"
echo " - this is done by checking the first child of the terminal, which is usually a shell"
echo " - default to ~ if there is no child"
exit 1
fi
# get pid of first child of terminal
first_child=$(command pgrep -P $1 | head -n 1)
# if there is no child, default to $HOME
if [[ -z "${first_child}" ]]; then
echo "${HOME}"
else
echo $(readlink -f /proc/${first_child}/cwd)
fi
#!/usr/bin/env bash
# All my gist code is licensed under the MIT license.
# Add this to your PATH
# print usage with -h/--help
if [[ "$#" -eq 1 && ($1 == "-h" || $1 == "--help") ]]; then
echo "Usage: sway-get-workspace-cwd [workspace-name]"
echo " - Prints the CWD of the first window in the provided workspace"
echo " - If no workspace is provided, uses the currently focused workspace"
echo " - If there is no window in the workspace, return ~"
exit 1
fi
# if workspace name was provided, use it. else, get focused workspace name
if [[ -n $1 ]]; then
workspace=$1
else
workspace=$(swaymsg -t get_workspaces | jq '.[] | select(.focused==true).name' | tr -d '"')
fi
# get first window in workspace
# - By getting every node in the workspace (even nested nodes), and
# returning the first one with a PID.
# - This assumes that the first window is a terminal.
first_window_pid=$(swaymsg -t get_tree | jq -r --arg workspace "$workspace" '.nodes[].nodes[] | select(.name==$workspace) | recurse(.nodes[]) | del(.nodes) | select(.pid) | .pid' | head -n 1)
# if pid was found, get its cwd. else return $HOME
if [[ -n $first_window_pid ]]; then
echo $(get-terminal-cwd $first_window_pid)
else
echo "$HOME"
fi
#!/usr/bin/env bash
# All my gist code is licensed under the MIT license.
# Add this to your ~/.bashrc
# sway-use-workspace-cwd
# - this must be implemented as a function because it calls 'cd'
# - usecase: matching scracthpad's cwd to the cwd of the workspace
function sway-use-workspace-cwd () {
# print usage with -h/--help
if [[ "$#" -eq 1 && ("$1" == "-h" || "$1" == "--help") ]]; then
echo "Usage: sway-use-workspace-cwd [workspace-name]"
echo " - Change to the CWD of the first window in the provided workspace"
echo " - If no workspace is provided, uses the currently focused workspace"
echo " - If there is no window in the workspace, change to ~"
exit 1
fi
cd $(sway-get-workspace-cwd "$@")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment