#!/usr/bin/env bash set -euo pipefail Reset="" Red="" Green="" Dim="" Bold="" if [[ -t 1 ]]; then Reset=$'\033[0m' Red=$'\033[0;31m' Green=$'\033[0;32m' Dim=$'\033[0;2m' Bold=$'\033[1m' fi error() { echo -e "${Red}error${Reset}: $*" >&2 exit 1 } info() { echo -e "${Dim}$*${Reset}" } success() { echo -e "${Green}$*${Reset}" } command -v curl >/dev/null || error "curl is required to install starship" if ! command -v unzip >/dev/null && ! command -v 7z >/dev/null; then error "either unzip or 7z is required to install starship" fi os="$(uname -s 2>/dev/null || true)" arch="$(uname -m 2>/dev/null || true)" if [[ ${OS:-} == Windows_NT ]]; then os="windows" fi case "$os" in Linux) os="linux" ;; Darwin) os="darwin" ;; MINGW* | MSYS* | CYGWIN*) os="windows" ;; windows) os="windows" ;; *) error "unsupported OS: $os" ;; esac case "$arch" in x86_64 | amd64) arch="x64" ;; arm64 | aarch64) arch="arm64" ;; *) error "unsupported architecture: $arch" ;; esac if [[ "$os" == "windows" ]]; then if [[ "$arch" != "x64" ]]; then error "unsupported platform: windows-$arch" fi platform="windows-x64" exe_name="starship.exe" else platform="$os-$arch" exe_name="starship" fi install_dir="${STARSHIP_INSTALL:-$HOME/.starship}" bin_dir="$install_dir/bin" exe="$bin_dir/$exe_name" # Display paths: use ~ for $HOME, otherwise show full path if [[ $install_dir == "$HOME"/* ]]; then display_dir="~${install_dir#"$HOME"}" else display_dir="$install_dir" fi display_bin="$display_dir/bin" display_exe="$display_bin/$exe_name" mkdir -p "$bin_dir" || error "failed to create install directory: $bin_dir" tmpdir="$(mktemp -d "${TMPDIR:-/tmp}/starship.XXXXXX")" archive="$tmpdir/starship.zip" trap 'rm -rf "$tmpdir"' EXIT download_url="https://install.starship.io/bin/boot/latest/$platform" info "Downloading $download_url" curl --fail --location --progress-bar --output "$archive" "$download_url" || error "failed to download: $download_url" if command -v unzip >/dev/null; then unzip -oq "$archive" -d "$tmpdir" || error "failed to extract archive" else 7z x -y "-o$tmpdir" "$archive" >/dev/null || error "failed to extract archive" fi src="$tmpdir/$exe_name" if [[ ! -f "$src" ]]; then src="$(find "$tmpdir" -type f -name "$exe_name" | head -n 1 || true)" fi [[ -n "$src" && -f "$src" ]] || error "archive did not contain $exe_name" mv "$src" "$exe" || error "failed to install to $exe" chmod +x "$exe" || error "failed to set executable permissions on $exe" success "starship installed to ${Bold}$display_exe${Reset}" # Auto-add to PATH in shell profile shell_name="$(basename "${SHELL:-}")" if [[ $bin_dir == "$HOME"/* ]]; then export_line="export PATH=\"\$HOME${bin_dir#"$HOME"}:\$PATH\"" else export_line="export PATH=\"$bin_dir:\$PATH\"" fi marker="# starship" profile="" case "$shell_name" in zsh) profile="$HOME/.zshrc" ;; bash) if [[ -f "$HOME/.bashrc" ]]; then profile="$HOME/.bashrc" elif [[ -f "$HOME/.bash_profile" ]]; then profile="$HOME/.bash_profile" else profile="$HOME/.bashrc" fi ;; fish) profile="${XDG_CONFIG_HOME:-$HOME/.config}/fish/config.fish" export_line="fish_add_path $bin_dir" ;; esac if [[ -n "$profile" ]]; then # Display path for profile if [[ $profile == "$HOME"/* ]]; then display_profile="~${profile#"$HOME"}" else display_profile="$profile" fi if [[ ! -f "$profile" ]] || ! grep -qF "$marker" "$profile"; then mkdir -p "$(dirname "$profile")" printf '\n%s\n%s\n' "$marker" "$export_line" >>"$profile" echo success "Added starship to PATH in ${Bold}$display_profile${Reset}" fi if [[ ":$PATH:" != *":$bin_dir:"* ]]; then echo info "Run:" echo " source $display_profile" echo " starship --help" echo " starship login" else echo info "Run:" echo " starship --help" echo " starship login" fi else echo info "Add starship to your PATH:" echo " $export_line" echo info "Run:" echo " $display_exe --help" echo " $display_exe login" fi