Major rewrite: switching back to Arch from NixOS

This commit is contained in:
ItsDrike 2024-10-02 14:37:28 +02:00
parent df585b737b
commit 254181c0fc
Signed by: ItsDrike
GPG key ID: FA2745890B7048C0
121 changed files with 5433 additions and 2371 deletions

View file

@ -59,15 +59,12 @@ fi
# Config access shortcuts
alias cfzsh='vim ~/.config/zsh/.zshrc'
alias cfprofile='vim ~/.config/zsh/.zprofile'
alias cfprompt='vim ~/.config/zsh/rc/prompt.zsh'
alias cfalias='vim ~/.config/shell/aliases'
alias cffunctions='vim ~/.config/shell/functions'
alias cfprofile='vim ~/.config/shell/profile'
alias cfenvironment='vim ~/.config/shell/environment'
alias cfenv='cfenvironment'
alias cfhandlers='vim ~/.config/shell/handlers'
alias cfprompt='vim ~/.config/shell/prompt'
alias cfkeybinds='vim ~/.config/shell/keybinds'
alias cftodo='vim ~/Personal/vimwiki/todo.md'
alias cfnvim='vim ~/.config/nvim'
alias cfvim='cfnvim'
alias cfgit='vim ~/.config/git/config'
@ -75,8 +72,6 @@ alias cfhypr='vim ~/.config/hypr'
alias cfsway='vim ~/.config/sway/config'
# zoxide shortcuts
alias cd='z' # use zoxide as the default cd command
alias cdi='zi' # interactive mode (fzf)
alias j='z' # for the sake of old habits from autojump
alias ji='zi' # interactive mode (fzf)
@ -142,11 +137,6 @@ alias iptlistout='iptables -L OUTPUT -n -v --line-numbers' # OUT rules
alias iptlistfw='iptables -L FORWARD -n -v --line-numbers' # FORWARD rules
alias ufw-log='journalctl -f -n 100 -g ufw' # Show UFW log entries in system journal
# Kernel actions
alias kernel-recompile='cd /usr/src/linux && make -j7 && make -j7 modules_install && make install'
alias kernel-oldconfig='cd /usr/src/linux && make oldconfig'
alias kernel-configure='cd /usr/src/linux && make menuconfig'
# System actions
alias sv='systemctl'
alias pacnew="find / -name '*.pacnew' 2>/dev/null" # Search for all new configurations after pacman update
@ -281,9 +271,11 @@ if [ -x /usr/bin/dircolors ]; then
alias egrep='egrep --color=auto'
alias diff='diff --color=auto'
# shellcheck disable=SC2032
alias ip='ip --color=auto'
# Take advantage of $LS_COLORS for completion as well
# shellcheck disable=SC2296
zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"
fi
@ -298,12 +290,6 @@ if [ ! "$(uname -s)" = 'Darwin' ]; then
fi
fi
# Autogenerate python aliases
if [ -f ~/.config/shell/py-alias ]; then
# shellcheck source=/home/itsdrike/.config/shell/py-alias
. "$HOME/.config/shell/py-alias"
fi
# Functions
if [ -f ~/.config/shell/functions ]; then
# shellcheck source=/home/itsdrike/.config/shell/functions

View file

