dotfiles/dot_config/bash/functions

424 lines
17 KiB
Plaintext
Raw Normal View History

2024-07-13 13:41:00 +03:00
#!/bin/bash
tdm() {
local awk_preview="/^~/ { next }
/^#+/ { print '\\033[35;3m'\$0'\\033[0m'; next }
/^ *\[\+\] / { print '\\033[32m'\$0'\\033[0m'; next }
/^ *\[x\] / { print '\\033[31m'\$0'\\033[0m'; next }
/^ *\[ \] / { print '\\033[33m'\$0'\\033[0m'; next }
/^ \* / { print '\\033[34m'\$0'\\033[0m'; next }
/^[0-9]+\. / { print '\\033[34m'\$0'\\033[0m'; next }
/^>/ { print '\\033[92m'\$0'\\033[0m'; next }
/^\(!\)/ { print '\\033[91;7m'\$0'\\033[0m'; next }
/.*/ { print \$0; next }";
local act="$1"; shift;
[ -z "$TODO_PATH" ] && return;
case "$act" in
motd) [ "$(tdm num)" -eq 0 ] && return;
printf "\033[33m===== TODO =====\033[0m\n";
tdm print;;
print)
awk "$awk_preview" "$TODO_PATH";;
add) echo "$*" | tee -a "$TODO_PATH";;
num) grep -vcP "^~" "$TODO_PATH";;
ed) case "$EDITOR" in
*vim) $EDITOR -c "set syntax=markdown" "$TODO_PATH";;
*) $EDITOR "$TODO_PATH";;
esac;;
*) echo "tdm [print|add|num|ed]";;
esac;
}
# Project management
#alias p="cd \"\$(find ~/projects/ -type d -name '.git' | sed 's|/.git$||' | gum filter)\""
p() {
_log() {
printf "\033[33m * \033[34m%s\033[0m\n" "$@";
}
local cmd="$1"; shift;
local projects_dir="${HOME}/projects";
case "$cmd" in
ls) find "${projects_dir}" -type d -name .git \
| sed -e 's|/.git$||' -e "s|^${projects_dir}||";;
cd) dirname="$1";
if [ "$dirname" = "" ]; then
dirname="$( (printf -- "--cancel--\n"; p ls) | gum filter)" || return;
if [ "$dirname" = "--cancel--" ]; then return; fi;
fi;
cd "${projects_dir}/${dirname}" || return;
if [ -d "${projects_dir}/${dirname}/venv" ]; then
export VIRTUAL_ENV_DISABLE_PROMPT=1;
source "${projects_dir}/${dirname}/venv/bin/activate";
fi;
;;
mk)
project_name="$(gum input --placeholder="Project name?" --value="$1")" || return;
project_type="$(printf "Python\nC\nPackwiz\nNone\n" | gum choose)" || return;
mkdir -vp "$projects_dir/$project_name";
cd "$projects_dir/$project_name" || return;
case "$project_type" in
Packwiz)
_log "Initializing Packwiz pack";
packwiz init --modloader fabric;
;;
Python)
_log "Creating .gitignore";
printf '*.py[cow]\n__pycache__/\nvenv/\n' > .gitignore;
_log "Creating requirements.txt";
echo > requirements.txt;
_log "Creating $project_name/__main__.py";
mkdir -vp "$project_name";
printf 'def main():\n' > "$project_name/__main__.py";
printf " print('Hi!')\n\n" >> "$project_name/__main__.py";
printf "if __name__ == '__main__':\n" >> "$project_name/__main__.py";
printf " main()\n" >> "$project_name/__main__.py";
_log "Creating virtualenv";
python3 -m virtualenv venv;
;;
C)
_log "Creating directories";
mkdir -vp src obj;
touch Makefile src/main.c obj/.gitkeep;
_log "Creating src/main.c";
printf '#include <stdio.h>\n' > src/main.c;
printf '#include <math.h>\n\n' >> src/main.c;
printf 'int main(void) {\n' >> src/main.c;
printf ' printf("Hi!\\n");\n' >> src/main.c;
printf '}\n' >> src/main.c;
_log "Creating Makefile";
printf 'CFLAGS += \n' > Makefile;
printf 'LDFLAGS := -lm\n' >> Makefile;
printf 'OBJECTS := \n\n' >> Makefile;
printf '%s: lib\n' "$project_name" >> Makefile;
# shellcheck disable=SC2016
printf '\t$(CC) $(CFLAGS) $(OBJECTS) src/main.c $(LDFLAGS) -o %s\n\n' "$project_name" >> Makefile;
printf 'all: %s\n\n' "$project_name" >> Makefile;
# shellcheck disable=SC2016
printf 'lib: $(OBJECTS)\n\n' >> Makefile;
printf 'clean:\n' >> Makefile;
# shellcheck disable=SC2016
printf '\t$(RM) $(OBJECTS) %s\n\n' "$project_name" >> Makefile;
printf 'obj/%%.o: src/%%.c\n' >> Makefile;
# shellcheck disable=SC2016
printf '\t$(CC) $(CFLAGS) -c $^ -o $@\n' >> Makefile;
_log "Creating .gitignore"
printf 'obj/*\n%s\n' "$project_name" > .gitignore;
;;
None) echo "nyan";;
*) echo "k bai"; return;;
esac;
_log "Creating Git repository";
git init; git add -v .;
_log "Commiting base project";
git commit -m 'Initial commit';
_log "Adding remote";
git remote add origin "ssh://git@[201:4230::a949:3323:1d96:1979]:2222/hkc/$project_name";
_log "Pushing";
git push --set-upstream origin master;
;;
esac;
}
pcolor() {
if [ "$1" = "all" ]; then pcolor nrm 256 true; return $?; fi;
if [ "$1" = "" ]; then pcolor nrm; return $?; fi;
_tex() {
if [ "$1" = "" ]; then
printf "\033[40m %-56s \033[0m\n" "";
else
printf "\033[40;37m %-56s \033[0m\n" "$1";
fi;
}
while [ "$#" -ge 1 ]; do
local opt="$1"; shift;
case "$opt" in
sep) _tex "";;
nrm) _tex "Normal";
for((i=40;i<48;i++)); do
printf "\033[40m %3d \033[%dm" "$i" "$i";
for((j=30;j<38;j++)); do
printf "\033[%dm%3d" "$j" "$j";
done;
for((j=90;j<98;j++)); do
printf "\033[%dm%3d" "$j" "$j";
done;
printf "\033[40m \033[0m\n";
done;
for((i=100;i<108;i++)); do
printf "\033[40m %3d \033[%dm" "$i" "$i";
for((j=30;j<38;j++)); do
printf "\033[%dm%3d" "$j" "$j";
done;
for((j=90;j<98;j++)); do
printf "\033[%dm%3d" "$j" "$j";
done;
printf "\033[40m \033[0m\n";
done;
_tex "";;
256) _tex "6x6x6 cube + 24 grayscale";
for((y=0;y<18;y++)); do
printf "\033[40m %02x->%02x " \
"$((y * 12 + 16))" \
"$((y * 12 + 16 + 11))";
for((x=0;x<12;x++)); do
i=$((x + y * 12 + 16));
printf "\033[48;5;%dm %3d" "$i" "$i";
done;
printf "\033[40m \033[0m\n";
done;
printf "\033[40m %02x->%02x " "232" "255";
for((i=232;i<=255;i++)); do
printf "\033[48;5;%dm%02x" "$i" "$i";
done;
printf "\033[40m \033[0m\n";
_tex "";;
true) _tex "24bit RGB TrueColor";
for((y=0;y<48;y+=2)); do
printf "\033[40m ";
for((x=0;x<48;x++)); do
b_r=$((x * 256 / 48));
b_g=$((y * 256 / 48));
b_b=$(((x + y) * 256 / 48));
f_r=$((x * 256 / 48));
f_g=$(((y + 1) * 256 / 48));
f_b=$(((x + y + 1) * 256 / 48));
printf "\033[48;2;%d;%d;%d;38;2;%d;%d;%dm\u2584" \
"$b_r" "$b_g" "$b_b" "$f_r" "$f_g" "$f_b";
done;
printf "\033[40m \033[0m\n";
done;
_tex;;
esac;
done
}
lscolors() {
if [ "$1" = "all" ]; then
lscolors nrm bri 256 true;
return $?;
fi;
if [ "$1" = "" ]; then
lscolors nrm bri;
return $?;
fi;
local fcn="\033[%dm \033[0m";
local fcc="\033[%dm\033[%dm %3d \033[0m";
if [ "$1" = "nrm" ]; then shift;
printf "\033[40m %-56s \033[0m\n" "";
printf "\033[40m %-56s \033[0m\n" "Normal";
printf "\033[40m ";
for((i=40;i<48;i++)); do printf "$fcn" $i; done;
printf "\033[40m \033[0m\n";
printf "\033[40m ";
for((i=40;i<48;i++)); do printf "$fcc" $i 97 $i; done;
printf "\033[40m \033[0m\n";
printf "\033[40m ";
for((i=40;i<48;i++)); do printf "$fcn" $i; done;
printf "\033[40m \033[0m\n";
fi;
if [ "x$1" = "xbri" ]; then shift;
printf "\033[40m %-56s \033[0m\n" "";
printf "\033[40m %-56s \033[0m\n" "Bright";
printf "\033[40m ";
for((i=100;i<108;i++)); do printf "$fcn" $i; done;
printf "\033[40m \033[0m\n";
printf "\033[40m ";
for((i=100;i<108;i++)); do printf "$fcc" $i 30 $i; done;
printf "\033[40m \033[0m\n";
printf "\033[40m ";
for((i=100;i<108;i++)); do printf "$fcn" $i; done;
printf "\033[40m \033[0m\n";
fi;
if [ "x$1" = "x256" ]; then shift;
printf "\033[40m %-56s \033[0m\n" "";
printf "\033[40m %-56s \033[0m\n" "256 Colors palette";
for((y=0;y<32;y++)); do
printf "\033[40m ";
for((x=0;x<8;x++)); do
i=$((y * 8 + x));
if [ "$i" -le 7 ]; then n=15;
elif [ "$i" -le 15 ]; then n=0;
elif [ "$i" -ge 232 ]; then n=$((255 - i + 232));
else
ni=$((i - 16));
nb=$((5 - (ni % 6))); ni=$((ni / 6));
ng=$((5 - (ni % 6))); ni=$((ni / 6));
nr=$((5 - (ni % 6)));
n=$((16 + nb + ng * 6 + nr * 36));
fi
printf "\033[48;5;%d;38;5;%dm %3d \033[40m" $i $n $i;
done;
printf "\033[40m \n";
done
fi;
if [ "x$1" = "x256blk" ]; then shift;
printf "\033[40m %-32s \033[0m\n" "";
printf "\033[40m %-32s \033[0m\n" "256 Colors palette";
for((y=0;y<16;y++)); do
printf "\033[40m ";
for((x=0;x<16;x++)); do
i=$((y * 16 + x));
printf "\033[48;5;%dm \033[40m" $i;
done;
printf "\033[40m \n";
done
fi;
if [ "x$1" = "xtrue" ]; then shift;
printf "\033[40m %-56s \033[0m\n" "";
printf "\033[40m %-56s \033[0m\n" "TrueColor";
for ((y=0;y<56;y=y+2)); do
printf "\033[40m ";
for((x=0;x<56;x++)); do
r1=$((x * 255 / 56));
g1=$((255 * (56 - y) / 56));
b1=$((255-r1));
r2=$((x * 255 / 56));
g2=$((255 * (56 - (y + 1)) / 56));
b2=$((255-r2));
printf "\033[48;2;%d;%d;%d;38;2;%d;%d;%dm\u2584" \
"$r1" "$g1" "$b1" "$r2" "$g2" "$b2";
done;
printf "\033[40m \n";
done;
fi;
printf "\033[40m %-56s \033[0m\n" "";
}
exper() {
case "$1" in
c) filename="$(mktemp "tmpXXXXXXXX.c" --tmpdir)";
printf '#include <stdio.h>\n\nint main(void)\n{\n return 0;\n}' > "$filename";
binary="${filename/.c/}";
echo "filename=${filename} binary=${binary}";
nvim -O "$filename" \
"term://ronchange '$filename' sh -c 'set -xe; \
gcc -o $binary $filename -Wall -pedantic \
-std=c99 -lm; sleep 0.2; time $binary'";
;;
cpp) filename="$(mktemp "tmpXXXXXXXX.cpp" --tmpdir)";
printf 'int main(void)\n{\n return 0;\n}' > "$filename";
binary="${filename/.cpp/}";
echo "filename=${filename} binary=${binary}";
nvim -O "$filename" \
"term://ronchange '$filename' sh -c 'set -xe; \
g++ -o $binary $filename -Wall -pedantic \
-lm; sleep 0.2; time $binary'";
;;
rs) filename="$(mktemp "tmpXXXXXXXX.rs" --tmpdir)";
printf 'fn main() {\n\n}' > "$filename";
binary="${filename/.rs/}";
echo "filename=${filename} binary=${binary}";
nvim -O "$filename" \
"term://ronchange '$filename' sh -c 'set -xe; \
rustc $filename -o $binary; sleep 0.2; time $binary'";
;;
py) filename="$(mktemp "tmpXXXXXXXX.py" --tmpdir)";
printf 'print("Hi")' > "$filename";
nvim -O "$filename" \
"term://ronchange '$filename' python3 '$filename'";
;;
sh) filename="$(mktemp "tmpXXXXXXXX.sh" --tmpdir)";
printf '#!/bin/sh\necho hi' > "$filename";
nvim -O "$filename" \
"term://ronchange '$filename' /bin/sh '$filename'";
;;
lua) filename="$(mktemp "tmpXXXXXXXX.lua" --tmpdir)";
printf 'print("meow")' > "$filename";
nvim -O "$filename" \
"term://ronchange '$filename' /sbin/lua '$filename'";
;;
java) dirname="$(mktemp "tmpXXXXXXXX-java" --tmpdir -d)";
printf "public class Experiment {\n public static void main(String[] args) {\n\n }\n}\n" > "$dirname/Experiment.java";
nvim -O "$dirname/Experiment.java" \
"term://ronchange '$dirname/Experiment.java' sh -c 'cd $dirname; set -xe; javac Experiment.java; java Experiment'";
;;
*) return;;
esac;
}
k8shelper() {
cmd="$1"; shift;
case "$cmd" in
pod_name_from_ip)
kubectl get --all-namespaces --output json pods | jq -r ".items[] | select(.status.podIP==\"$1\").metadata | \"\\(.namespace)/\\(.name)\"";;
logwatch) kubectl logs -n "${1}" -f "$(kubectl get pod -n "${1}" | grep "${2}" | head -n 1 | awk '{print$1}')";;
logall)
namespace="${1}";
local pid_list=()
while read -r pod_name; do
kubectl logs -n "${namespace}" "${pod_name}" -f | awk "{print\"${pod_name}: \"\$0}" & bg >/dev/null 2>&1;
pid_list+=("$$");
done < <(kubectl get pod -n "${namespace}" | grep -v 'NAME' | awk '{print$1}');
read -r;
for log_pid in "${pid_list[@]}"; do
kill "${log_pid}";
done;
;;
shell)
namespace="${1}"; shift;
partial_name="${1}"; shift;
pod_name="$(kubectl get pod -n "${namespace}" | grep "${partial_name}" | head -n 1 | awk '{print$1}')";
kubectl exec -n "${namespace}" -it "${pod_name}" -- sh -c "bash || ash || sh";;
exec)
namespace="${1}"; shift;
partial_name="${1}"; shift;
pod_name="$(kubectl get pod -n "${namespace}" | grep "${partial_name}" | head -n 1 | awk '{print$1}')";
kubectl exec -n "${namespace}" -it "${pod_name}" -- "${@}";;
nodeport)
NODE_PORT=$(kubectl get --namespace "${1}" -o jsonpath="{.spec.ports[0].nodePort}" services "${2}");
NODE_IP=$(kubectl get nodes --namespace "${1}" -o jsonpath="{.items[0].status.addresses[0].address}");
echo "${NODE_IP}:${NODE_PORT}";
;;
*) return;;
esac;
}
bandcampdl() {
url="${1}";
YT_DLP_COMMAND="yt-dlp -f "'bestaudio[ext=mp3]/bestaudio'" --add-metadata --embed-thumbnail --extract-audio --audio-format mp3 --retries infinite";
echo "${YT_DLP_COMMAND}";
curl "${url}" -fsSL | grep -oP '/(track|album)/[^"]+' | sort -r | uniq \
| while read -r name; do
if [[ "${name}" =~ /album/ ]]; then
$YT_DLP_COMMAND -o '%(playlist)s/%(playlist_autonumber)2d - %(title)s.%(ext)s' "${url}${name}"
elif [[ "${name}" =~ /track/ ]]; then
$YT_DLP_COMMAND -o '%(title)s.%(ext)s' "${url}${name}"
else
echo "oopsie! ${name} is invalid";
fi
done
}
fded() {
readarray -t filelist < <(fd "$@");
if [ "${#filelist[@]}" = 0 ]; then
echo "No files found";
elif [ "${#filelist[@]}" = 1 ]; then
nvim "${filelist[0]}";
else
printf "%s\n" "${filelist[@]}" | gum filter | xargs nvim
fi
}