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
|
2022-02-02 08:13:19 +00:00
|
|
|
|
read -p "ℹ️ Please give this workspace a name: " NAME
|
2022-01-20 18:12:28 +00:00
|
|
|
|
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)
|
|
|
|
|
|
2022-02-02 08:13:19 +00:00
|
|
|
|
LAYOUT=$(cat <<EOF | fzf --reverse --header "ℹ️ Select a layout/custom-command for ${NAME}"
|
2022-01-20 18:12:28 +00:00
|
|
|
|
ide
|
|
|
|
|
grid
|
|
|
|
|
layout "2 1"
|
|
|
|
|
layout "1 2"
|
2022-02-02 08:13:19 +00:00
|
|
|
|
Custom command
|
2022-01-20 18:12:28 +00:00
|
|
|
|
EOF
|
|
|
|
|
)
|
2022-02-02 08:13:19 +00:00
|
|
|
|
|
|
|
|
|
# If custom command...
|
|
|
|
|
IS_CMD=0
|
|
|
|
|
if [[ "${LAYOUT}" == "Custom command" ]]; then
|
|
|
|
|
IS_CMD=1
|
|
|
|
|
read -p "ℹ️ Please provide a command: " LAYOUT
|
|
|
|
|
fi
|
|
|
|
|
|
2022-01-20 18:12:28 +00:00
|
|
|
|
if [ -z "$LAYOUT" ]; then
|
2022-02-02 08:13:19 +00:00
|
|
|
|
echo "Invalid layout/command"
|
2022-01-20 18:12:28 +00:00
|
|
|
|
exit 1
|
|
|
|
|
fi
|
2022-02-02 08:13:19 +00:00
|
|
|
|
|
2022-01-20 18:12:28 +00:00
|
|
|
|
echo $(cat ${JSON_PATH} \
|
2022-02-02 08:13:19 +00:00
|
|
|
|
| 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}
|
2022-01-20 18:12:28 +00:00
|
|
|
|
echo "✅ Added ${NAME}"
|
|
|
|
|
|