linux-config/files/dotfiles/shellconfig/workspaces/add_workspace.sh

73 lines
1.8 KiB
Bash
Executable file
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
JSON_PATH=~/.shellconfig/workspaces/workspaces.json
function yes_no_question() {
QUESTION=$1
RESPONSE=$(cat <<EOF | fzf --reverse --header "${QUESTION}"
No
🗸 Yes
EOF
)
echo $(echo $RESPONSE | awk '{print $2}')
}
NAME=${1:-}
# check if folder already saved as a workspace
EXISTING_WSP=$(cat $JSON_PATH | jq -r --arg pwd $(pwd) 'to_entries[] | select(.value.dir == $pwd)')
if [ ! -z "$EXISTING_WSP" ]; then
WSP_NAME=$(echo "$EXISTING_WSP" | jq -r --arg pwd $(pwd) '.key')
RESPONSE=$(yes_no_question "⚠️ There is already a workspace named '${WSP_NAME}' for this folder. Would you like to override?")
if [[ "$RESPONSE" != "Yes" ]]; then
echo Canceled
exit
fi
NAME=$WSP_NAME
fi
if [ -z "$NAME" ]; then
read -p " Please give this workspace a name: " NAME
fi
if [ -z "$NAME" ]; then
echo "Error: Invalid workspace name"
exit 1
fi
# Check if workspace already exists
if [ -z "$EXISTING_WSP" ] && [ $(cat $JSON_PATH | jq 'keys' | grep "${NAME}") ]; then
OVERRIDE=$(yes_no_question "a Worspace with name '${NAME}' already exists. Override?")
if [[ "$OVERRIDE" == "No" ]]; then
echo Canceled
exit
fi
fi
CURRENT_DIR=$(pwd)
LAYOUT=$(cat <<EOF | fzf --reverse --header " Select a layout/custom-command for ${NAME}"
ide
grid
layout "2 1"
layout "1 2"
Custom command
EOF
)
# If custom command...
IS_CMD=0
if [[ "${LAYOUT}" == "Custom command" ]]; then
IS_CMD=1
read -p " Please provide a command: " LAYOUT
fi
if [ -z "$LAYOUT" ]; then
echo "Invalid layout/command"
exit 1
fi
echo $(cat ${JSON_PATH} \
| jq --arg NAME "$NAME" --arg LAYOUT "$LAYOUT" --arg CURRENT_DIR "$CURRENT_DIR" --arg IS_CMD "$IS_CMD" '. + {($NAME):{layout: $LAYOUT, dir: $CURRENT_DIR, is_cmd: $IS_CMD}}') > ${JSON_PATH}
echo "✅ Added ${NAME}"