2022-01-20 18:12:28 +00:00
|
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
|
|
JSON_PATH=~/.shellconfig/workspaces/workspaces.json
|
|
|
|
|
|
2022-01-23 10:28:42 +00:00
|
|
|
|
function yes_no_question() {
|
|
|
|
|
QUESTION=$1
|
|
|
|
|
RESPONSE=$(cat <<EOF | fzf --reverse --header "${QUESTION}"
|
2022-01-24 09:54:38 +00:00
|
|
|
|
⨯ No
|
|
|
|
|
🗸 Yes
|
2022-01-23 10:28:42 +00:00
|
|
|
|
EOF
|
2022-01-24 09:54:38 +00:00
|
|
|
|
)
|
|
|
|
|
echo $(echo $RESPONSE | awk '{print $2}')
|
2022-01-23 10:28:42 +00:00
|
|
|
|
}
|
2022-01-20 18:12:28 +00:00
|
|
|
|
|
|
|
|
|
NAME=${1:-}
|
2022-01-23 10:28:42 +00:00
|
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
2022-01-20 18:12:28 +00:00
|
|
|
|
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
|
|
|
|
|
|
2022-01-23 08:52:04 +00:00
|
|
|
|
# Check if workspace already exists
|
2022-01-23 10:28:42 +00:00
|
|
|
|
if [ -z "$EXISTING_WSP" ] && [ $(cat $JSON_PATH | jq 'keys' | grep "${NAME}") ]; then
|
|
|
|
|
OVERRIDE=$(yes_no_question "a Worspace with name '${NAME}' already exists. Override?")
|
2022-01-23 08:52:04 +00:00
|
|
|
|
if [[ "$OVERRIDE" == "No" ]]; then
|
|
|
|
|
echo Canceled
|
|
|
|
|
exit
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
|
2022-01-20 18:12:28 +00:00
|
|
|
|
CURRENT_DIR=$(pwd)
|
|
|
|
|
|
|
|
|
|
LAYOUT=$(cat <<EOF | fzf --reverse --header "Select a layout for ${NAME}"
|
|
|
|
|
ide
|
|
|
|
|
grid
|
|
|
|
|
layout "2 1"
|
|
|
|
|
layout "1 2"
|
|
|
|
|
EOF
|
|
|
|
|
)
|
|
|
|
|
if [ -z "$LAYOUT" ]; then
|
|
|
|
|
echo "Invalid layout"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
echo $(cat ${JSON_PATH} \
|
|
|
|
|
| jq --arg NAME "$NAME" --arg LAYOUT "$LAYOUT" --arg CURRENT_DIR "$CURRENT_DIR" '. + {($NAME):{layout: $LAYOUT, dir: $CURRENT_DIR}}') > ${JSON_PATH}
|
|
|
|
|
echo "✅ Added ${NAME}"
|
|
|
|
|
|