#!/usr/bin/env sh # -*- mode: sh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*- # vim: ft=sh sw=2 ts=2 et set -eu WORKDIR="$(mktemp -d)" || exit 1 trap 'rm -rf "${WORKDIR:?}"' EXIT INT TERM ZOPT="" AOPT="" BOPT="main" while getopts ":i:a:b:" opt; do case ${opt} in i) ZOPT="${ZOPT}${OPTARG}" ;; a) AOPT="${AOPT}${OPTARG}" ;; b) BOPT="${OPTARG}" ;; \?) echo "Invalid option: ${OPTARG}" 1>&2 exit 1 ;; :) echo "Invalid option: ${OPTARG} requires an argument" 1>&2 exit 1 ;; *) echo "Invalid option: ${OPTARG}" 1>&2 exit 1 ;; esac done shift $((OPTIND - 1)) if [ "${AOPT}" = loader ]; then ZI_CONFIG_DIR="${XDG_CONFIG_HOME:-${HOME}/.config}/zi" command mkdir -p "${ZI_CONFIG_DIR}" if command -v curl >/dev/null 2>&1; then command curl -fsSL https://raw.githubusercontent.com/z-shell/zi-src/main/lib/zsh/init.zsh -o "${ZI_CONFIG_DIR}/init.zsh" elif command -v wget >/dev/null 2>&1; then command wget -qO "${ZI_CONFIG_DIR}/init.zsh" https://raw.githubusercontent.com/z-shell/zi-src/main/lib/zsh/init.zsh fi command chmod go-w "${ZI_CONFIG_DIR}" && command chmod a+x "${ZI_CONFIG_DIR}/init.zsh" # shellcheck disable=SC2016 command sed -i 's|: ${ZI\[STREAM\]:="main"}|: ${ZI[STREAM]:="'"${BOPT}"'"}|' "${ZI_CONFIG_DIR}/init.zsh" fi if [ -z "${ZI_HOME-}" ]; then ZI_HOME="${XDG_DATA_HOME:-${HOME}/.local/share}/zi" fi if [ -z "${ZI_BIN_DIR_NAME-}" ]; then ZI_BIN_DIR_NAME="bin" fi if ! test -d "${ZI_HOME}"; then command mkdir "${ZI_HOME}" command chmod go-w "${ZI_HOME}" fi if ! command -v git >/dev/null 2>&1; then printf '%s\n' "▓▒░ Something went wrong: no git available, cannot proceed." exit 1 fi # Get the download-progress bar tool if command -v curl >/dev/null 2>&1; then command mkdir -p /tmp/zi cd /tmp/zi || exit 1 command curl -fsSLO https://raw.githubusercontent.com/z-shell/zi/main/lib/zsh/git-process-output.zsh && command chmod a+x /tmp/zi/git-process-output.zsh elif command -v wget >/dev/null 2>&1; then command mkdir -p /tmp/zi cd /tmp/zi || exit 1 command wget -q https://raw.githubusercontent.com/z-shell/zi/main/lib/zsh/git-process-output.zsh && command chmod a+x /tmp/zi/git-process-output.zsh fi if test -d "${ZI_HOME}/${ZI_BIN_DIR_NAME}/.git"; then cd "${ZI_HOME}/${ZI_BIN_DIR_NAME}" || exit 1 printf '%s\n' "▓▒░ Updating (z-shell/zi) plugin manager at ${ZI_HOME}/${ZI_BIN_DIR_NAME}" command git clean -d -f -f command git reset --hard HEAD command git pull -q origin "${BOPT}" else cd "${ZI_HOME}" || exit 1 printf '%s\n' "▓▒░ Installing (z-shell/zi) plugin manager at ${ZI_HOME}/${ZI_BIN_DIR_NAME}" { git clone --progress --depth=1 --branch "${BOPT}" https://github.com/z-shell/zi.git "${ZI_BIN_DIR_NAME}" \ 2>&1 | { /tmp/zi/git-process-output.zsh || cat; }; } 2>/dev/null if [ -d "${ZI_HOME}/${ZI_BIN_DIR_NAME}" ] && [ -f "${ZI_HOME}/${ZI_BIN_DIR_NAME}/zi.zsh" ]; then printf '%s\n' "▓▒░ Successfully installed at ${ZI_HOME}/${ZI_BIN_DIR_NAME}". else printf '%s\n' "▓▒░ Something went wrong, couldn't install ZI at ${ZI_HOME}/${ZI_BIN_DIR_NAME}" exit 1 fi fi # # Modify .zshrc # MAIN_PROFILE() { THE_ZDOTDIR="${ZDOTDIR:-${HOME}}" if grep -E '(zi|init|zinit)\.zsh' "${THE_ZDOTDIR}/.zshrc" >/dev/null 2>&1; then printf '%s\n' "▓▒░ Seems that .zshrc already has content or setup skipped - no changes will be made." ZOPT='skip' fi if [ "${ZOPT}" != skip ] && [ "${AOPT}" != loader ]; then printf '%s\n' "▓▒░ Updating ${THE_ZDOTDIR}/.zshrc" ZI_HOME="$(echo "${ZI_HOME}" | sed "s|${HOME}|\$HOME|")" command cat <<-EOF >>"${THE_ZDOTDIR}/.zshrc" if [[ ! -f ${ZI_HOME}/${ZI_BIN_DIR_NAME}/zi.zsh ]]; then print -P "%F{33}▓▒░ %F{160}Installing (%F{33}z-shell/zi%F{160})…%f" command mkdir -p "${ZI_HOME}" && command chmod go-rwX "${ZI_HOME}" command git clone -q --depth=1 --branch "${BOPT}" https://github.com/z-shell/zi "${ZI_HOME}/${ZI_BIN_DIR_NAME}" && \\ print -P "%F{33}▓▒░ %F{34}Installation successful.%f%b" || \\ print -P "%F{160}▓▒░ The clone has failed.%f%b" fi source "${ZI_HOME}/${ZI_BIN_DIR_NAME}/zi.zsh" autoload -Uz _zi (( \${+_comps} )) && _comps[zi]=_zi # examples here -> https://wiki.zshell.dev/ecosystem/category/-annexes zicompinit # <- https://wiki.zshell.dev/docs/guides/commands EOF printf '%s\n' "▓▒░ Minimal configuration" fi if [ "${AOPT}" = loader ] && [ "${ZOPT}" != skip ]; then command cat <<-EOF >>"${THE_ZDOTDIR}/.zshrc" if [[ -r "\${XDG_CONFIG_HOME:-\${HOME}/.config}/zi/init.zsh" ]]; then source "\${XDG_CONFIG_HOME:-\${HOME}/.config}/zi/init.zsh" && zzinit fi EOF printf '%s\n' "▓▒░ Loader added" fi } ANNEX_PROFILE() { if [ "${AOPT}" = annex ]; then file="${WORKDIR}/temp-zsh-config" command cat <<-EOF >>"${file}" zi light-mode for \\ z-shell/z-a-meta-plugins \\ @annexes # <- https://wiki.zshell.dev/ecosystem/category/-annexes # examples here -> https://wiki.zshell.dev/community/gallery/collection zicompinit # <- https://wiki.zshell.dev/docs/guides/commands EOF printf '%s\n' "▓▒░ Installing annexes" command cat "${file}" >>"${THE_ZDOTDIR}/.zshrc" zsh -ic "@zi-scheduler burst" elif [ "${AOPT}" = zunit ]; then file2="${WORKDIR}/temp-zunit-config" command cat <<-EOF >>"${file2}" zi light-mode for \\ z-shell/z-a-meta-plugins \\ @annexes @zunit EOF printf '%s\n' "▓▒░ Installing annexes + zunit" command cat "${file2}" >>"${THE_ZDOTDIR}/.zshrc" zsh -ic "@zi-scheduler burst" else printf '%s\n' "▓▒░ Skipped all annexes" fi } ZPMOD_PROFILE() { _zpmod_sh="" case "$0" in */*) _script_dir="$(cd "$(dirname "$0")" 2>/dev/null && pwd)" || _script_dir="" ;; *) _script_dir="" ;; esac if [ -n "${_script_dir}" ] && [ -f "${_script_dir}/install_zpmod.sh" ]; then _zpmod_sh="${_script_dir}/install_zpmod.sh" else _zpmod_sh="${WORKDIR}/install_zpmod.sh" _zpmod_url="https://raw.githubusercontent.com/z-shell/zi-src/main/lib/sh/install_zpmod.sh" if command -v curl >/dev/null 2>&1; then command curl -fsSL "${_zpmod_url}" -o "${_zpmod_sh}" elif command -v wget >/dev/null 2>&1; then command wget -qO "${_zpmod_sh}" "${_zpmod_url}" else printf '%s\n' "-- ERROR -- curl or wget is required to download install_zpmod.sh" >&2 exit 1 fi if [ ! -s "${_zpmod_sh}" ]; then printf '%s\n' "-- ERROR -- failed to download install_zpmod.sh" >&2 exit 1 fi command chmod a+x "${_zpmod_sh}" fi exec sh "${_zpmod_sh}" "$@" } CLOSE_PROFILE() { git_refs="$( command cd "${ZI_HOME}/${ZI_BIN_DIR_NAME}" || true command git log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit | head -5 )" printf '%s\n' "▓▒░ Latest changes:" printf '%s\n' "${git_refs}" } MAIN() { if [ "${AOPT}" = zpmod ]; then ZPMOD_PROFILE "$@" else MAIN_PROFILE ANNEX_PROFILE CLOSE_PROFILE fi command cat <<-EOF ▓▒░ ■■■■■■■■■■■■■■■■■ Successfully installed ❮ ZI ❯ ■■■■■■■■■ ▓▒░ Wiki: https://wiki.zshell.dev ▓▒░ Issues: https://github.com/z-shell/zi/issues ▓▒░ Discussions: https://discussions.zshell.dev ▓▒░ ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ EOF exit 0 } MAIN "${@}"