@ -8,6 +8,23 @@
# which means the XDG definitions will be ignored anyway, and
# defining them may break programs when root is actually logged in.
# Add all folders in ~/.local/bin into PATH
# Some window managers require this line to be in profile not in .zshenv
if [ -d "$HOME/.local/bin" ]; then
PATH+=":${$(find -L ~/.local/bin -type d | tr '\n' ':')%%:}"
fi
if [ -d "$HOME/.local/share/npm/bin" ]; then
PATH+=":$HOME/.local/share/npm/bin"
fi
# Prepend rye shims to PATH, to have rye python executables
# take priority over the system-wide python.
if [ -d "${RYE_HOME:-$HOME/.rye}/shims" ]; then
PATH="${RYE_HOME:-$HOME/.rye}/shims:$PATH"
fi
# Define some variables for POSIX compatibility
uid="$(id -u)"
@ -16,14 +33,26 @@ export EDITOR="nvim"
export BROWSER="firefox"
export TERMINAL="alacritty"
export DIFFPROG="nvim -d"
export FILEMANAGER="pcmanfm"
export FILEMANAGER="pcmanfm-qt"
# XDG Standard paths
# XDG Base Directory standard paths
export XDG_CONFIG_HOME="$HOME/.config"
export XDG_CACHE_HOME="$HOME/.cache"
export XDG_DATA_HOME="$HOME/.local/share"
export XDG_RUNTIME_DIR="/run/user/$uid"
export XDG_STATE_HOME="$HOME/.local/state"
export XDG_BIN_HOME="$HOME/.local/bin"
# XDG User directories (should match ~/.config/user-dirs.dirs)
#export XDG_DESKTOP_DIR="$HOME/Desktop"
export XDG_DOWNLOAD_DIR="$HOME/Downloads"
export XDG_TEMPLATES_DIR="$XDG_DATA_HOME/templates"
export XDG_PUBLICSHARE_DIR="$XDG_DATA_HOME/public"
#export XDG_DOCUMENTS_DIR="$HOME/Documents"
export XDG_MUSIC_DIR="$HOME/Media/Music"
export XDG_PICTURES_DIR="$HOME/Media/Pictures"
export XDG_VIDEOS_DIR="$HOME/Media/Videos"
export XDG_SCREENSHOTS_DIR="$HOME/Media/Pictures/Screenshots"
# Per-Application XDG settings
export ZDOTDIR="$XDG_CONFIG_HOME/zsh"
@ -33,7 +62,7 @@ export ZSH_COMPDUMP="$ZSH_CACHE/zcompdump-$ZSH_VERSION"
export XINITRC="$XDG_CONFIG_HOME/x11/xinitrc"
#export XAUTHORITY="$XDG_RUNTIME_DIR/Xauthority" # This line will break some DMs.
export LESSHISTFILE="-"
export GTK2_RC_FILES="$XDG_CONFIG_HOME/gtk-2.0/gtkrc"
export GTK2_RC_FILES="$XDG_CONFIG_HOME/gtk-2.0/gtkrc":"$XDG_CONFIG_HOME/gtk-2.0/gtkrc.mine"
export WGETRC="$XDG_CONFIG_HOME/wget/wgetrc"
export GNUPGHOME="$XDG_DATA_HOME/gnupg"
export GOPATH="$XDG_DATA_HOME/go"
@ -44,22 +73,46 @@ export _JAVA_OPTIONS="-Djava.util.prefs.userRoot=$XDG_CONFIG_HOME/java"
export NPM_CONFIG_USERCONFIG="$XDG_CONFIG_HOME/npm/npmrc"
export NPM_CONFIG_CACHE="$XDG_CACHE_HOME/npm"
export NPM_CONFIG_PREFIX="$XDG_DATA_HOME/npm"
export NPM_CONFIG_TMP="$XDG_RUNTIME_DIR/npm"
export NUGET_PACKAGES="$XDG_CACHE_HOME/NuGetPackages"
export PARALLEL_HOME="$XDG_CONFIG_HOME/parallel"
export RANDFILE="$XDG_CACHE_HOME/rnd"
export PYTHONSTARTUP="$XDG_CONFIG_HOME/python/pythonrc.py"
export PYTHONPYCACHEPREFIX="$XDG_CACHE_HOME/python"
export PYTHONUSERBASE="$XDG_DATA_HOME/python"
# Less commonly used applications
export _ZL_DATA="$XDG_DATA_HOME/zlua"
export SQLITE_HISTORY="$XDG_DATA_HOME/sqlite_history"
export WAKATIME_HOME="$XDG_CONFIG_HOME/wakatime"
export IPYTHONDIR="$XDG_CONFIG_HOME/ipython"
export PYENV_ROOT="$XDG_DATA_HOME/pyenv"
export MYPY_CACHE_DIR="$XDG_CACHE_HOME/mypy"
#export PYLINTHOME="$XDG_CACHE_HOME/pylint"
#export DOCKER_CONFIG="$XDG_CONFIG_HOME/docker"
#export CUDA_CACHE_PATH="$XDG_CACHE_HOME/nv"
export PYLINTHOME="$XDG_CACHE_HOME/pylint"
export DOCKER_CONFIG="$XDG_CONFIG_HOME/docker"
export CUDA_CACHE_PATH="$XDG_CACHE_HOME/nv"
export WINEPREFIX="$XDG_DATA_HOME/wine"
export MPLAYERHOME="$XDG_CONFIG_HOME/mplayer"
export ANDROID_HOME="$XDG_DATA_HOME/android"
export M2_HOME="$XDG_DATA_HOME/m2"
export NODE_REPL_HISTORY="$XDG_DATA_HOME/node_repl_history"
export DOTNET_CLI_HOME="$XDG_DATA_HOME/dotnet"
export JUPYTER_CONFIG_DIR="$XDG_CONFIG_HOME/jupyter"
export RYE_HOME="$XDG_CONFIG_HOME/rye"
# Theming
export GTK_THEME="Tokyonight-Dark"
export GTK_USE_PORTAL=1
export QT_STYLE_OVERRIDE=kvantum
export QT_QPA_PLATFORMTHEME="qt5ct"
export QT_AUTO_SCREEN_SCALE_FACTOR="1"
export QT_QPA_PLATFORM="wayland;xcb"
export QT_WAYLAND_DISABLE_WINDOWDECORATION="1"
export XCURSOR_THEME="BreezeX-RosePine-Linux"
export XCURSOR_SIZE="24"
export HYPRCURSOR_THEME="rose-pine-hyprcursor"
export HYPRCURSOR_SIZE="24"
# Colorful man pages!
# Use neovim to open man pages
@ -84,12 +137,9 @@ fi
# Other program settings
export HISTTIMEFORMAT="%Y-%m-%d %T "
export SUDO_ASKPASS="$HOME/.local/bin/scripts/gui/prompt/menupass"
export PIPENV_VENV_IN_PROJECT=1 # Force pipenv to create new environments within projects ./.venv
export XSECURELOCK_SHOW_HOSTNAME=0 # Don't show hostname in xsecurelock
export XSECURELOCK_SHOW_DATETIME=1 # Show current date and time in xsecurelock
export QT_QPA_PLATFORMTHEME="qt5ct" # Have QT use theme from qt5ct.
#export QT_STYLE_OVERRIDE="gtk2" # Have QT use the gtk2 theme (needs aur/qt5-styleplugins)
#export QSG_RENDER_LOOP=basic # Fix Stremio freezing
export PIPENV_VENV_IN_PROJECT=1 # Force pipenv to create new environments within projects ./.venv
export XSECURELOCK_SHOW_HOSTNAME=0 # Don't show hostname in xsecurelock
export XSECURELOCK_SHOW_DATETIME=1 # Show current date and time in xsecurelock
# Remove irrelevant variables added for posix compatibility
unset posix

