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)
35resolution=$(echo "$output" | jq -r '.SPDisplaysDataType[0].spdisplays_ndrvs[] | select(.spdisplays_online == "spdisplays_yes") | ._spdisplays_resolution')
36width=$(echo "$resolution" | jq -Rr 'match("(\\d+) x (\\d+)").captures[0].string')
37half_width=$(($width / 2))
38height=$(echo "$resolution" | jq -Rr 'match("(\\d+) x (\\d+)").captures[1].string')
39y=0
40
41echo "Width: $width"
42echo "Half-width: $half_width"
43echo "Height: $height"
44
45position_1=0,${y}
46position_2=${half_width},${y}
47
48# Authenticate using the collab server's admin secret.
49export ZED_STATELESS=1
50export ZED_ADMIN_API_TOKEN=secret
51export ZED_SERVER_URL=http://localhost:8080
52export ZED_WINDOW_SIZE=${half_width},${height}
53
54cargo build
55sleep 0.5
56
57# Start the two Zed child processes. Open the given paths with the first instance.
58trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT
59ZED_IMPERSONATE=${username_1} ZED_WINDOW_POSITION=${position_1} target/debug/Zed $@ &
60ZED_IMPERSONATE=${username_2} ZED_WINDOW_POSITION=${position_2} target/debug/Zed &
61wait