start-local-collaboration

 1#!/bin/bash
 2
 3set -e
 4
 5if [[ -z "$GITHUB_TOKEN" ]]; then
 6  cat <<-MESSAGE
 7Missing \`GITHUB_TOKEN\` environment variable. This token is needed
 8for fetching your GitHub identity from the command-line.
 9
10Create an access token here: https://github.com/settings/tokens
11Then edit your \`~/.zshrc\` (or other shell initialization script),
12adding a line like this:
13
14    export GITHUB_TOKEN="(the token)"
15
16MESSAGE
17  exit 1
18fi
19
20# Install jq if it's not installed
21if ! command -v jq &> /dev/null; then
22    echo "Installing jq..."
23    brew install jq
24fi
25
26# Start one Zed instance as the current user and a second instance with a different user.
27username_1=$(curl -sH "Authorization: bearer $GITHUB_TOKEN" https://api.github.com/user | jq -r .login)
28username_2=nathansobo
29if [[ $username_1 == $username_2 ]]; then
30  username_2=as-cii
31fi
32
33# Make each Zed instance take up half of the screen.
34output=$(system_profiler SPDisplaysDataType -json)
35main_display=$(echo "$output" | jq '.SPDisplaysDataType[].spdisplays_ndrvs[] | select(.spdisplays_main == "spdisplays_yes")')
36resolution=$(echo "$main_display" | jq -r '._spdisplays_resolution')
37width=$(echo "$resolution" | jq -Rr 'match("(\\d+) x (\\d+)").captures[0].string')
38half_width=$(($width / 2))
39height=$(echo "$resolution" | jq -Rr 'match("(\\d+) x (\\d+)").captures[1].string')
40y=0
41
42position_1=0,${y}
43position_2=${half_width},${y}
44
45# Authenticate using the collab server's admin secret.
46export ZED_STATELESS=1
47export ZED_ADMIN_API_TOKEN=secret
48export ZED_SERVER_URL=http://localhost:8080
49export ZED_WINDOW_SIZE=${half_width},${height}
50
51cargo build
52sleep 0.5
53
54# Start the two Zed child processes. Open the given paths with the first instance.
55trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT
56ZED_IMPERSONATE=${ZED_IMPERSONATE:=${username_1}} ZED_WINDOW_POSITION=${position_1} target/debug/Zed $@ &
57SECOND=true ZED_IMPERSONATE=${username_2} ZED_WINDOW_POSITION=${position_2} target/debug/Zed &
58wait