zsh: almost all functions are now scripts

This commit is contained in:
PowerUser64 2021-12-26 16:15:56 -08:00
parent 1d378ca202
commit ee7de5f557

273
.zshrc
View file

@ -13,7 +13,7 @@
export EDITOR=vi export EDITOR=vi
fi fi
export PATH="$PATH:${XDG_DATA_HOME:-$HOME/.local/bin}" export PATH="$PATH:${XDG_DATA_HOME:-$HOME/.local/bin}:${XDG_CONFIG_HOME:-$HOME/.config/shell/bin}"
export SYSTEMD_EDITOR="$EDITOR" export SYSTEMD_EDITOR="$EDITOR"
export LSCOLORS="Gxfxcxdxbxegedabagacad" export LSCOLORS="Gxfxcxdxbxegedabagacad"
@ -21,6 +21,9 @@
export ZINIT_HOME_DIR="$HOME/.local/share/zinit" export ZINIT_HOME_DIR="$HOME/.local/share/zinit"
# Don't add extra space to the right side of the prompt
ZLE_RPROMPT_INDENT=0
WORKING_DIR_SAVE_FILE="${XDG_CACHE_HOME:-$HOME/.cache}/zsh/last-working-dir" WORKING_DIR_SAVE_FILE="${XDG_CACHE_HOME:-$HOME/.cache}/zsh/last-working-dir"
# make the dir for the file if needed # make the dir for the file if needed
! test -d "$(dirname "$WORKING_DIR_SAVE_FILE")" && mkdir -p "$(dirname "$WORKING_DIR_SAVE_FILE")" ! test -d "$(dirname "$WORKING_DIR_SAVE_FILE")" && mkdir -p "$(dirname "$WORKING_DIR_SAVE_FILE")"
@ -194,159 +197,6 @@
# <<< # <<<
### Functions >>> ### Functions >>>
{ {
# cd search: cd to a directory, given part of its name
# (also can take arguments for an `fd` commnd)
# (also can cd to a file if `-t f` is passed)
cds() {
if ! [ -z "$1" ];then
DIR="$(fd --max-results=1 -t d $@)"
if [ -f "$DIR" ];then
cd "$(dirname "$DIR")" && pwd
else
cd "$DIR" && pwd
fi
else
echo "$0: no arguments provided"
return 1
fi
}
# cd find (interactive): cd to the containing dir of a file, or inside a folder, given part of its name
# Basically, an interactive version of what's above
# can take an `fd` command
# requires that `fzy` is installed
cdf() {
DIR="$(fd $@ | fzy)"
if [ -f "$DIR" ];then
cd "$(dirname "$DIR")" && pwd
else
cd "$DIR" && pwd
fi
}
# cd all: opens a new shell in each of the directories below the one you're in, so
# you can execute commands in them until you type 'exit'
# If I use this enough, I may make something that can do more
# Ideas for what it could do in the future:
# - somehow record typed commands so they can be automatically repeated like a macro
# - maybe get the length of the histfile before and after, executing the last X number of commands
# - Maybe just add an argument that takes a command and executes it in all dirs
# (although that wouldn't be as good as recording actions bc it's easy to do
# that by just writing a for loop)
cda() {
for DIR in *; do
(cd "$DIR" && zsh)
done
}
# edit file: Uses fzy to locate a file, and opens it in your favorite text editor
# Takes arguments for `fd`
ef() {
"$EDITOR" "$(fd $@ | fzy)"
}
# toggles whether a file or group of files is executable
chx() {
for FILE in "$@";do
if [ -x "$FILE" ];then
chmod -x "$FILE" &&
echo -e "$FILE: -x"
elif ! [ -x "$FILE" ];then
chmod +x "$FILE" &&
echo -e "$FILE: +x"
else
echo "error: file $FILE does not exist" >&2
fi
done
}
launch() {
$@ > /dev/null 2>&1 &
disown
}
# a simple way to manage your todo list
# Usage:
# todo -- pull latest repo version and edit ~/todo
# todo [any git command] -- manage todo for easy syncing, assuming ~/todo is
# a symlink that points to a file in a git repo
#
# Suggested use: make a git repo for holding your todo list, then make
# a symlink called 'todo' that points to your todo list in your home directory
#
todo() {
todo_dir="$(dirname "$(realpath ~/todo)")"
todo_file="$(realpath ~/todo)"
if [ -z "$@" ];then
( # subshell to protect against directory changes
cd "$todo_dir"
# pull the latest commits
git rev-parse &&
git pull
# open the file
"$EDITOR" "$todo_file"
# commit and push the file if it's in a git repo and the file has changed
if git rev-parse && ! git diff --exit-code "$todo_file"; then
git commit "$todo_file" -m 'todo' &&
git push
fi
)
elif [ "$@" = "cd" ]; then
cd "$todo_dir"
else
git -C "$todo_dir" $@
fi
}
# Simple extraction script. Taken from manjaro's .bashrc
ex() {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1;;
*.7z) 7z x $1 ;;
*) echo "'$1' cannot be extracted via ex()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
# TODO: encode the url to fix things like spaces using something like https://stackoverflow.com/a/10660730/11162605
# usage: sitepath <path to file>
# usage: sitepath <file to find>
# usage: sitepath
# usage: sitepath <fd commands> -- you can search with 'fd' if you put in a command
sitepath() {
# account for if there is no urlencode command
URLENCODE=urlencode
if ! command -v urlencode > /dev/null 2>&1;then
URLENCODE=cat
fi
# If there is no input, make the input the current directory
[ -z ${1+x} ] && 1="$(pwd)"
FILEPATH="$(([ -e "$1" ] && readlink -f "$1") || fd $@ /home/blake/docker/blakenorth.net/site)"
# change all file paths into urls
echo "$FILEPATH" |
grep -o 'site.*' |
sed 's+^site+https://blakenorth.net+g' |
$URLENCODE # if you need this, I am using dead10ck/urlencode (cargo install urlencode)
}
# for sending error messages from functions and whatnot # for sending error messages from functions and whatnot
error() { error() {
@ -355,96 +205,6 @@
return $ERROR_CODE return $ERROR_CODE
} }
# prcolors(): Display all colors in a few different ways >>>
# TODO: change this to make it not just a few scripts smashed together
prcolors() {
[ -z "$1" ] && 1=0
if [ $1 -eq '1' ];then
# Taken from manjaro's bashrc >>>
local fgc bgc vals seq0
printf "Color escapes are %s\n" '\e[${value};...;${value}m'
printf "Values 30..37 are \e[33mforeground colors\e[m\n"
printf "Values 40..47 are \e[43mbackground colors\e[m\n"
printf "Value 1 gives a \e[1mbold-faced look\e[m\n\n"
# foreground colors
for fgc in {30..37}; do
# background colors
for bgc in {40..47}; do
fgc=${fgc#37} # white
bgc=${bgc#40} # black
vals="${fgc:+$fgc;}${bgc}"
vals=${vals%%;}
seq0="${vals:+\e[${vals}m}"
printf " %-9s" "${seq0:-(default)}"
printf " ${seq0}TEXT\e[m"
printf " \e[${vals:+${vals+$vals;}}1mBOLD\e[m"
done
echo; echo
done
# <<<
elif [ $1 -eq '2' ];then
# from base16shell >>>
ansi_mappings=(
Red Green
Yellow Blue
Magenta Cyan
White Black
Bright_Red Bright_Green
Bright_Yellow Bright_Blue
Bright_Magenta Bright_Cyan
Bright_White Bright_Black
)
colors=(
base00 base08
base0B base0A
base0D base0E
base0C base05
base03 base08
base0B base0A
base0D base0E
base0C base07
base09 base0F
base01 base02
base04 base06
)
for padded_value in `seq -w 0 21`; do
color_variable="color${padded_value}"
eval current_color=\$${color_variable}
current_color=$(echo ${current_color//\//} | tr '[:lower:]' '[:upper:]') # get rid of slashes, and uppercase
non_padded_value=$((10#$padded_value))
base16_color_name=${colors[$non_padded_value]}
current_color_label=${current_color:-unknown}
ansi_label=${ansi_mappings[$non_padded_value]}
block=$(printf "\x1b[48;5;${non_padded_value}m___________________________")
foreground=$(printf "\x1b[38;5;${non_padded_value}m$color_variable")
printf "%s %s %s %-30s %s\x1b[0m\n" $foreground $base16_color_name $current_color_label ${ansi_label:-""} $block
done;
#if [ $# -eq 1 ]; then
# printf "To restore current theme, source ~/.base16_theme or reopen your terminal\n"
#fi
# <<<
elif [ $1 -eq '3' ];then
# Similar to above, but does 256-bit colors. >>>
# Taken from this: https://github.com/romkatv/powerlevel10k#set-colors-through-Powerlevel10k-configuration-parameters
for i in {0..255}; do
print -Pn "%K{$i} %k%F{$i}${(l:3::0:)i}%f " ${${(M)$((i%6)):#3}:+$'\n'}
done
# <<<
else
cat <<-EOF
Usage: prcolors <printing method number>
1: Manjaro bashrc -- for checking text readability
2: base26shell -- for seeing the 16 set terminal colors
3: 256 -- for testing 256 color output
EOF
fi
}
# <<<
# shellcheck disable=SC2016 # shellcheck disable=SC2016
mkcd() { mkcd() {
mkdir -p "$1" || error $LINENO mkdir -p "$1" || error $LINENO
@ -462,36 +222,11 @@
done done
} }
# Git cLONE cD
gloned() {
git clone "$1" || return $?
if ! cd "$(echo "$1" | sed 's/\.git//g' | rev | cut -d '/' -f 1 | rev)";then
echo 'Error: Could not `cd` into the repo'
return 1
fi
echo
# ls --color=auto
}
# believe it or not, zsh makes for a fine calculator # believe it or not, zsh makes for a fine calculator
calc() { calc() {
echo $(($*)) echo $(($*))
} }
# Makes a swapfile at /swapfile of a given size (ex: mkswp 4G)
mkswp() {
sudo fallocate -l "$1" /swapfile &&
sudo chmod 600 /swapfile
sudo mkswap /swapfile &&
sudo swapon /swapfile
}
# Deletes the swapfile created by mkswp()
rmswp() {
sudo swapoff -v /swapfile
sudo rm /swapfile
}
# optionally source an external function file # optionally source an external function file
[ -f "${XDG_CONFIG_HOME:-$HOME/.config}/shell/fnrc" ] && source "${XDG_CONFIG_HOME:-$HOME/.config}/shell/fnrc" [ -f "${XDG_CONFIG_HOME:-$HOME/.config}/shell/fnrc" ] && source "${XDG_CONFIG_HOME:-$HOME/.config}/shell/fnrc"
} }