View file

@ -202,8 +202,8 @@ anonymize() {
# Reset the prompt on initial run to allow this script
# to be ran multiple times without user having to reload
# PS1 manually
# shellcheck source=/home/itsdrike/.config/shell/prompt
. "${XDG_CONFIG_DIR:-$HOME/.config}/shell/prompt"
# shellcheck source=/home/itsdrike/.config/zsh/rc/prompt.zsh
. "${ZDOTDIR}/rc/prompt.zsh"
# Regular expression to match 0-255 numbers (color)
color_int_re='^(0+)?([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])$'

View file

@ -1,40 +0,0 @@
#!/bin/sh
if command -v pkgfile > /dev/null; then
# Command not found hook that uses `pkgfile` package
# to search through the package index in order to find
# a package which includes given command, which was resolved
# and not found, if there are no such packages, only print
# command not found message
command_not_found_handler() {
cmd="$1"
printf 'zsh: command not found: %s' "$cmd" # print command not found asap, then search for packages
repos="$(pkgfile "$cmd")"
if [ -n "$repos" ]; then
printf '\r%s may be found in the following packages:\n' "$cmd"
echo "$repos" | while read -r pkg; do
printf ' %s\n' "$pkg"
done
else
printf '\n'
fi
return 127
}
elif [ -x /usr/lib/command-not-found ] || [ -x /usr/share/command-not-found/command-not-found ]; then
# Ubuntu handle for bash default command-not-found
# it works similarely to the above arch alternative,
# this is based on the original bash implementation
command_not_found_handler() {
# check because cmd not found could've been removed in the meantime
if [ -x /usr/lib/command-not-found ]; then
/usr/lib/command-not-found -- "$1"
return $?
elif [ -x /usr/share/command-not-found/command-not-found ]; then
/usr/share/command-not-found/command-not-found -- "$1"
return $?
else
printf "%s: command not found\n" "$1" >&2
return 127
fi
}
fi

View file

@ -1,65 +0,0 @@
#!/usr/bin/env zsh
# shellcheck disable=SC2030,SC2031,SC2015
# Set default keybindings (mostly from oh-my-zsh)
# Make sure that the terminal is in application mode when zle is active, since
# only then values from $terminfo are valid
if (( ${+terminfo[smkx]} )) && (( ${+terminfo[rmkx]} )); then
function zle-line-init() {
echoti smkx
}
function zle-line-finish() {
echoti rmkx
}
zle -N zle-line-init
zle -N zle-line-finish
fi
# Use emacs keybindings
bindkey -e
# Start typing + [Up-Arrow] - fuzzy find history forward
if [ -n "${terminfo[kcuu1]}" ]; then
autoload -U up-line-or-beginning-search
zle -N up-line-or-beginning-search
bindkey "${terminfo[kcuu1]}" up-line-or-beginning-search
fi
# Start typing + [Down-Arrow] - fuzzy find history backward
if [ -n "${terminfo[kcud1]}" ]; then
autoload -U down-line-or-beginning-search
zle -N down-line-or-beginning-search
bindkey "${terminfo[kcud1]}" down-line-or-beginning-search
fi
# [Home] - Go to beginning of line
[ -n "${terminfo[khome]}" ] && bindkey "${terminfo[khome]}" beginning-of-line || bindkey "^[[H" beginning-of-line
# [End] - Go to end of line
[ -n "${terminfo[kend]}" ] && bindkey "${terminfo[kend]}" end-of-line || bindkey "^[[F" end-of-line
# [Shift-Tab] - move through the completion menu backwards
[ -n "${terminfo[kcbt]}" ] && bindkey "${terminfo[kcbt]}" reverse-menu-complete
# [Backspace] - delete backward
bindkey '^?' backward-delete-char
# [Delete] - delete forward
[ -n "${terminfo[kdch1]}" ] && bindkey "${terminfo[kdch1]}" delete-char || bindkey "^[[3~" delete-char
# [Ctrl-Delete] - delete whole forward-word
bindkey '^[[3;5~' kill-word
# [Ctrl-RightArrow] - move forward one word
bindkey '^[[1;5C' forward-word
# [Ctrl-LeftArrow] - move backward one word
bindkey '^[[1;5D' backward-word
# [Ctrl-r] - Search backward incrementally for a specified string. The string may begin with ^ to anchor the search to the beginning of the line.
bindkey '^r' history-incremental-search-backward
# [PageUp] - Up a line of history
[ -n "${terminfo[kpp]}" ] && bindkey "${terminfo[kpp]}" up-line-or-history
# [PageDown] - Down a line of history
[ -n "${terminfo[knp]}" ] && bindkey "${terminfo[knp]}" down-line-or-history
# [Space] - do history expansion on space
bindkey ' ' magic-space
# [ctrl+space] Accept suggestion from zsh-autosuggestions plugin
bindkey '^ ' autosuggest-accept

View file

@ -1,30 +0,0 @@
#!/bin/zsh
# User .profile definition.
# This file is only sourced once, after login, Unlike
# .zshrc/.bashrc, which will run whenever a new terminal
# is opened.
# Add all folders in ~/.local/bin into PATH
# Some window managers require this line to be in profile
# not in .zshenv
if [ -d "$HOME/.local/bin" ]; then
PATH+=":${$(find -L ~/.local/bin -type d | tr '\n' ':')%%:}"
fi
if [ -d "$HOME/.local/share/pyenv/shims" ]; then
PATH+=":$HOME/.local/share/pyenv/shims"
fi
if [ -d "$HOME/.local/share/npm/bin" ]; then
PATH+=":$HOME/.local/share/npm/bin"
fi
# Start graphical session automatically on tty1 if Hyprland or startx is available
if [ "$(tty)" = "/dev/tty1" ] && [ "$UID" != 0 ]; then
if command -v Hyprland >/dev/null; then
! pidof -s Hyprland >/dev/null 2>&1 && launch-hypr
elif command -v startx >/dev/null; then
! pidof -s Xorg >/dev/null 2>&1 && exec startx "$XINITRC"
fi
fi

View file

@ -1,187 +0,0 @@
#!/usr/bin/env zsh
# shellcheck disable=SC2155
# Configuration variables:
# Once we are too deep in the filestructure, we can usually afford to shorten
# the whole working directory and only print something like ~/.../dir3/dir4/dir5
# instead of ~/dir1/dir2/dir3/dir4/dir5. If this isn't desired, set this to 0
USE_SHORTENED_WORKDIR=1
# Show how much time it took to run a command
CMD_TIME_SHOW=1
# Minimum units to show the time precision, if
# we use "s" (seconds), and the output took 0s,
# we don't print the output at all to avoid clutter.
# Same goes for any other units, however with "ms"
# (miliseconds), this is very unlikely
# Valid options: ms/s/m/h/d
CMD_TIME_PRECISION="s"
# Minimum time in miliseconds, to print the time took,
# if the command takes less than this amount of miliseconds,
# don't bother printing the time took, this is nice if you
# don't need to see how long commands like 'echo' took
# Setting this to 0 will always print the time taken
CMD_TIME_MINIMUM=100
# hide EOL sign ('%')
export PROMPT_EOL_MARK=""
# TTY (pure linux) terminal only has 8-bit color support
# (unless you change it in kernel), respect this and downgrade
# the color scheme accordingly (it won't look best, but it's
# still better than no colors)
if [ "$TERM" = "linux" ]; then
GREEN="%F{002}"
RED="%F{001}"
ORANGE="%F{003}"
BLUE="%F{004}"
LBLUE="%F{006}"
PURPLE="%F{005}"
else
GREEN="%F{047}"
RED="%F{196}"
ORANGE="%F{214}"
BLUE="%F{027}"
LBLUE="%F{075}"
PURPLE="%F{105}"
fi
RESET="%f"
# Signals git status of CWD repository (if any)
git_prompt() {
ref=$(command git symbolic-ref HEAD 2> /dev/null) || ref=$(command git rev-parse --short HEAD 2> /dev/null) || return 0
echo -n " $ORANGE${ref#refs/heads/}"
if [ -n "$(git status --short 2>/dev/null)" ]; then
echo "$RED+"
fi
}
# Adds @chroot or @ssh
foreign_prompt() {
if [ "$(awk '$5=="/" {print $1}' </proc/1/mountinfo)" != "$(awk '$5=="/" {print $1}' </proc/$$/mountinfo)" ]; then
echo -n "@${ORANGE}chroot"
elif [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then
echo -n "@${ORANGE}ssh"
fi
}
#nd Prints appropriate working directory
working_directory() {
# By default up to 5 directories will be tolerated before shortening
# After we surpass that, first directory (or ~) will be printed together with last 3
# This feature uses special symbol '…', but this isn't aviable when in TTY. Because
# of this, when we are in TTY, we fall back to longer '...'
if [ $USE_SHORTENED_WORKDIR != 1 ]; then
echo -n " $BLUE$~"
elif [ "$TERM" = "linux" ]; then
echo -n " $BLUE%(5~|%-1~/.../%3~|%4~)"
else
echo -n " $BLUE%(5~|%-1~/…/%3~|%4~)"
fi
}
# Execution time tracking hooks, this is unique to zsh, as it can add
# preexec and precmd hooks. We can utilize this to keep track of the
# amount of time it took to run certain command. We store the start time
# within a variable: PROMPT_EXEC_TIME_START, which we then compare and
# unset after the command was finished. In here, we simply set the
# PROMPT_EXEC_TIME_DURATION, which is then used in the actual prompt
# This will only be enabled if SHOW_CMD_TIME is 1.
exec_time_preexec_hook() {
[[ $SHOW_CMD_TIME == 0 ]] && return
PROMPT_EXEC_TIME_START=$(date +%s.%N)
}
exec_time_precmd_hook() {
[[ $SHOW_CMD_TIME == 0 ]] && return
[[ -z $PROMPT_EXEC_TIME_START ]] && return
local PROMPT_EXEC_TIME_STOP="$(date +%s.%N)"
PROMPT_EXEC_TIME_DURATION=$(echo "($PROMPT_EXEC_TIME_STOP - $PROMPT_EXEC_TIME_START) * 1000" | bc -l)
unset PROMPT_EXEC_TIME_START
}
format_time() {
# Do some formatting to get nice time (e.g. 2m 12s) from miliseconds
# $1 is the milisecond amount (int or float)
# $2 is the precision (ms/s/m/h/d)
local T="$1"
local D="$(echo "scale=0;$T/1000/60/60/24" | bc -l)"
local H="$(echo "scale=0;$T/1000/60/60%24" | bc -l)"
local M="$(echo "scale=0;$T/1000/60%60" | bc -l)"
local S="$(echo "scale=0;$T/1000%60" | bc -l)"
local MS="$(echo "scale=0;$T%1000" | bc -l)"
local precision=$2
local out=""
case "$precision" in
"ms") [[ "$MS" -gt 0 ]] && out="$(printf "%dms" "$MS") ${out}"; precision="s" ;&
"s") [[ "$S" -gt 0 ]] && out="$(printf "%ds" "$S") ${out}"; precision="m" ;&
"m") [[ "$M" -gt 0 ]] && out="$(printf "%dm" "$M") ${out}"; precision="h" ;&
"h") [[ "$H" -gt 0 ]] && out="$(printf "%dh" "$H") ${out}"; precision="d" ;&
"d") [[ "$D" -gt 0 ]] && out="$(printf "%dd" "$D") ${out}" ;;
*) out="$T" ;; # Return $1 ($T) if precision wasn't specified/valid
esac
printf "%s" "$out"
}
display_cmd_time() {
[[ $CMD_TIME_SHOW == 0 ]] && return
[[ -z $PROMPT_EXEC_TIME_DURATION ]] && return
# If the time duration is less than minimum time,
# don't print the time taken
[[ $PROMPT_EXEC_TIME_DURATION -lt $CMD_TIME_MINIMUM ]] && return
local time_took="$(format_time "$PROMPT_EXEC_TIME_DURATION" "$CMD_TIME_PRECISION")"
# Don't display if the time didn't give us output
# this happens when all fields (seconds/minutes/...) are 0,
# if we use milisecond precision, this will likely never happen
# but with other precisions, it could
[ ${#time_took} -eq 0 ] && return
echo -n " ${LBLUE}took ${time_took}"
}
setopt promptsubst # enable command substitution in prompt
# Setup ZSH hooks to display the running time of commands
autoload -Uz add-zsh-hook
add-zsh-hook preexec exec_time_preexec_hook
add-zsh-hook precmd exec_time_precmd_hook
# Primary Prompt
[ "$EUID" -eq 0 ] && PS1="$RED%n$RESET" || PS1="$GREEN%n$RESET" # user
PS1+="$(foreign_prompt)"
PS1+="$(working_directory)"
PS1+="\$(git_prompt)"
PS1+="\$(display_cmd_time)"
PS1+=" $PURPLE%(!.#.$)$RESET " # Final symbol (# or $)
# Next line prompt
PS2="$RED\ $RESET"
# Right side prompt
RPS1=""
if [ "$TERM" = "linux" ]; then
# Displaying cmd time here works, but often causes issues when we
# resize the terminal, since right prompts can be annoying to deal
# with when resizing. This would run relatively often so it makes
# more sense to only use it in PS1 (left prompt), but if desired,
# this can be uncommented
#RPS1+="\$(display_cmd_time)"
# If we find a non-zero return code, print it in the right prompt,
# use X here, to avoid issues with TTY not having support for
# a nicer unicode character that we use otherwise ("↵")
RPS1+="%(?..${RED}%? X$RESET)"
else
# Read comments for the section above.
#RPS1+="\$(display_cmd_time)"
# NOTE: "↵" symbol could cause issues with on some terminals/machines that
# don't handle unicode well, this issue could be very confusing to debug,
# and it would not be apparent what's wrong since the symbol itself will
# be drawn, however when it is drawn, it will also move the cursor line
# 2 places back since this symbol is made up of 3 bytes (in unicode) and
# regular ASCII characters only take up 1 byte, this means that whenever
# the right-side prompt appears (on error), the prompt would have this issue.
RPS1="%(?..${RED}%? ↵$RESET)"
fi

View file

@ -1,196 +0,0 @@
#!/usr/bin/env zsh
# Simple script which automatically defines certain aliases for python,
# which will automatically use certain python version
# Versions are automatically obtained from $PYENV_ROOT/versions directories
# NOTE: This assumes that all folders in this directory are valid python versions
#
# Assume we have these installed pyenv python versions 3.6.5, 3.6.12 and 3.10.1:
# - Set full-version aliases: py3.6.5, py3.6.12 and py3.10.1
# - Set py3 to 3.10.1 (latest with major version 3)
# - Set py3.6 to 3.6.12 (latest with major version 3 and minor version 6)
# - Set py3.10 to 3.10.1 (latest, and only python with major version 3 and minor version 10)
# Define all wanted aliases for a given python version
# $1 - full valid pyenv python version (for example '3.6.12', `3.11-dev`, or `pypy3.6-7.2.0-src`)
# $2 - version used in the alias (for example '3.6', '3', or even '', but also `pypy3.7`, ...)
define_aliases() {
version="$1"
alias_version="$2"
cmd_prefix="PYENV_VERSION=$version"
alias "py$alias_version=$cmd_prefix python"
alias "ipy$alias_version=$cmd_prefix ipython"
alias "bpy$alias_version=$cmd_prefix bpython"
alias "pydoc$alias_version=$cmd_prefix pydoc"
alias "pytest$alias_version=$cmd_prefix pytest"
alias -g "PY$alias_version=$cmd_prefix"
}
# Handle splitting full version into prefix, version number and suffix
# Because of the huge variaty of python implemenations and their different namings,
# this function will only be able to handle the default CPython version names,
# which follow the regex pattern of: '\d+\.\d+\.\d+', the rest will print 'full_version;;'
# In the future, this may also include support for some other naming schemes.
# $1 - full valid pyenv python version (for example '3.6.12', `3.11-dev`, or `pypy3.6-7.2.0-src`)
parse_python_version() {
full_version="$1"
if echo "$full_version" | grep -E "[0-9]+\.[0-9]+\.[0-9]+" >/dev/null; then
echo ";$full_version;"
else
echo ';;'
fi
}
# Prints version number extracted from alias for given version
# $1 - version used in the alias (for example '3.6', '3', or even '', but also 'pypy3.6', ...)
get_alias_version() {
alias_version="$1"
definition="$(alias "py$alias_version")"
full_version="$(echo "$definition" | cut -d= -f3 | cut -d' ' -f1)"
version_info="$(parse_python_version "$full_version")"
version="$(echo "$version_info" | cut -d';' -f2)"
echo "$version"
}
# Compares 2 python versions in major, minor and micro parts
# $1 - version #1
# $2 - version #2
# Returns:
# 0 - version #1 is newer
# 1 - version #2 is newer
# 2 - versions are equal
version_compare() {
version_1="$1"
version_2="$2"
# ZSH Only:
version_1=("${(@s:.:)version_1}")
version_2=("${(@s:.:)version_2}")
major_1=$version_1[1]
major_2=$version_2[1]
minor_1=$version_1[2]
minor_2=$version_2[2]
micro_1=$version_1[3]
micro_2=$version_2[3]
# POSIX, but slow:
# major_1="$(echo "$version_1" | cut -d. -f1)"
# major_2="$(echo "$version_2" | cut -d. -f1)"
# minor_1="$(echo "$version_1" | cut -d. -f2)"
# minor_2="$(echo "$version_2" | cut -d. -f2)"
# micro_1="$(echo "$version_1" | cut -d. -f3)"
# micro_2="$(echo "$version_2" | cut -d. -f3)"
# Compare majors
if [ $major_1 -gt $major_2 ]; then
# version 1's major is bigger, version 1 is newer
return 0
elif [ $major_1 -lt $major_2 ]; then
# version 1's major is smaller, version 2 is newer
return 1
fi
# Majors equal, compare minors
if [ $minor_1 -gt $minor_2 ]; then
# version 1's minor is bigger, version 1 is newer
return 0
elif [ $minor_1 -lt $minor_2 ]; then
# version 1's major is smaller, version 2 is newer
return 1
fi
# Minors equal, compare micros
if [ $micro_1 -gt $micro_2 ]; then
# version 1's micro is bigger, version 1 is newer
return 0
elif [ $micro_1 -lt $micro_2 ]; then
# version 1's micro is smaller, version 2 is newer
return 1
fi
# Micros equal, versions equal
return 2
}
# Define new aliases if they don't already exsist, in which case override
# if the current version is newer than the version in the alias
# $1 - full valid pyenv python version (for example '3.6.12', `3.11-dev`, or `pypy3.6-7.2.0-src`)
# $2 - version used in the alias (for example '3.6', '3', or even '', but also `pypy3.7`, ...)
try_define_aliases() {
version="$1"
alias_version="$2"
# Check if alias already exists
if alias "py$alias_version" >/dev/null; then
# Compare version from the existing alias with current version,
# if current is newer, override the existing alias(es)
defined_version="$(get_alias_version "$alias_version")"
if version_compare "$version" "$defined_version"; then
define_aliases "$version" "$alias_version"
# echo "Overwrote '$alias_version' aliases to point to '$version'"
return 0
else
return 1
fi
fi
# The aliases aren't already defined, it's safe to create them
define_aliases "$version" "$alias_version"
# echo "Made '$alias_version' aliases pointing to '$version'"
return 0
}
define_version_aliases() {
prefix="$1"
version="$2"
suffix="$3"
# ZSH only:
version_data=("${(@s:.:)version}")
major_version=$version_data[1]
minor_version=$version_data[2]
# POSIX, but slow:
# major_version="$(echo "$version" | cut -d. -f1)"
# minor_version="$(echo "$version" | cut -d. -f2)"
# Define the major.minor.micro (full) alias
try_define_aliases "$version" "$prefix$version$suffix"
# Define the major.minor alias
try_define_aliases "$version" "$prefix$major_version.$minor_version$suffix"
# Define the major alias
try_define_aliases "$version" "$prefix$major_version$suffix"
# Define top level alias
try_define_aliases "$version" "$prefix$suffix"
}
for python_dir in "$PYENV_ROOT"/versions/*/ ; do
full_version="$(basename $python_dir)"
version_info="$(parse_python_version "$full_version")"
if [ $version_info = ';;' ]; then
# Version info wasn't obtained successfully, skip this version
echo "Skipped $full_version"
continue
fi
# ZSH only:
version_data=("${(@s:;:)version_info}")
prefix=$version_data[1]
version=$version_data[2]
suffix=$version_data[3]
# POSIX, but slow:
# prefix="$(echo "$version_info" | cut -d';' -f1)"
# version="$(echo "$version_info" | cut -d';' -f2)"
# suffix="$(echo "$version_info" | cut -d';' -f3)"
# startTime=$(date +%N)
define_version_aliases "$prefix" "$version" "$suffix"
# endTime=$(date +%N)
# nanos="$(expr $endTime - $startTime)"
# echo "took $(expr $nanos / 1000000) miliseconds"
done
if command -v poetry >/dev/null 2>&1; then
alias poetry-pyenv='poetry env use "$(pyenv which python)" && poetry install'
fi