From 2da1d30e9e579e1b30d8d692c1ed8330031b728b Mon Sep 17 00:00:00 2001 From: Hans Goor Date: Fri, 2 Aug 2024 00:08:16 +0200 Subject: [PATCH] Add modular ZSH configuration --- zsh/dot-zshrc | 8 +++-- zsh/dot-zshrc.d/00-setup.sh | 7 ++++ zsh/dot-zshrc.d/01-aliases.sh | 4 +++ zsh/dot-zshrc.d/10-workspace.sh | 60 +++++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 zsh/dot-zshrc.d/00-setup.sh create mode 100644 zsh/dot-zshrc.d/01-aliases.sh create mode 100644 zsh/dot-zshrc.d/10-workspace.sh diff --git a/zsh/dot-zshrc b/zsh/dot-zshrc index 803076e..b4e2a3e 100644 --- a/zsh/dot-zshrc +++ b/zsh/dot-zshrc @@ -105,5 +105,9 @@ zstyle ':completion:*:manuals' separate-sections true zstyle ':completion:*:manuals.*' insert-sections true zstyle ':completion:*:man:*' menu yes select -alias vim='nvim' -alias devspace='podman run -it -v "$(pwd):/workspace" -e PUID="$(id -u)" -e PGID="$(id -g)" --uidmap="$(id -u)":0:1 --uidmap=0:1:"$(id -u)" --gidmap="$(id -g)":0:1 --gidmap=0:1:"$(id -g)"' +# Load all files from the .zshrc.d folder. +if [[ -d "${HOME}/.zshrc.d" ]]; then + for ext in "${HOME}"/.zshrc.d/*.sh; do + source "${ext}" + done +fi diff --git a/zsh/dot-zshrc.d/00-setup.sh b/zsh/dot-zshrc.d/00-setup.sh new file mode 100644 index 0000000..9522509 --- /dev/null +++ b/zsh/dot-zshrc.d/00-setup.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +export SPACES_DIR="${HOME}/spaces" +export WORKSPACE_DIR="${SPACES_DIR}/workspace" + +mkdir -p "${SPACES_DIR}" + diff --git a/zsh/dot-zshrc.d/01-aliases.sh b/zsh/dot-zshrc.d/01-aliases.sh new file mode 100644 index 0000000..2b7723b --- /dev/null +++ b/zsh/dot-zshrc.d/01-aliases.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +alias vi="nvim" +alias vim="nvim" diff --git a/zsh/dot-zshrc.d/10-workspace.sh b/zsh/dot-zshrc.d/10-workspace.sh new file mode 100644 index 0000000..5cd9731 --- /dev/null +++ b/zsh/dot-zshrc.d/10-workspace.sh @@ -0,0 +1,60 @@ +#!/bin/bash + +alias cdw="cd ${WORKSPACE_DIR}" + +function _get_projects() { + ( cd -- "${WORKSPACE_DIR}" && find . -mindepth 3 -maxdepth 3 -type d | sed 's|^\./||' ) +} + +function cdp() { + if command -v fzf &>/dev/null; then + cd -- "${WORKSPACE_DIR}/$(_get_projects | fzf --query "$*")" + return + fi + + local projects=( "${(@f)$( _get_projects )}" ) + if [[ "$#" -gt 0 ]]; then + projects=( "${(@Mb)projects:#*${1}*}" ) + fi + + select project in "${projects[@]}"; do + cd -- "${WORKSPACE_DIR}/${project}" + break + done +} + +function _cdp_comp() { + local projects=( "${(@f)$( _get_projects )}" ) + + compset -P '*\/' + _describe "${projects[@]}" +} +compdef _cdp_comp cdp + +function clone() { + if [[ "$#" -lt 1 ]]; then + echo "[!] Missing URL to clone!" >&2 + return 1 + fi + + local url="$1" + local forge owner repo + if [[ "${url}" =~ ^ssh:\/\/[a-zA-Z0-9_]+@([a-zA-Z0-9_.]+):[0-9]+/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)(.git)?$ ]]; then + forge="${match[1]:l}" + owner="${match[2]:l}" + repo="${match[3]:l}" + elif [[ "${url}" =~ ^[a-zA-Z0-9_]+@([a-zA-Z0-9_.]+):([a-zA-Z0-9_]+)\/([a-zA-Z0-9_]+)(.git)?$ ]]; then + forge="${match[1]:l}" + owner="${match[2]:l}" + repo="${match[3]:l}" + elif [[ "${url}" =~ ^https?:\/\/([a-zA-Z0-9_.]+)(:[0-9]+)?\/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)(.git)?$ ]]; then + forge="${match[1]:l}" + owner="${match[3]:l}" + repo="${match[4]:l}" + else + echo "[!] Do not know how to handle ${url}!" >&2 + return 1 + fi + + git clone "${url}" "${WORKSPACE_DIR}/${forge}/${owner}/${repo}" +}