dotfiles/.bashrc

360 lines
11 KiB
Bash

alias bsq='cd ~/repos/piscine/bsq && zellij'
if [ -f ~/.bash_aliases ] || :; then
. ~/.bash_aliases
fi
alias killrev="2>/dev/null kill -9 \$(ps auxww|grep '[_]_import__('|awk '{print $2}')"
alias l='ls -lArthF --color=auto'
alias v='vim'
alias ..='cd ..'
alias ...='cd ../..'
alias tmux='tmux -2'
alias ft='cc -Wall -Wextra -Werror -o main -xc -- <(grep -v "////" *.c) && ./main ; rm -f -- ./main ; :'
alias ft2='cc -Wall -Wextra -Werror -o main -xc -- <(grep -v "////" *.c) && ./main'
alias git='EDITOR=vim git'
alias cmatrix='cmatrix -u3 -Cred'
alias gca='git add -u && git commit -m "Automatic add"'
alias watch='watch -cn.1'
alias push='git push origin master ; git push gitea master'
alias francinette=~/francinette/tester.sh
alias paco=~/francinette/tester.sh
alias norm='alacritty -e sh -c '\''git rev-parse --show-toplevel || { echo "Exiting in 5" ; sleep 1 ; echo "Exiting in 4" ; sleep 1 ; echo "Exiting in 3" ; sleep 1 ; echo "Exiting in 2" ; sleep 1 ; echo "Exiting in 1" ; sleep 1 ; } && cd "$(git rev-parse --show-toplevel)" && watch -cn.5 date \&\& norminette -R CheckForbiddenSourceHeader'\'' & disown'
alias AH='cd /nfs/homes/tischmid/repos/42cursus/libft'
alias z='\zellij options --disable-mouse-mode'
alias zellij='\zellij options --disable-mouse-mode'
alias pacop='clear && 2>/dev/null paco && 2>/dev/null paco --strict'
# echo "hackerman? come to bocal :)"
# curl parrot.live
xset r rate 200 60
export TERM=xterm
HISTSIZE=-1
HISTFILESIZE=-1
__ftest_file () {
file="${1}"
tmp_dir="${2}"
test_file_name="$($(type -P basename) -- "${file%.c}_test.c")"
test_file="$(find "${tmp_dir}" -name "${test_file_name}" -type f)"
if [ -z "${test_file}" ]; then
printf '\033[31m%s\033[m\n' "No test file for file '${file}'"
return 1
fi
${test_file}
}
ftest () {
err="0"
if [ -z "${tmp_dir}" ]; then
tmp_dir="$(mktemp -dt -- '42tests-tischmid.XXXXXXXX')"
git clone --quiet 'https://git.timo.one/42berlin/42tests-tischmid.git' "${tmp_dir}"
fi
if [ -z "${@}" ]; then
for file in $(find . -mindepth 1 -type f -name '*.c'); do
__ftest_file "${file}" "${tmp_dir}" || err="1"
done
else
while [ -z "${1}" ]; do
if [ -f "${1}" ]; then
__ftest_file "${1}" "${tmp_dir}" || err="1"
else
printf '\033[31m%s\033[m\n' "File ${1} does not exist"
fi
shift
done
fi
rm -rf -- "/tmp/42tests-tischmid.*"
unset -v -- "tmp_dir"
return -- "${err}"
}
dapt () {
ACTION="${1}"
shift
CMD="RUN apt ${ACTION} -yy ${@}"
echo "${CMD}" >> ~/repos-other/docker/Dockerfile
docker build -t ubuntu-bloated ~/repos-other/docker/
docker rmi $(docker images -f "dangling=true" -q) 2>/dev/null
}
# norminette () {
# version="$($(type -P norminette) -v | cut -d" " -f2)"
# if [ ! "${version}" = "3.3.50" ]; then
# printf "%s\n" "Your norminette version is not 3.3.50, but ${version}, which will not catch some indentation errors. Please up-/downgrade"
# fi
# $(type -P norminette) -R CheckForbiddenSourceHeader "$@"
# }
jmp () {
cd "$(find . -name "${1}" -not -wholename "*mouli*" -print -quit)"
if [ "${?}" = "0" ] ; then
cd "$(find .. -name "${1}" -not -wholename "*mouli*" -print -quit)"
fi
}
PATH="${PATH}:~/.local/bin"
GIT_PROMPT="0"
if [ ! -f ~/git-prompt.sh ] && [ "${GIT_PROMPT}" = "1" ] ; then
curl --silent --location 'https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh' -o ~/git-prompt.sh
fi
__norm () {
return 1;
[ -z "${PWD##*ft*}" ] || { return 1; }
norminette 1>/dev/null 2>&1 && printf ' \033[92m%s\033[m' "[Norm: OK]" || printf ' \033[101;37m%s\033[m' "[䝝誒 ‼ NORM ‼ 屌誒]"
return 0
}
if [ -f ~/git-prompt.sh ] && [ "${GIT_PROMPT}" = "1" ] ; then
. ~/git-prompt.sh
export GIT_PS1_SHOWDIRTYSTATE=1
PS1='\[\033[31m\]\u\[\033[m\]@\[\033[32m\]\h\[\033[m\] \[\033[33m\][\w]\[\033[m\]\[\033[36m\]$(__git_ps1 " (%s)")\[\033[m\]\[\033[36m\]$(__norm)\[\033[m\]\n\[\033[35m\]~>\[\033[m\] '
else
PS1='\[\033[31m\]\u\[\033[m\]@\[\033[32m\]\h\[\033[m\] \[\033[33m\][\w]\[\033[m\]\[\033[36m\]$(__norm)\[\033[m\]\n\[\033[36m\]~>\[\033[m\] '
fi
##-----------------------------------------------------
## synth-shell-greeter.sh
if [ -f ~/.config/synth-shell/synth-shell-greeter.sh ] && [ -n "$( echo $- | grep i )" ] ; then
:
# source ~/.config/synth-shell/synth-shell-greeter.sh
fi
##-----------------------------------------------------
## synth-shell-prompt.sh
if [ -f ~/.config/synth-shell/synth-shell-prompt.sh ] && [ -n "$( echo $- | grep i )" ] ; then
:
# source ~/.config/synth-shell/synth-shell-prompt.sh
fi
##-----------------------------------------------------
## better-ls
if [ -f ~/.config/synth-shell/better-ls.sh ] && [ -n "$( echo $- | grep i )" ] ; then
:
# source ~/.config/synth-shell/better-ls.sh
fi
##-----------------------------------------------------
## alias
if [ -f ~/.config/synth-shell/alias.sh ] && [ -n "$( echo $- | grep i )" ] ; then
source ~/.config/synth-shell/alias.sh
fi
##-----------------------------------------------------
## better-history
if [ -f ~/.config/synth-shell/better-history.sh ] && [ -n "$( echo $- | grep i )" ] ; then
source ~/.config/synth-shell/better-history.sh
fi
check () {
# Set params
URL="${1}"
DIR="/tmp/tmp_repo_$(date +%s)"
# If no URL, get it from current repo
if [ -z "${URL}" ] ; then
URL="$(git remote get-url origin)"
fi
# Clone in temp folder
git clone --quiet "${URL}" "${DIR}"
# Only proceed if clone success
if [ -d "${DIR}" ] ; then
# Check the norm and print beautiful message
norminette -R CheckForbiddenSourceHeader "${DIR}" && printf '\033[30;102m%s\033[m\n' "Norminette success" || printf '\033[30;101m%s\033[m\n' "Norminette fail!!!"
# Remove temp folder
rm -rf "${DIR}"
else
printf '\033[30;101m%s\033[m\n' "Could not clone the repo"
fi
}
safe_guard_name () {
printf '%s' "${1}" | tr \\'!@#$%^&*()_+~{}|:">[]?<, ./ ;=\t\-'\'\` '__________________________________'
}
timoulinette () {
# Set params
subject="${1}"
ex="${2}"
extras="${3}"
# Arghandle
if [ -z "${subject}" ] ; then
printf '%s\n' "Must provide at least one argument. Exiting"
return 1
fi
# If exercise was not provided, get it from current folder name
if [ -z "${ex}" ] ; then
ex="$(basename -- "${PWD}")"
fi
# Search for a compiler
if command -v cc >/dev/null 2>&1 ; then
CC="$(command -v cc)"
elif command -v gcc >/dev/null 2>&1 ; then
CC="$(command -v gcc)"
elif command -v clang >/dev/null 2>&1 ; then
CC="$(command -v clang)"
else
printf '%s\n' "Could not find a compiler. Exiting"
return
fi
git_base="https://git.timo.one/42berlin/piscine-timo-moulinette/raw/branch/master"
# Find the first C file
file_name="$(find . -mindepth 1 -maxdepth 1 -name '*.c' -not -name '*mouli*' -type f -exec basename {} \; -quit)"
# If no C files found, exit
if [ -z "${file_name}" ] ; then
printf '%s\n' "Could not find any C files in the current directory. Exiting"
return 2
fi
# Testfiles have the "_test.c" suffix
test_file_name="$(printf '%s' "${file_name}" | sed 's/\.c$/_test.c/g')"
# Full git url to raw file
git_full="${git_base}/${subject}/${ex}/${test_file_name}"
# Name of the C testfile
download_file_name="./timoulinette${subject}_${ex}_${test_file_name}"
# Download tesfile
curl -sL "${git_full}" -o "${download_file_name}"
if [ ! "${?}" = "0" ] ; then
printf '%s\n' "There's now testfile for this task, sorry!"
printf '%s\n' "Download url is"
printf '%s\n' "${git_full}"
rm -f -- "${download_file_name}"
return
fi
# Preprocess source file (remove comments)
${CC} -dD -E -o "${file_name}.nocomments" -- "${file_name}"
# Extract function definitions from preprocessed file and append semicolon
prototypes="$(cat -- "${file_name}.nocomments" | grep -v '#' | grep ')$' | grep -v ' (' | grep -v ' ' | grep '[a-zA-Z0-9](' | grep -v 'int[[:space:]]\+main(' | sed 's/$/ ;/')"
# Extract macros from file (ignore the ones with line continuations)
macros="$(cat -- "${file_name}" | grep '^#' | grep -v '\\$')"
# Remove preprocessed file
rm -f -- "${file_name}.nocomments"
# Write prototypes to beginning of testfile
IFS="
"
for proto in ${prototypes} ; do
printf '%s\n' "${proto}" | cat -- - "${download_file_name}" > ".tmpaddfirstlinefile.c"
mv -- ".tmpaddfirstlinefile.c" "${download_file_name}"
done
# Write macros to beginning of testfile
IFS="
"
for macro in ${macros} ; do
guard_name="$(safe_guard_name "${macro}")"
# Endif include guard
printf '%s\n\n' "#endif" | cat -- - "${download_file_name}" > ".tmpaddfirstlinefile.c"
mv -- ".tmpaddfirstlinefile.c" "${download_file_name}"
# The macro itself
printf '%s\n' "${macro}" | cat -- - "${download_file_name}" > ".tmpaddfirstlinefile.c"
mv -- ".tmpaddfirstlinefile.c" "${download_file_name}"
# Define include guard
printf '%s\n' "# define ${guard_name}" | cat -- - "${download_file_name}" > ".tmpaddfirstlinefile.c"
mv -- ".tmpaddfirstlinefile.c" "${download_file_name}"
# Include guard
printf '%s\n' "#ifndef ${guard_name}" | cat -- - "${download_file_name}" > ".tmpaddfirstlinefile.c"
mv -- ".tmpaddfirstlinefile.c" "${download_file_name}"
done
IFS="$(printf ' \n\t')"
# Compile
out_binary="timoulinette${subject}_${ex}_$(basename -- "${test_file_name}" .c).out"
${CC} -Wall -Wextra -Werror -o "${out_binary}" -xc -- *.c
err_code="${?}"
if [ ! "${err_code}" = 0 ] ; then
printf '%s\n' "Could not compile. Exiting"
return
fi
# Delete testfile
if [ "$(printf '%s' "${extras}" | sed 's/keep-test//g')" = "${extras}" ] ; then
rm -f -- "${download_file_name}"
fi
# Run binary
./${out_binary}
if [ "${?}" = "0" ] && [ "$(printf '%s' "${extras}" | sed 's/keep-binary//g')" = "${extras}" ] ; then
rm -f -- "./${out_binary}"
fi
}
prev () {
if [ -z "${1}" ] ; then
ex="$(basename "${PWD}")"
ex_num="$(printf '%s' "${ex}" | cut -c3- | sed 's/^0*//g')"
next_ex_num="$(( ${ex_num} - 1 ))"
else
next_ex_num="${1}"
fi
builtin printf -v padded '%02d' "${next_ex_num}"
next_dir="ex${padded}"
builtin cd "../${next_dir}"
}
next () {
# If no arg, calculate next num
if [ -z "${1}" ] ; then
# Exercise name
ex="$(basename "${PWD}")"
# Exercise num
ex_num="$(printf '%s' "${ex}" | cut -c3- | sed 's/^0*//g')"
# + 1 for next
next_ex_num="$(( ${ex_num} + 1 ))"
else
next_ex_num="${1}"
fi
# Pad to two 0's and save in variable "padded"
builtin printf -v padded '%02d' "${next_ex_num}"
next_dir="ex${padded}"
builtin cd "../${next_dir}"
}
clone42 () {
folder="${1}"
repo_url="${2}"
\unset -f norminette
git clone --quiet "${repo_url}" ${folder}
if [ "${?}" = "0" ]; then
cd "${folder}"
\norminette -R CheckForbiddenSourceHeader "."
else
printf '%s\n' "Could not clone repo!"
fi
}
export MAIL='timo42@proton.me'