From e6f3aae287687d9d83c3e493e808fa832568347f Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Mon, 2 Oct 2023 08:39:07 +0300 Subject: [PATCH] fix(shell): rework shell config for more stability --- base/bashrc | 13 +- base/zshrc | 36 +++- config/alias | 71 ++++++- config/alias-tunkki | 4 - config/exports | 8 - config/exports-apps | 9 +- config/exports-lakka | 2 +- config/exports-shell | 16 +- config/functions | 70 ------- config/nvim/lazy-lock.json | 2 +- local/bin/dfm | 5 +- local/bin/x-dc | 16 ++ local/bin/x-load-configs | 8 +- scripts/install-cheat-purebashbible.sh | 1 - scripts/install-cheat-tldr.sh | 1 - scripts/shared.sh | 258 ++++++++++++++----------- 16 files changed, 294 insertions(+), 226 deletions(-) delete mode 100755 config/alias-tunkki create mode 100755 local/bin/x-dc diff --git a/base/bashrc b/base/bashrc index 811a361..cf021cb 100755 --- a/base/bashrc +++ b/base/bashrc @@ -3,11 +3,20 @@ # Defaults export DOTFILES="$HOME/.dotfiles" +export PATH="$HOME/.local/bin:$HOME/.dotfiles/local/bin:$PATH" # shellcheck source=scripts/shared.sh source "$DOTFILES/scripts/shared.sh" -path_prepend "$DOTFILES/local/bin" -$DOTFILES/local/bin/x-load-configs +# Explicitly set XDG folders +# https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html +export XDG_CONFIG_HOME="$HOME/.config" +export XDG_DATA_HOME="$HOME/.local/share" +export XDG_STATE_HOME="$HOME/.local/state" + +# custom variables +export XDG_BIN_HOME="$HOME/.local/bin" +export XDG_CACHE_HOME="$HOME/.cache" +export XDG_RUNTIME_DIR="$HOME/.local/run" # Import ssh keys in keychain ssh-add -A 2>/dev/null diff --git a/base/zshrc b/base/zshrc index a47fdc7..f8032a4 100755 --- a/base/zshrc +++ b/base/zshrc @@ -5,11 +5,37 @@ export DOTFILES="$HOME/.dotfiles" # shellcheck source=scripts/shared.sh source "$DOTFILES/scripts/shared.sh" -path_prepend "$DOTFILES/local/bin" +export PATH="$HOME/.local/bin:$HOME/.dotfiles/local/bin:$PATH" + +# Explicitly set XDG folders +# https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html +export XDG_CONFIG_HOME="$HOME/.config" +export XDG_DATA_HOME="$HOME/.local/share" +export XDG_STATE_HOME="$HOME/.local/state" + +# custom variables +export XDG_BIN_HOME="$HOME/.local/bin" +export XDG_CACHE_HOME="$HOME/.cache" +export XDG_RUNTIME_DIR="$HOME/.local/run" export COMPLETION_WAITING_DOTS=true -$DOTFILES/local/bin/x-load-configs +path_append "/opt/homebrew/bin" +if type brew &> /dev/null; then + eval "$(brew shellenv)" + FPATH="$HOMEBREW_PREFIX/share/zsh/site-functions:${FPATH}" +fi + +path_append "/usr/local/bin" +path_prepend "$XDG_DATA_HOME/cargo/bin" +path_prepend "$XDG_BIN_HOME" +path_prepend "$HOME/.local/go/bin" +path_prepend "$XDG_DATA_HOME/bob/nvim-bin" + +source "$DOTFILES/config/exports-shell" +source "$DOTFILES/config/exports-apps" +source "$DOTFILES/config/alias" +source "$DOTFILES/config/functions" export ZSH_CUSTOM_COMPLETION_PATH="$XDG_CONFIG_HOME/zsh/completion" x-dc "$ZSH_CUSTOM_COMPLETION_PATH" @@ -17,10 +43,6 @@ x-dc "$ZSH_CUSTOM_COMPLETION_PATH" # Add completion scripts to zsh path FPATH="$ZSH_CUSTOM_COMPLETION_PATH:$FPATH" -if type brew &> /dev/null; then - FPATH="$HOMEBREW_PREFIX/share/zsh/site-functions:${FPATH}" -fi - # Try to load antigen, if present ANTIGEN_ZSH_PATH="$XDG_BIN_HOME/antigen.zsh" # shellcheck source=../../.local/bin/antigen.zsh @@ -29,7 +51,7 @@ ANTIGEN_ZSH_PATH="$XDG_BIN_HOME/antigen.zsh" # antigen is present antigen use oh-my-zsh -export ZSH_TMUX_AUTOSTART=false +export ZSH_TMUX_AUTOSTART=true export ZSH_TMUX_CONFIG="$DOTFILES/config/tmux/tmux.conf" export ZSH_TMUX_UNICODE=true export ZSH_TMUX_AUTOQUIT=false diff --git a/config/alias b/config/alias index 1db84f5..afe7eec 100755 --- a/config/alias +++ b/config/alias @@ -1,7 +1,7 @@ #!/usr/bin/env bash # shellcheck source="../scripts/shared.sh" # shellcheck disable=1091,2139 -source "$DOTFILES/scripts/shared.sh" +DOTFILES="$HOME/.dotfiles" # Get installed php versions from brew and setup aliases function x-set-php-aliases @@ -106,9 +106,6 @@ alias updatedb="sudo /usr/libexec/locate.updatedb" # Always return full history alias history="history 1" -alias pip="pip3 " -alias python="python3 " - # tmux: automatically attach or create session with name 'main' alias tmux='tmux new-session -A -s main' # tmux: attach or create new session @@ -149,3 +146,69 @@ fi alias wget=wget --hsts-file="$XDG_DATA_HOME/wget-hsts" alias svn="svn --config-dir $XDG_CONFIG_HOME/subversion" + +# Alacritty preexec hook to update dynamic title +preexec() +{ + print -Pn "\e]0;$1%~\a" +} + +# Update dotfiles +dfu() +{ + ( + cd "$DOTFILES" && git pull --ff-only && ./install -q + ) +} + +# Weather in Tampere, or other city +weather() +{ + # https://github.com/chubin/wttr.in#usage + local city="${1:-Tampere}" + curl "http://wttr.in/${city// /+}?2nFQM&lang=fi" +} + +# Docker +ssh-docker() +{ + docker exec -it "$@" bash +} + +# All the dig info +digga() +{ + dig +nocmd "$1" any +multiline +noall +answer +} + +# Rector project to php version 8.2 by default. +rector() +{ + local php="${1:-82}" + docker run -v "$(pwd)":/project rector/rector:latest process \ + "/project/$1" \ + --set "php${php}" \ + --autoload-file /project/vendor/autoload.php +} + +# Commit everything +commit() +{ + commitMessage="$*" + + if [ "$commitMessage" = "" ]; then + commitMessage="Automated commit" + fi + + git add . + eval "git commit -a -m '${commitMessage}'" +} + +scheduler() +{ + while :; do + php artisan schedule:run + echo "Sleeping 60 seconds..." + sleep 60 + done +} diff --git a/config/alias-tunkki b/config/alias-tunkki deleted file mode 100755 index 19ef70f..0000000 --- a/config/alias-tunkki +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env bash -# Setting aliases for tunkki - -alias nvim="/snap/nvim/current/usr/bin/nvim" diff --git a/config/exports b/config/exports index b3c0d51..f923e2a 100755 --- a/config/exports +++ b/config/exports @@ -3,8 +3,6 @@ # vim: filetype=zsh export DOTFILES="$HOME/.dotfiles" -source "$DOTFILES/scripts/shared.sh" -path_prepend "$DOTFILES/local/bin" # Explicitly set XDG folders # https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html @@ -17,12 +15,6 @@ export XDG_BIN_HOME="$HOME/.local/bin" export XDG_CACHE_HOME="$HOME/.cache" export XDG_RUNTIME_DIR="$HOME/.local/run" -path_append "/opt/homebrew/bin" -have brew && eval "$(brew shellenv)" - -path_append "/usr/local/bin" -path_prepend "$XDG_BIN_HOME" - source "$DOTFILES/config/exports-shell" source "$DOTFILES/config/exports-apps" diff --git a/config/exports-apps b/config/exports-apps index 3abee89..687c58a 100755 --- a/config/exports-apps +++ b/config/exports-apps @@ -4,9 +4,6 @@ # shellcheck disable=1091,2139 # vim: filetype=bash -: "${DOTFILES:=$HOME/.dotfiles}" -source "$DOTFILES/scripts/shared.sh" - # Antigen configuration # https://github.com/zsh-users/antigen/wiki/Configuration export ADOTDIR="$XDG_DATA_HOME/antigen" @@ -24,10 +21,8 @@ x-dc "$ANSIBLE_GALAXY_CACHE_DIR" export ANDROID_HOME="$XDG_DATA_HOME/android" # bob manages nvim versions -have bob && { - path_prepend "$XDG_DATA_HOME/bob/nvim-bin" - have nvim && export EDITOR="nvim" -} +path_prepend "$XDG_DATA_HOME/bob/nvim-bin" +have nvim && export EDITOR="nvim" export HOMEBREW_NO_ENV_HINTS=true diff --git a/config/exports-lakka b/config/exports-lakka index e49962c..95d5ed3 100644 --- a/config/exports-lakka +++ b/config/exports-lakka @@ -3,7 +3,7 @@ ## LUA have luarocks && $(luarocks path --bin) -path_append "$HOME/.local/go/bin" +path_prepend "$HOME/.local/go/bin" alias logrotate='/usr/sbin/logrotate -s $HOME/logs/state' alias nano='nano -wS -$' diff --git a/config/exports-shell b/config/exports-shell index 0349d1d..7977d8a 100755 --- a/config/exports-shell +++ b/config/exports-shell @@ -4,7 +4,17 @@ # vim: filetype=bash export DOTFILES="$HOME/.dotfiles" -source "$DOTFILES/scripts/shared.sh" + +# Explicitly set XDG folders +# https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html +export XDG_CONFIG_HOME="$HOME/.config" +export XDG_DATA_HOME="$HOME/.local/share" +export XDG_STATE_HOME="$HOME/.local/state" + +# custom variables +export XDG_BIN_HOME="$HOME/.local/bin" +export XDG_CACHE_HOME="$HOME/.cache" +export XDG_RUNTIME_DIR="$HOME/.local/run" export LC_ALL=fi_FI.UTF-8 @@ -60,10 +70,6 @@ if [ "$DOTFILES_CURRENT_SHELL" = "-zsh" ]; then zstyle ':completion:*' cache-path "$XDG_CACHE_HOME/zsh/zcompcache" zstyle ':completion:*' list-colors "$LS_COLORS" - # fzf - [ -f "${XDG_CONFIG_HOME:-$HOME/.config}"/fzf/fzf.zsh ] \ - && source "${XDG_CONFIG_HOME:-$HOME/.config}"/fzf/fzf.zsh - # Load iterm2 shell integration # https://iterm2.com/documentation-shell-integration.html [[ -f "$XDG_BIN_HOME/iterm2_shell_integration.zsh" ]] \ diff --git a/config/functions b/config/functions index 4a5dc03..a35326f 100755 --- a/config/functions +++ b/config/functions @@ -1,73 +1,3 @@ #!/usr/bin/env bash # # shell functions -# -# shellcheck source="../scripts/shared.sh" -export DOTFILES="$HOME/.dotfiles" -source "$DOTFILES/scripts/shared.sh" - -# Alacritty preexec hook to update dynamic title -preexec() -{ - print -Pn "\e]0;$1%~\a" -} - -# Update dotfiles -dfu() -{ - ( - cd "$DOTFILES" && git pull --ff-only && ./install -q - ) -} - -# Weather in Tampere, or other city -weather() -{ - # https://github.com/chubin/wttr.in#usage - local city="${1:-Tampere}" - curl "http://wttr.in/${city// /+}?2nFQM&lang=fi" -} - -# Docker -ssh-docker() -{ - docker exec -it "$@" bash -} - -# All the dig info -digga() -{ - dig +nocmd "$1" any +multiline +noall +answer -} - -# Rector project to php version 8.2 by default. -rector() -{ - local php="${1:-82}" - docker run -v "$(pwd)":/project rector/rector:latest process \ - "/project/$1" \ - --set "php${php}" \ - --autoload-file /project/vendor/autoload.php -} - -# Commit everything -commit() -{ - commitMessage="$*" - - if [ "$commitMessage" = "" ]; then - commitMessage="Automated commit" - fi - - git add . - eval "git commit -a -m '${commitMessage}'" -} - -scheduler() -{ - while :; do - php artisan schedule:run - echo "Sleeping 60 seconds..." - sleep 60 - done -} diff --git a/config/nvim/lazy-lock.json b/config/nvim/lazy-lock.json index ba6c470..b482b4e 100644 --- a/config/nvim/lazy-lock.json +++ b/config/nvim/lazy-lock.json @@ -6,7 +6,7 @@ "animation.nvim": { "branch": "main", "commit": "fb77091ab72ec9971aee0562e7081182527aaa6a" }, "bufdelete.nvim": { "branch": "master", "commit": "07d1f8ba79dec59d42b975a4df1c732b2e4e37b4" }, "bufferline.nvim": { "branch": "main", "commit": "6ecd37e0fa8b156099daedd2191130e083fb1490" }, - "catppuccin": { "branch": "main", "commit": "3d9a5ed556e289bce6c1fb0af89ec838360641b2" }, + "catppuccin": { "branch": "main", "commit": "18267654c665310c665d3b7c6bc43d5f5ea5e410" }, "cmp-buffer": { "branch": "main", "commit": "3022dbc9166796b644a841a02de8dd1cc1d311fa" }, "cmp-emoji": { "branch": "main", "commit": "19075c36d5820253d32e2478b6aaf3734aeaafa0" }, "cmp-nvim-lsp": { "branch": "main", "commit": "44b16d11215dce86f253ce0c30949813c0a90765" }, diff --git a/local/bin/dfm b/local/bin/dfm index ffe622f..08a7845 100755 --- a/local/bin/dfm +++ b/local/bin/dfm @@ -9,7 +9,6 @@ # Helper variables, override with ENVs like `VERBOSE=1 dfm help` : "${VERBOSE:=0}" : "${DOTFILES:=$HOME/.dotfiles}" -: "${SHARED_SCRIPT:=$DOTFILES/scripts/shared.sh}" : "${BREWFILE:=$DOTFILES/config/homebrew/Brewfile}" : "${HOSTFILES:=$DOTFILES/hosts}" @@ -17,9 +16,9 @@ SCRIPT=$(basename "$0") VERSION_NVM="v0.39.5" -export DOTFILES_SHARED_LOADED="" +export DOTFILES="$HOME/.dotfiles" # shellcheck source=./../../scripts/shared.sh -source "$SHARED_SCRIPT" +source "$HOME/.dotfiles/scripts/shared.sh" # Loads configs for better installation experience x-load-configs diff --git a/local/bin/x-dc b/local/bin/x-dc new file mode 100755 index 0000000..fc9f174 --- /dev/null +++ b/local/bin/x-dc @@ -0,0 +1,16 @@ +#!/usr/bin/env bash +# Create directory if it doesn't exist already +# +# Copyright (c) 2023 Ismo Vuorinen. All Rights Reserved. +# Licensed under MIT License. http://www.opensource.org/licenses/mit-license. + +dir="$1" + +[ $# -eq 0 ] && { + echo "Usage: $0 full/path/to/dir/to/create" + exit 1 +} + +if [ ! -d "$dir" ]; then + mkdir -p "$dir" && exit 0 +fi diff --git a/local/bin/x-load-configs b/local/bin/x-load-configs index 9eea63e..563a10f 100755 --- a/local/bin/x-load-configs +++ b/local/bin/x-load-configs @@ -2,13 +2,13 @@ # Load our configuration files # Copyright (c) 2023 Ismo Vuorinen. All Rights Reserved. -: "${DOTFILES:=$HOME/.dotfiles}" -source "$DOTFILES/scripts/shared.sh" +DOTFILES="$HOME/.dotfiles" +source "$HOME/.dotfiles/scripts/shared.sh" CONFIG_PATH="$DOTFILES/config" # Load the shell dotfiles, and then some: -function load() +function x-load-config-fn() { for FILE in $CONFIG_PATH/{exports,alias,functions}; do FILENAME="$FILE" @@ -28,4 +28,4 @@ function load() done } -load +x-load-config-fn diff --git a/scripts/install-cheat-purebashbible.sh b/scripts/install-cheat-purebashbible.sh index 6332790..b567a93 100755 --- a/scripts/install-cheat-purebashbible.sh +++ b/scripts/install-cheat-purebashbible.sh @@ -1,7 +1,6 @@ #!/usr/bin/env bash # shellcheck disable=SC2231,SC2034,SC2181,SC2068 # shellcheck source=shared.sh -DOTFILES_SHARED_LOADED="" source "$HOME/.dotfiles/scripts/shared.sh" PBB_REQUIRED_TOOLS=(git cheat) diff --git a/scripts/install-cheat-tldr.sh b/scripts/install-cheat-tldr.sh index 2a9b86c..b749616 100755 --- a/scripts/install-cheat-tldr.sh +++ b/scripts/install-cheat-tldr.sh @@ -1,7 +1,6 @@ #!/usr/bin/env bash # shellcheck disable=SC2231,SC2034,SC2181,SC2068 # shellcheck source=shared.sh -DOTFILES_SHARED_LOADED="" source "$HOME/.dotfiles/scripts/shared.sh" TLDR_REQUIRED_TOOLS=(git cheat) diff --git a/scripts/shared.sh b/scripts/shared.sh index f9ce3be..edb9563 100755 --- a/scripts/shared.sh +++ b/scripts/shared.sh @@ -5,11 +5,160 @@ # Helper env variables. Use like this: VERBOSE=1 ./script.sh : "${VERBOSE:=0}" -: "${DOTFILES:=$HOME/.dotfiles}" +# Modified from https://stackoverflow.com/a/28776166 +( + [[ -n $ZSH_VERSION && $ZSH_EVAL_CONTEXT =~ :file$ ]] \ + || [[ -n $BASH_VERSION ]] && (return 0 2> /dev/null) +) && sourced=1 || sourced=0 + +export DOTFILES="$HOME/.dotfiles" DOTFILES_CURRENT_SHELL=$(ps -p $$ -oargs=) export DOTFILES_CURRENT_SHELL +# Explicitly set XDG folders +# https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html +export XDG_CONFIG_HOME="$HOME/.config" +export XDG_DATA_HOME="$HOME/.local/share" +export XDG_STATE_HOME="$HOME/.local/state" + +# custom variables +export XDG_BIN_HOME="$HOME/.local/bin" +export XDG_CACHE_HOME="$HOME/.cache" +export XDG_RUNTIME_DIR="$HOME/.local/run" + +# Remove directory from the PATH variable +# usage: path_remove ~/.local/bin +function path_remove +{ + PATH=$(echo -n "$PATH" | awk -v RS=: -v ORS=: "\$0 != \"$1\"" | sed 's/:$//') +} + +# Append directory to the PATH +# usage: path_append ~/.local/bin +function path_append +{ + path_remove "$1" + PATH="${PATH:+"$PATH:"}$1" +} + +# Prepend directory to the PATH +# usage: path_prepend ~/.local/bin +function path_prepend +{ + path_remove "$1" + PATH="$1${PATH:+":$PATH"}" +} + +# Create directory if it doesn't exist already +x-dc() +{ + dir="$1" + + [ $# -eq 0 ] && { + echo "Usage: $0 full/path/to/dir/to/create" + exit 1 + } + + if [ ! -d "$dir" ]; then + mkdir -p "$dir" && exit 0 + fi +} + +# Create a new directory and enter it +mkd() +{ + mkdir -p "$@" && cd "$@" || exit +} + +# Run command silently +# Usage: silent uptime +silent() +{ + "$@" >&/dev/null +} + +# Check if a file contains non-ascii characters +nonascii() +{ + LC_ALL=C grep -n '[^[:print:][:space:]]' "${@}" +} + +# Cache commands using bkt if installed +if command -v bkt >&/dev/null; then + bkt() + { + command bkt --cache-dir="$XDG_CACHE_HOME/bkt" "$@" + } +else + # If bkt isn't installed skip its arguments and just execute directly. + # Optionally write a msg to stderr suggesting users install bkt. + bkt() + { + while [[ "$1" == --* ]]; do shift; done + "$@" + } +fi + +# shorthand for checking if the system has the bin in path, +# this version does not use caching +# usage: have_command php && php -v +function have_command +{ + command -v "$1" >&/dev/null +} + +# shorthand for checking if the system has the bin in path, +# this version uses caching +# usage: have php && php -v +function have +{ + bkt -- which "$1" >&/dev/null +} + +function brew_installed +{ + bkt -- brew list +} + +# shorthand for checking if brew package is installed +# usage: have_brew php && php -v +function have_brew +{ + ! have brew && return 125 + + if bkt -- brew list "$1" &> /dev/null; then + return 0 + else + return 1 + fi +} + +CONFIG_PATH="$DOTFILES/config" + +# Load the shell dotfiles, and then some: +function x-load-config-fn() +{ + for FILE in $CONFIG_PATH/{exports,alias,functions}; do + FILENAME="$FILE" + HOST="$(hostname -s)" + # global (exports|alias|functions) FILENAME for all hosts + # shellcheck source=../config/exports + [ -r "$FILENAME" ] && source "$FILENAME" + # global secret FILENAME, git ignored + # shellcheck source=../config/exports-secret + [ -r "$FILENAME-secret" ] && source "$FILENAME-secret" + # host specific (exports|alias|functions) FILENAME + # shellcheck source=../config/exports + [ -r "$FILENAME-$HOST" ] && source "$FILENAME-$HOST" + # host specific (exports|alias|functions) FILENAME, git ignored + # shellcheck source=../config/exports + [ -r "$FILENAME-$HOST-secret" ] && source "$FILENAME-$HOST-secret" + done +} + +x-load-config-fn + source "$DOTFILES/local/bin/msgr" # -- Menu builder -- # @@ -67,79 +216,6 @@ function menu_usage() done } -# Cache commands using bkt if installed -if command -v bkt >&/dev/null; then - bkt() - { - command bkt --cache-dir="$XDG_CACHE_HOME/bkt" "$@" - } -else - # If bkt isn't installed skip its arguments and just execute directly. - # Optionally write a msg to stderr suggesting users install bkt. - bkt() - { - while [[ "$1" == --* ]]; do shift; done - "$@" - } -fi - -# shorthand for checking if the system has the bin in path, -# this version does not use caching -# usage: have_command php && php -v -function have_command -{ - command -v "$1" >&/dev/null -} - -# shorthand for checking if the system has the bin in path, -# this version uses caching -# usage: have php && php -v -function have -{ - bkt -- which "$1" >&/dev/null -} - -function brew_installed -{ - bkt -- brew list -} - -# shorthand for checking if brew package is installed -# usage: have_brew php && php -v -function have_brew -{ - ! have brew && return 125 - - if bkt -- brew list "$1" &> /dev/null; then - return 0 - else - return 1 - fi -} - -# Remove directory from the PATH variable -# usage: path_remove ~/.local/bin -function path_remove -{ - PATH=$(echo -n "$PATH" | awk -v RS=: -v ORS=: "\$0 != \"$1\"" | sed 's/:$//') -} - -# Append directory to the PATH -# usage: path_append ~/.local/bin -function path_append -{ - path_remove "$1" - PATH="${PATH:+"$PATH:"}$1" -} - -# Prepend directory to the PATH -# usage: path_prepend ~/.local/bin -function path_prepend -{ - path_remove "$1" - PATH="$1${PATH:+":$PATH"}" -} - # Creates a random string rnd() { @@ -194,37 +270,3 @@ function replacable() return 1 } - -# Create directory if it doesn't exist already -x-dc() -{ - dir="$1" - - [ $# -eq 0 ] && { - echo "Usage: $0 full/path/to/dir/to/create" - exit 1 - } - - if [ ! -d "$dir" ]; then - mkdir -p "$dir" && exit 0 - fi -} - -# Create a new directory and enter it -mkd() -{ - mkdir -p "$@" && cd "$@" || exit -} - -# Run command silently -# Usage: silent uptime -silent() -{ - "$@" >&/dev/null -} - -# Check if a file contains non-ascii characters -nonascii() -{ - LC_ALL=C grep -n '[^[:print:][:space:]]' "${@}" -}