dotfiles/.bashrc

269 lines
8.4 KiB
Bash

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 -cu3 -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 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'
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. Exiting"
fi
$(type -P norminette) "$@"
}
jmp () {
cd "$(find . -name "${1}" -not -wholename "*mouli*" -print -quit)"
if [ "${?}" = "0" ] ; then
cd "$(find .. -name "${1}" -not -wholename "*mouli*" -print -quit)"
fi
}
xset r rate 200 60
PATH="${PATH}:/nfs/homes/tischmid/.local/bin"
# if [ ! -f ~/git-prompt.sh ] ; then
# curl --silent --location 'https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh' -o "~/git-prompt.sh"
# fi
#
# . ~/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\]\n$ '
PS1='\[\033[31m\]\u\[\033[m\]@\[\033[32m\]\h\[\033[m\] \[\033[33m\][\w]\[\033[m\]\[\033[36m\]\[\033[m\]\n$ '
##-----------------------------------------------------
## synth-shell-greeter.sh
if [ -f /nfs/homes/tischmid/.config/synth-shell/synth-shell-greeter.sh ] && [ -n "$( echo $- | grep i )" ] ; then
:
# source /nfs/homes/tischmid/.config/synth-shell/synth-shell-greeter.sh
fi
##-----------------------------------------------------
## synth-shell-prompt.sh
if [ -f /nfs/homes/tischmid/.config/synth-shell/synth-shell-prompt.sh ] && [ -n "$( echo $- | grep i )" ] ; then
:
# source /nfs/homes/tischmid/.config/synth-shell/synth-shell-prompt.sh
fi
##-----------------------------------------------------
## better-ls
if [ -f /nfs/homes/tischmid/.config/synth-shell/better-ls.sh ] && [ -n "$( echo $- | grep i )" ] ; then
:
# source /nfs/homes/tischmid/.config/synth-shell/better-ls.sh
fi
##-----------------------------------------------------
## alias
if [ -f /nfs/homes/tischmid/.config/synth-shell/alias.sh ] && [ -n "$( echo $- | grep i )" ] ; then
source /nfs/homes/tischmid/.config/synth-shell/alias.sh
fi
##-----------------------------------------------------
## better-history
if [ -f /nfs/homes/tischmid/.config/synth-shell/better-history.sh ] && [ -n "$( echo $- | grep i )" ] ; then
source /nfs/homes/tischmid/.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\-'\'\` '__________________________________'
}
timo_moulinette () {
# 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="./timo_moulinette_${subject}_${ex}_${test_file_name}"
# Download tesfile
curl -sL "${git_full}" -o "${download_file_name}"
# 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' "#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="timo_moulinette_${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
}