#!/bin/sh # # Installs the terminal version of Edit as the `edit` command. # # curl -fsSL https://rustedit.dev/install.sh | sh # # EDIT_INSTALL_DIR where `edit` goes (default: ~/.local/bin) # EDIT_NO_MODIFY_PATH=1 leave the shell profile alone # NO_COLOR=1 plain output, without colour or animation # # Run it again to update. To remove Edit, delete the `edit` file it names at the end. set -eu SITE="${EDIT_SITE:-https://rustedit.dev}" INSTALL_DIR="${EDIT_INSTALL_DIR:-$HOME/.local/bin}" # ── Looks ───────────────────────────────────────────────────────────────────── # # Colour and motion only on a terminal that shows them. Piped into a file or a CI log, every line # is plain text, and nothing waits for an animation. FANCY=0 if [ -t 1 ] && [ -z "${NO_COLOR:-}" ] && [ "${TERM:-dumb}" != "dumb" ]; then FANCY=1 fi if [ "$FANCY" = "1" ]; then ESC="$(printf '\033')" RESET="$ESC[0m" BOLD="$ESC[1m" DIM="$ESC[2m" RED="$ESC[31m" # The site's accent, hsl(182 92% 62%), and a deeper teal for the logo between waves. case "${COLORTERM:-}" in truecolor | 24bit) ACCENT="$ESC[38;2;69;240;247m" DEEP="$ESC[38;2;22;120;128m" ;; *) ACCENT="$ESC[38;5;87m" DEEP="$ESC[38;5;30m" ;; esac else ESC="" RESET="" BOLD="" DIM="" RED="" ACCENT="" DEEP="" fi say() { printf ' %s\n' "$*"; } fail() { [ "$FANCY" = "1" ] && printf '\r%s[K' "$ESC" printf ' %s✗%s %s\n' "$RED" "$RESET" "$*" >&2 exit 1 } show_cursor() { [ "$FANCY" = "1" ] && printf '%s[?25h' "$ESC"; return 0; } hide_cursor() { [ "$FANCY" = "1" ] && printf '%s[?25l' "$ESC"; return 0; } # Edit's mark, a window with its title bar and a few lines of code, on an 11 × 7 grid. LOGO_0="###########" LOGO_1="#.#.#.#...#" LOGO_2="###########" LOGO_3="#.........#" LOGO_4="#.#####...#" LOGO_5="#.###.###.#" LOGO_6="###########" # One row of the logo at time `frame`, into LINE. A wave of light runs from the top left to the # bottom right, as Edit's working mark does while an agent works. A negative frame draws it at rest. # Plain parameter expansion, no subshells, so each frame draws at once. logo_row() { # sh has no local variables, so these names are its own: header and draw_header loop on theirs. lr_row="$1" lr_frame="$2" lr_rest="$3" LINE="" lr_col=0 while [ -n "$lr_rest" ]; do lr_cell="${lr_rest%"${lr_rest#?}"}" lr_rest="${lr_rest#?}" if [ "$lr_cell" = "#" ]; then if [ "$lr_frame" -lt 0 ]; then LINE="$LINE${ACCENT}■$RESET " else case $((lr_frame - lr_row - lr_col)) in 0) LINE="$LINE$BOLD${ACCENT}■$RESET " ;; 1 | -1) LINE="$LINE${ACCENT}■$RESET " ;; *) LINE="$LINE${DEEP}■$RESET " ;; esac fi else LINE="$LINE${DIM}·$RESET " fi lr_col=$((lr_col + 1)) done } # The logo with the name, version and site beside it. draw_header() { dh_frame="$1" for dh_row in 0 1 2 3 4 5 6; do eval "dh_mask=\$LOGO_$dh_row" case "$dh_row" in 1) text="$BOLD""Edit $VERSION$RESET" ;; 2) text="the agentic development environment, in your terminal" ;; 3) text="$DIM${SITE#https://}$RESET" ;; *) text="" ;; esac logo_row "$dh_row" "$dh_frame" "$dh_mask" printf ' %s %s%s[K\n' "$LINE" "$text" "$ESC" done } header() { printf '\n' if [ "$FANCY" = "1" ]; then hide_cursor draw_header 0 frame=1 # Two passes of the wave, then the logo settles in the accent colour. while [ "$frame" -le 34 ]; do sleep 0.03 printf '%s[7A' "$ESC" draw_header $((frame % 17)) frame=$((frame + 1)) done printf '%s[7A' "$ESC" draw_header -1 show_cursor else printf ' Edit %s\n the agentic development environment, in your terminal\n %s\n' "$VERSION" "${SITE#https://}" fi printf '\n' } # Runs a command with a spinner beside `label`, and leaves nothing behind it. The caller prints # the step's ✓ row. Without a terminal it just runs the command. spin() { label="$1" shift if [ "$FANCY" != "1" ]; then "$@" return fi hide_cursor "$@" & pid=$! i=0 while kill -0 "$pid" 2>/dev/null; do case $((i % 10)) in 0) glyph="⠋" ;; 1) glyph="⠙" ;; 2) glyph="⠹" ;; 3) glyph="⠸" ;; 4) glyph="⠼" ;; 5) glyph="⠴" ;; 6) glyph="⠦" ;; 7) glyph="⠧" ;; 8) glyph="⠇" ;; *) glyph="⠏" ;; esac printf '\r %s%s%s %s%s[K' "$ACCENT" "$glyph" "$RESET" "$label" "$ESC" i=$((i + 1)) sleep 0.08 done printf '\r%s[K' "$ESC" show_cursor wait "$pid" } # A finished step: ✓, its name, then what it found, in columns. done_row() { printf ' %s✓%s %s%-11s%s %s\n' "$ACCENT" "$RESET" "$BOLD" "$1" "$RESET" "$2" } # A second line under a step's value. more_row() { printf ' %s%s%s\n' "$DIM" "$1" "$RESET" } need() { command -v "$1" >/dev/null 2>&1 || fail "This installer needs $1." } # Reads one field of the terminal build from release.json. plutil ships with macOS and reads JSON. release_field() { index=0 while id="$(plutil -extract "apps.$index.id" raw -o - "$1" 2>/dev/null)"; do if [ "$id" = "terminal" ]; then plutil -extract "apps.$index.$2" raw -o - "$1" return fi index=$((index + 1)) done return 1 } # The Team ID in "Developer ID Application: Name (TEAMID)". team_of_signer() { printf '%s\n' "$1" | sed -n 's/.*(\([A-Z0-9]\{10\}\))$/\1/p' } profile_for_shell() { case "${SHELL:-}" in */zsh) printf '%s\n' "${ZDOTDIR:-$HOME}/.zshrc" ;; */bash) printf '%s\n' "$HOME/.bash_profile" ;; */fish) printf '%s\n' "${XDG_CONFIG_HOME:-$HOME/.config}/fish/conf.d/edit.fish" ;; *) printf '%s\n' "$HOME/.profile" ;; esac } # The path with the home folder written as ~, as people read it. tilde() { case "$1" in "$HOME"/*) printf '~%s\n' "${1#"$HOME"}" ;; *) printf '%s\n' "$1" ;; esac } add_to_path() { PATH_NOTE="" case ":$PATH:" in *":$INSTALL_DIR:"*) return 0 ;; esac if [ "${EDIT_NO_MODIFY_PATH:-0}" = "1" ]; then PATH_NOTE="Add $(tilde "$INSTALL_DIR") to your PATH to run edit by name." RUN_WITH="$(tilde "$INSTALL_DIR")/edit" return 0 fi profile="$(profile_for_shell)" case "$profile" in *.fish) line="fish_add_path \"$INSTALL_DIR\"" ;; *) line="export PATH=\"$INSTALL_DIR:\$PATH\"" ;; esac if [ -f "$profile" ] && grep -Fqx "$line" "$profile"; then : else mkdir -p "$(dirname "$profile")" printf '\n# Added by the Edit installer\n%s\n' "$line" >> "$profile" PATH_NOTE="Added $(tilde "$INSTALL_DIR") to PATH in $(tilde "$profile")." fi NEW_SHELL_NEEDED=1 } # "1.8 MB", "640 KB". human_size() { awk -v bytes="$1" 'BEGIN { if (bytes >= 1048576) printf "%.1f MB", bytes / 1048576 else printf "%d KB", (bytes + 1023) / 1024 }' } main() { case "$(uname -s)" in Darwin) ;; Linux) fail "There is no Linux build to download yet. This installer covers macOS." ;; *) fail "This installer covers macOS. $(uname -s) is not supported yet." ;; esac case "$(uname -m)" in arm64) ;; *) fail "The macOS build is for Apple silicon. This Mac reports $(uname -m)." ;; esac for tool in curl plutil shasum ditto codesign; do need "$tool" done work="$(mktemp -d "${TMPDIR:-/tmp}/edit-install.XXXXXX")" trap 'show_cursor; rm -rf "$work"' EXIT trap 'show_cursor; rm -rf "$work"; exit 130' INT TERM spin "Reading the latest release" curl -fsSL "$SITE/downloads/release.json" -o "$work/release.json" \ || fail "Could not read $SITE/downloads/release.json." file="$(release_field "$work/release.json" file)" || fail "The release lists no terminal build." VERSION="$(release_field "$work/release.json" version)" sha="$(release_field "$work/release.json" sha256)" signer="$(plutil -extract signer raw -o - "$work/release.json")" case "$file" in *[!A-Za-z0-9._-]* | *..* | "") fail "The release names an unexpected file: $file" ;; esac header spin "Downloading edit $VERSION" curl -fsSL "$SITE/downloads/$file" -o "$work/$file" \ || fail "Could not download $file." size="$(human_size "$(wc -c < "$work/$file" | tr -d ' ')")" done_row "Downloaded" "edit $VERSION $DIM$size$RESET" actual="$(shasum -a 256 "$work/$file" | awk '{print $1}')" [ "$actual" = "$sha" ] || fail "The download does not match its SHA-256. Nothing was installed." done_row "Verified" "sha-256 $(printf '%s' "$actual" | cut -c 1-8) ${DIM}matches the release$RESET" ditto -x -k "$work/$file" "$work/unpacked" binary="$(find "$work/unpacked" -type f -name edit | head -n 1)" [ -n "$binary" ] || fail "$file does not contain the edit binary." spin "Checking the signature" codesign --verify --strict "$binary" 2>/dev/null \ || fail "The edit binary is not validly signed. Nothing was installed." expected_team="$(team_of_signer "$signer")" actual_team="$(codesign -dv "$binary" 2>&1 | sed -n 's/^TeamIdentifier=//p')" if [ -n "$expected_team" ] && [ "$actual_team" != "$expected_team" ]; then fail "The edit binary is signed by team $actual_team, not $expected_team. Nothing was installed." fi done_row "Signed" "Developer ID $DIM${actual_team:-unknown team}$RESET" mkdir -p "$INSTALL_DIR" chmod 755 "$binary" cp "$binary" "$INSTALL_DIR/.edit.new" mv -f "$INSTALL_DIR/.edit.new" "$INSTALL_DIR/edit" done_row "Installed" "$(tilde "$INSTALL_DIR")/edit" NEW_SHELL_NEEDED=0 RUN_WITH=edit add_to_path [ -n "$PATH_NOTE" ] && more_row "$PATH_NOTE" found="$(command -v edit 2>/dev/null || true)" if [ -n "$found" ] && [ "$found" != "$INSTALL_DIR/edit" ]; then more_row "$(tilde "$found") comes first on your PATH, so 'edit' runs that one." fi printf '\n' if [ "$NEW_SHELL_NEEDED" = "1" ]; then say "Open a new terminal, then run:" else say "Start editing:" fi printf '\n %s%s%s %s[file]%s\n\n' "$BOLD$ACCENT" "$RUN_WITH" "$RESET" "$DIM" "$RESET" printf ' %s%-10s%s %s\n' "$DIM" "Website" "$RESET" "${SITE#https://}" printf ' %s%-10s%s %s\n' "$DIM" "Update" "$RESET" "curl -fsSL $SITE/install.sh | sh" printf ' %s%-10s%s %s\n\n' "$DIM" "Uninstall" "$RESET" "rm $(tilde "$INSTALL_DIR")/edit" } main "$@"