dynamic aliases

This commit is contained in:
Piotr Domański 2024-02-21 13:06:01 +01:00
parent 016450ecbb
commit 7ab614d730
8 changed files with 189 additions and 1 deletions

16
.config/aliasrc/docker Normal file
View file

@ -0,0 +1,16 @@
# docker
if [ -x "$(which docker)" ]; then
alias dc='docker-compose'
alias killdocker='docker kill $(docker ps -q)'
alias cleardocker='docker system prune && docker volume prune'
alias balancedocker='sudo btrfs filesystem balance /var/lib/docker'
# if macos i manage docker manually, because it drains battery and ram
if [ "$(uname)" = "Darwin" ]; then
alias startdocker='open -a Docker'
alias stopdocker="pkill -SIGHUP -f /Applications/Docker.app 'docker serve'"
alias sd='startdocker'
alias ssd='startdocker'
fi
fi

4
.config/aliasrc/dynamic Normal file
View file

@ -0,0 +1,4 @@
# if .cache/aliases exists source it
if [ -f $HOME/.cache/aliases ]; then
source $HOME/.cache/aliases
fi

2
.config/aliasrc/pandoc Normal file
View file

@ -0,0 +1,2 @@
# pandoc
alias npandoc='pandoc --from markdown --template ~/.config/pandoc/eisvogel.tex'

View file

@ -0,0 +1,25 @@
# pacman and yay
if [ -x "$(which pacman)" ]; then
alias spa='sudo pacman -S'
alias spaa='sudo pacman -S --overwrite="*"'
alias spu='sudo pacman -Syyuu'
alias spm='sudo pacman -Syy'
alias spr='sudo pacman -R'
fi
if [ -x "$(which yay)" ]; then
alias ys='yay -S'
alias yss='yay -S --overwrite="*"'
alias yr='yay -R'
alias yu='yay -Syyuu'
fi
# brew
if [ -x "$(which pacman)" ]; then
alias bu='brew update && brew update --cask'
alias bup='brew upgrade && brew upgrade --cask'
alias bd='brew doctor'
alias bp='brew pin'
alias bi='brew install'
alias bic='brew install --cask'
fi

View file

@ -5,7 +5,26 @@ function cd
elif [ " $1" = " -" ]; then
pushd "$OLDPWD" > /dev/null
else
pushd "$@" > /dev/null
pushd "$1" > /dev/null
fi
if [ "$VIRTUAL_ENV" != "" ]; then
local venvpath=$(dirname $VIRTUAL_ENV)
case $PWD/ in
$venvpath/* ) ;;
*) deactivate;;
esac
fi
if [ -f .venv/bin/activate ]; then
source .venv/bin/activate
fi
if [ $# -gt 1 ]; then
if [ -f Makefile ]; then
make ${@:2}
else
fi
fi
}

View file

@ -0,0 +1,31 @@
function fcd {
where="${1:-.}"
echo $where
cd $(find $where \( \
-name ".git" -o \
-name ".dotnet" -o \
-name "debug" -o \
-name "bin" -o \
-name "obj" -o \
-name ".idea" -o \
-name ".fleet" -o \
-name "node_modules" -o \
-name "volumes" -o \
-name ".terraform" -o \
-name "Library" -o \
-name "Pictures" -o \
-name "Documents" -o \
-name ".local" -o \
-name ".nuget" -o \
-name ".npm" -o \
-name ".vscode" -o \
-name ".rustup" -o \
-name ".cargo" -o \
-name ".quokka" -o \
-name ".vscode-insiders" \
\) -prune -false -o -type d -print | fzf)
}
function fv {
vim $(fzf)
}

View file

@ -0,0 +1,88 @@
# not that pretty, but needs it to be pretty fast
import os
from shutil import which
IGNORE = ["go", "parallels", "tmp", "library"]
MAX_DEPTH = 5
def subdirs(path):
return [d for d in os.listdir(path) if not d.startswith(".") and os.path.isdir(os.path.join(path, d))]
def gen_aliases(dirs, prefix="", subpath=""):
if len(dirs) == 0:
return {}
aliases = {}
for d in dirs:
if " " in d:
# who uses spaces in directory names anyway?
continue
for i in range(1, len(d)):
k = d.lower()[:i]
if i == len(d) - 1:
aliases[prefix + d.lower()] = os.path.join(subpath, d)
break
if all([dd == d or not dd.lower().startswith(k) for dd in dirs]) and not which(prefix + k):
aliases[prefix + k] = os.path.join(subpath, d)
break
return aliases
def process_dir(path, prefix="", depth=0):
if depth >= MAX_DEPTH:
return {}
if os.path.exists(os.path.join(path, ".git")):
# dont wanna go deepper into repo
return {}
if os.path.exists(os.path.join(path, ".hg")):
return {}
if os.path.exists(os.path.join(path, ".svn")):
return {}
if os.path.exists(os.path.join(path, ".venv")):
return {}
dirs = subdirs(path)
if depth == 0:
dirs = [d for d in dirs if all([i not in d.lower() for i in IGNORE])]
aliases = gen_aliases(dirs, prefix, path)
# we can go deeper
for pref, pth in aliases.copy().items():
aliases.update(process_dir(pth, pref, depth + 1))
return aliases
def gen_alias(alias_name, path):
cmd = f"cd {path}"
return "alias {}='{}'".format(alias_name, cmd)
print("Generating aliases...")
ALIASES = []
for alias_name, path in process_dir(os.path.expanduser("~")).items():
ALIASES.append(gen_alias(alias_name, path))
if not os.path.exists(os.path.expanduser("~/.cache")):
os.path.mkdir(os.path.expanduser("~/.cache"))
with open(os.path.expanduser("~/.cache/aliases"), "w") as f:
f.write("\n".join(ALIASES))
print("Done!")

View file

@ -0,0 +1,3 @@
function gen_aliases() {
py ~/.config/scriptrc/gen_aliases.py
}