From 7ab614d730961dcffcad43f5174992ef5a075a0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Doma=C5=84ski?= Date: Wed, 21 Feb 2024 13:06:01 +0100 Subject: [PATCH] dynamic aliases --- .config/aliasrc/docker | 16 ++++++ .config/aliasrc/dynamic | 4 ++ .config/aliasrc/pandoc | 2 + .config/aliasrc/pkgmanager | 25 ++++++++++ .config/scriptrc/cd.sh | 21 +++++++- .config/scriptrc/fuzzies.sh | 31 ++++++++++++ .config/scriptrc/gen_aliases.py | 88 +++++++++++++++++++++++++++++++++ .config/scriptrc/gen_aliases.sh | 3 ++ 8 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 .config/aliasrc/docker create mode 100644 .config/aliasrc/dynamic create mode 100644 .config/aliasrc/pandoc create mode 100644 .config/aliasrc/pkgmanager create mode 100644 .config/scriptrc/fuzzies.sh create mode 100644 .config/scriptrc/gen_aliases.py create mode 100644 .config/scriptrc/gen_aliases.sh diff --git a/.config/aliasrc/docker b/.config/aliasrc/docker new file mode 100644 index 0000000..9aa4d0d --- /dev/null +++ b/.config/aliasrc/docker @@ -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 diff --git a/.config/aliasrc/dynamic b/.config/aliasrc/dynamic new file mode 100644 index 0000000..19f248e --- /dev/null +++ b/.config/aliasrc/dynamic @@ -0,0 +1,4 @@ +# if .cache/aliases exists source it +if [ -f $HOME/.cache/aliases ]; then + source $HOME/.cache/aliases +fi diff --git a/.config/aliasrc/pandoc b/.config/aliasrc/pandoc new file mode 100644 index 0000000..6c0107b --- /dev/null +++ b/.config/aliasrc/pandoc @@ -0,0 +1,2 @@ +# pandoc +alias npandoc='pandoc --from markdown --template ~/.config/pandoc/eisvogel.tex' diff --git a/.config/aliasrc/pkgmanager b/.config/aliasrc/pkgmanager new file mode 100644 index 0000000..ad847cd --- /dev/null +++ b/.config/aliasrc/pkgmanager @@ -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 diff --git a/.config/scriptrc/cd.sh b/.config/scriptrc/cd.sh index 478fbb1..28bb764 100644 --- a/.config/scriptrc/cd.sh +++ b/.config/scriptrc/cd.sh @@ -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 } diff --git a/.config/scriptrc/fuzzies.sh b/.config/scriptrc/fuzzies.sh new file mode 100644 index 0000000..146f0fb --- /dev/null +++ b/.config/scriptrc/fuzzies.sh @@ -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) +} diff --git a/.config/scriptrc/gen_aliases.py b/.config/scriptrc/gen_aliases.py new file mode 100644 index 0000000..af8d8a1 --- /dev/null +++ b/.config/scriptrc/gen_aliases.py @@ -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!") diff --git a/.config/scriptrc/gen_aliases.sh b/.config/scriptrc/gen_aliases.sh new file mode 100644 index 0000000..93fd7d0 --- /dev/null +++ b/.config/scriptrc/gen_aliases.sh @@ -0,0 +1,3 @@ +function gen_aliases() { + py ~/.config/scriptrc/gen_aliases.py +}