diff --git a/.gitignore b/.gitignore index 6ba475c..0c697b3 100644 --- a/.gitignore +++ b/.gitignore @@ -16,12 +16,9 @@ zsh/* !zsh/.zshrc !zsh/.zshenv !zsh/.zprofile -!zsh/.zsh_plugins*.txt !zsh/scripts.zsh !zsh/aliases.zsh -!zsh/completions/.keep !zsh/ssh-menu -!zsh/get-dump # starship !starship diff --git a/zsh/.zprofile b/zsh/.zprofile index 33d3a19..680433b 100644 --- a/zsh/.zprofile +++ b/zsh/.zprofile @@ -1,2 +1,5 @@ -export PATH="/usr/local/bin:/opt/homebrew/bin:$HOME/neovim/bin:$HOME/bin:${PATH}" - +path=( + "$HOME/.local/bin" + "$GOPATH/bin" + $path +) diff --git a/zsh/.zsh_plugins.txt b/zsh/.zsh_plugins.txt deleted file mode 100644 index acca4cd..0000000 --- a/zsh/.zsh_plugins.txt +++ /dev/null @@ -1,11 +0,0 @@ -# set up Zsh completions with plugins -# mattmc3/ez-compinit -zsh-users/zsh-completions kind:fpath path:src - -# frameworks like oh-my-zsh are supported -getantidote/use-omz # handle OMZ dependencies -ohmyzsh/ohmyzsh path:lib # load OMZ's library -ohmyzsh/ohmyzsh path:plugins/git - -# popular fish-like plugins -zsh-users/zsh-autosuggestions diff --git a/zsh/.zsh_plugins_darwin.txt b/zsh/.zsh_plugins_darwin.txt deleted file mode 100644 index 7007731..0000000 --- a/zsh/.zsh_plugins_darwin.txt +++ /dev/null @@ -1 +0,0 @@ -lukechilds/zsh-nvm diff --git a/zsh/.zshenv b/zsh/.zshenv index 09af955..5460e20 100644 --- a/zsh/.zshenv +++ b/zsh/.zshenv @@ -4,22 +4,8 @@ export XDG_CACHE_HOME="$HOME/.cache" export XDG_DATA_HOME="$HOME/.local/share" export XDG_STATE_HOME="$HOME/.local/state" -# ~/.local/bin -export PATH="$HOME/.local/bin:$PATH" - -# brew sbin -export PATH="/opt/homebrew/sbin:$PATH" - -# brew lib -export LIBRARY_PATH="$LIBRARY_PATH:/opt/homebrew/lib" - -# cargo -if [[ "$OSTYPE" == "darwin"* ]]; then - source "$HOME/.cargo/env" -fi - -# language environment -export LANG=en_US.UTF-8 +# deduplicate path +typeset -U path # editor export EDITOR=nvim @@ -27,35 +13,12 @@ export EDITOR=nvim # go export GOPATH="$XDG_DATA_HOME/go" export GOMODCACHE="$XDG_CACHE_HOME/go/mod" -export PATH="$GOPATH/bin:$PATH" # ggman export GGROOT="$HOME/git" -# neovim -export PATH="$XDG_DATA_HOME/neovim/bin:$PATH" - -# pyenv -export PYENV_ROOT="$XDG_DATA_HOME/.pyenv" -command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH" - -# pyenv-virtualenv -# leave python promt to starship -export PYENV_VIRTUALENV_DISABLE_PROMPT=1 - -# poetry -export POETRY_HOME="$XDG_DATA_HOME/poetry" -command -v poetry >/dev/null || export PATH="$POETRY_HOME/bin:$PATH" - -# gnupg -export GPG_TTY=$(tty) - -# aflabs -export USER_UID=$(id -u) -export USER_GID=$(id -g) - # name export NAME="Matej Janežič" -# starship nest config into a folder, default is ~/.config/startship.toml +# starship nest config into a folder, default is ~/.config/starship.toml export STARSHIP_CONFIG="$XDG_CONFIG_HOME/starship/config.toml" diff --git a/zsh/.zshrc b/zsh/.zshrc index 713fb81..3d28e77 100644 --- a/zsh/.zshrc +++ b/zsh/.zshrc @@ -1,46 +1,69 @@ -# zsh history file -export HISTFILE="$XDG_STATE_HOME/zsh/history" -export HISTSIZE=100 +# shell options +unsetopt autocd # don't cd into directories by name +setopt NO_BEEP # no terminal bell +setopt AUTO_PUSHD # cd pushes to directory stack +setopt PUSHD_IGNORE_DUPS # no duplicates in directory stack +setopt EXTENDED_GLOB # enable extended glob operators (#, ~, ^) -## pre plugin load -# nvm -export NVM_COMPLETION=true -# zsh autosugestions -export ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE="fg=#ffffff,bg=cyan,bold,underline" +# history +HISTFILE="$XDG_STATE_HOME/zsh/history" +HISTSIZE=10000 +SAVEHIST=10000 +setopt HIST_IGNORE_DUPS # skip consecutive duplicates +setopt HIST_IGNORE_SPACE # skip commands starting with space +setopt SHARE_HISTORY # share history across sessions +setopt HIST_REDUCE_BLANKS # strip extra whitespace -# source antidote -source $ZDOTDIR/.antidote/antidote.zsh -antidote load +# plugins +local _plugins="$XDG_DATA_HOME/zsh/plugins" +local _stamp="$XDG_CACHE_HOME/zsh/plugins-updated" +local _repos=( + "https://github.com/zsh-users/zsh-completions" + "https://github.com/zsh-users/zsh-autosuggestions" +) +local _needs_update=0 -if [[ "$OSTYPE" == "darwin"* ]]; then - antidote load ${ZDOTDIR:-~}/.zsh_plugins_darwin.txt +if [[ ! -f "$_stamp" ]] || [[ -n $_stamp(#qN.mh+168) ]]; then + mkdir -p "${_stamp:h}" + touch "$_stamp" + _needs_update=1 fi -unsetopt autocd +for _repo in $_repos; do + if [[ ! -d "$_plugins/${_repo:t}" ]]; then + git clone "$_repo" "$_plugins/${_repo:t}" + elif (( _needs_update )); then + git -C "$_plugins/${_repo:t}" pull --quiet & + fi +done +(( _needs_update )) && wait -if [[ "$OSTYPE" == "darwin"* ]]; then - eval "$(pyenv init - zsh)" - eval "$(pyenv virtualenv-init - zsh)" -fi +fpath=("$_plugins/zsh-completions/src" $fpath) +ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE="fg=#ffffff,bg=cyan,bold,underline" +source "$_plugins/zsh-autosuggestions/zsh-autosuggestions.zsh" -if [[ "$OSTYPE" == "darwin"* ]]; then - export SSH_AUTH_SOCK=$(gpgconf --list-dirs agent-ssh-socket) -fi - -# ggman -eval "$(ggman shellrc)" - -# custom functions and aliases +# aliases and functions source "$ZDOTDIR/aliases.zsh" source "$ZDOTDIR/scripts.zsh" # completion -fpath=($ZDOTDIR/completions $fpath) +fpath=("$XDG_DATA_HOME/zsh/completions" $fpath) autoload -Uz compinit -compinit -d $XDG_CACHE_HOME/zsh/zcompdump-$ZSH_VERSION -zstyle ':completion:*' cache-path $XDG_CACHE_HOME/zsh/zcompcache - -# opam configuration -# [[ ! -r /Users/janezicmatej/.opam/opam-init/init.zsh ]] || source /Users/janezicmatej/.opam/opam-init/init.zsh > /dev/null 2> /dev/null +# only regenerate dump once per day +local _zcompdump="$XDG_CACHE_HOME/zsh/zcompdump-$ZSH_VERSION" +if [[ -n $_zcompdump(#qN.mh+24) ]]; then + compinit -d "$_zcompdump" +else + compinit -C -d "$_zcompdump" +fi +zstyle ':completion:*' cache-path "$XDG_CACHE_HOME/zsh/zcompcache" +zstyle ':completion:*' menu select +zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}' +# tool hooks +eval "$(ggman shellrc)" eval "$(starship init zsh)" +eval "$(direnv hook zsh)" + +# gnupg +export GPG_TTY=$(tty) diff --git a/zsh/aliases.zsh b/zsh/aliases.zsh index a98ecd1..150853c 100644 --- a/zsh/aliases.zsh +++ b/zsh/aliases.zsh @@ -1,12 +1,7 @@ alias z="exec zsh" alias t=tmux_attach -if [[ "$OSTYPE" == "darwin"* ]]; then - alias n=nvim_ve - alias vim=nvim_ve -elif [[ "$OSTYPE" == "linux"* ]]; then - alias n=nvim -fi +alias n=nvim alias s="ssh-menu" @@ -15,28 +10,59 @@ alias dp="docker ps --format 'table {{.Names}}\t{{.Ports}}'" alias dcp="docker compose ps --format 'table {{.Name}}\t{{.Ports}}'" # navigation -alias cdgit="cd $GGROOT" -alias icloud="cd $HOME/Library/Mobile\ Documents/com\~apple\~CloudDocs" -alias temp="cd $HOME/Desktop/temp" +alias cdgit='cd $GGROOT' -# apple specific -alias accenton="defaults write -g ApplePressAndHoldEnabled -bool true" -alias accentoff="defaults write -g ApplePressAndHoldEnabled -bool false" - -# git -alias gpo="git push origin" -alias gros="git reset origin/$(git_current_branch) --soft" -alias gcan="git commit -v --amend --no-edit" +# git status/diff +alias gst="git status" +alias gd="git diff" +alias gds="git diff --staged" +alias gdup="git diff @{upstream}" +alias glog="git log --oneline --decorate --graph" +alias gloga="git log --oneline --decorate --graph --all" alias glogaa="git log --oneline --graph --all --pretty=format:\"%C(auto)%h %C(blue)(%aL/%cL)%C(auto)%(decorate) %s%Creset\"" -# unsorted -alias ci="glab ci list" -alias bm=batman -alias grow="~/.local/bin/cbonsai -liWC -M 15 -t 2 -w 60 -s $(date +%s)" -alias clean_ds_store='find . -name ".DS_Store" -type f -delete -print' +# git staging/commit +alias ga="git add" +alias gc="git commit -v" +alias gc!="git commit -v --amend" +alias gcan="git commit -v --amend --no-edit" +alias gcan!="git commit -v --all --no-edit --amend" +alias grs="git restore" + +# git branch/checkout +alias gb="git branch" +alias gco="git checkout" +alias gcm='git checkout $(git_main_branch)' +alias gsw="git switch" + +# git fetch/pull/push +alias gfa="git fetch --all --prune" +alias gl="git pull" +alias gp="git push" +alias gpf="git push --force-with-lease" +alias gpf!="git push --force" +alias gpo="git push origin" + +# git rebase +alias grb="git rebase" +alias grba="git rebase --abort" +alias grbc="git rebase --continue" +alias grbi="git rebase -i" +grbm() { git rebase "$(git_main_branch)" } + +# git merge +alias gm="git merge" +alias gma="git merge --abort" +alias gmc="git merge --continue" + +# git cherry-pick +alias gcp="git cherry-pick" +alias gcpa="git cherry-pick --abort" +alias gcpc="git cherry-pick --continue" + +# git stash/reset +alias gstp="git stash pop" +gros() { git reset "origin/$(git_current_branch)" --soft } # ruff alias ruffme="ruff check --fix && ruff format" - -# clipboard -alias xcp="xclip -selection c" diff --git a/zsh/completions/.keep b/zsh/completions/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/zsh/get-dump b/zsh/get-dump deleted file mode 100755 index 6dd2a63..0000000 --- a/zsh/get-dump +++ /dev/null @@ -1,51 +0,0 @@ -#!/usr/bin/env bash - -function get-dump { - if [[ -z $1 ]]; then - host="$(rg -N --no-heading 'Host .*' ~/.ssh \ - | sed 's/Host \(.*\)/\1/' \ - | fzf --cycle \ - --bind 'tab:toggle-up,btab:toggle-down' \ - --delimiter ':' \ - --with-nth 2 \ - --header "Navigate with ARROW KEYS or TAB/S-TAB. Select with ENTER." \ - --border "double" \ - --border-label "get-dump" \ - --ansi \ - --highlight-line \ - | cut -d ":" -f2 - )" - if [[ -z $host ]]; then - return - fi - else - host=$1 - fi - - selected=$(ssh "$host" docker ps --format "{{.Names}}" \ - | fzf --cycle \ - --bind 'tab:toggle-up,btab:toggle-down' \ - --header "Navigate with ARROW KEYS or TAB/S-TAB. Select with ENTER." \ - --border "double" \ - --border-label "get-dump" \ - --ansi \ - --highlight-line \ - ) - - if [[ -z $selected ]]; then - echo "not provided" - return - fi - - if ! [[ -d "./dumps" ]]; then - mkdir "./dumps" - fi - - location="./dumps/${host}_${selected}_$(date +'%s')" - - echo "dumping to $location" - - ssh "$host" docker exec "$selected" pg_dump -U db --format=c db >"$location" -} - -get-dump "$@" diff --git a/zsh/scripts.zsh b/zsh/scripts.zsh index db488f8..8d3446d 100644 --- a/zsh/scripts.zsh +++ b/zsh/scripts.zsh @@ -1,90 +1,28 @@ -function nvim_ve { - if [[ -e $VIRTUAL_ENV && -f "$VIRTUAL_ENV/bin/activate" ]]; then - source "$VIRTUAL_ENV/bin/activate" - command nvim $@ - deactivate - else - command nvim $@ - fi +# git helpers for aliases +function git_current_branch { + command git rev-parse --abbrev-ref HEAD 2>/dev/null } -function afm { - RES=$(curl -s "https://$1/api/monitoring/requirements") - COUNT=$(echo "$RES" | grep -c $2) - if [[ $COUNT -ge 0 ]]; then - VER=$(echo "$RES" | jq '.[] | select(.name | contains("'"$2"'")).version') - # OP=$(echo "$RES" | jq '.[] | select(.name | contains("'"$2"'")).requirement?[0][0]') - # echo "$2""$(echo "$OP" | tr -d '"')""$(echo "$VER" | tr -d '"')" - echo "${2}==$(echo "$VER" | tr -d '"')" - fi +function git_main_branch { + command git rev-parse --git-dir &>/dev/null || return + local ref + for ref in refs/{heads,remotes/{origin,upstream}}/{main,master,trunk}; do + if command git show-ref -q --verify "$ref"; then + echo ${ref:t} + return 0 + fi + done + echo main } -function nukepip { - pip uninstall $(pip freeze) -y - pip install -r $(pyenv root)/default-packages -} - -function pyinit { - if [[ -f .python-version ]]; then - echo "found .python-version, stopping" - return - fi - - DIRNAME=$(basename "$PWD") - - if [[ $(pyenv versions | grep -E "/${DIRNAME}$") ]]; then - echo "found existing version with this name, setting..." - pyenv local "$DIRNAME" - return - fi - - if [[ -z $1 ]]; then - echo "no python version provided, defaulting to $(pyenv version-name)" - VERSION=$(pyenv version-name) - else - VERSION=$1 - fi - - pyenv virtualenv "$VERSION" "$DIRNAME" - pyenv local "$DIRNAME" - -} - -function lh { - if [[ -z $1 ]]; then - PORT=7999 - else - PORT=$1 - fi - - open "http://localhost:$PORT" -} - -function dps { - if [[ -z $1 ]]; then - docker ps --format table'{{ .ID }}\t{{ .Image }}\t{{ .Ports}}\t{{ .Names }}' - else - docker ps --format table'{{ .ID }}\t{{ .Image }}\t{{ .Ports}}\t{{ .Names }}' | grep $1 - fi -} - -function tssh { - if [[ -z $1 ]]; then - echo "provide ssh host" - return - fi - - ssh -t "$@" "command -v tmux && (tmux a || tmux new-session -s gorazd -c ~) || bash" -} - - function tmux_attach { + local tmux_running tmux_running=$(pgrep tmux) - if ! [[ -z ${TMUX} ]]; then + if [[ -n ${TMUX} ]]; then echo "already attached; refreshing env" source <(tmux show-environment | sed -n 's/^\(.*\)=\(.*\)$/export \1="\2"/p') - return 1 + return 0 fi if [[ -z $tmux_running ]]; then