dotfiles/.bashrc

218 lines
6.3 KiB
Bash
Raw Normal View History

2014-06-22 16:16:14 +00:00
# If not running interactively, don't do anything more
[ -z "$PS1" ] && return
DEBEMAIL=julien
DEBEMAIL=$DEBEMAIL@
DEBEMAIL=${DEBEMAIL}palard.fr
DEBFULLNAME="Julien Palard"
PATH="$HOME/.local/bin:$PATH"
2014-06-22 16:16:14 +00:00
shopt -s cdspell
shopt -s dirspell
shopt -s autocd
shopt -s globstar
2014-06-22 16:16:14 +00:00
shopt -s nocaseglob
2018-08-12 09:51:32 +00:00
if [ -n "$DISPLAY" ]
then
2016-01-04 09:53:00 +00:00
xset b off
fi
2014-06-22 16:16:14 +00:00
# http://nion.modprobe.de/blog/archives/572-less-colors-for-man-pages.html
export LESS_TERMCAP_mb=$'\E[01;31m' # debut de blink
export LESS_TERMCAP_md=$'\E[01;31m' # debut de gras
export LESS_TERMCAP_me=$'\E[0m' # fin
export LESS_TERMCAP_so=$'\E[01;44;33m' # début de la ligne d'état
export LESS_TERMCAP_se=$'\E[0m' # fin
export LESS_TERMCAP_us=$'\E[01;32m' # début de souligné
export LESS_TERMCAP_ue=$'\E[0m' # fin
export DEBEMAIL DEBFULLNAME
export EDITOR=emacs
export HISTCONTROL=ignoredups
export HISTFILESIZE=5000
export HISTSIZE=5000
umask 022
eval "`dircolors`"
HOSTNAME_SUM=$(cksum <(hostname) | cut -d' ' -f1)
2014-06-22 16:16:14 +00:00
HOSTNAME_BOLD=$(( ($HOSTNAME_SUM + 1) % 2))
HOSTNAME_HUE=$(( ($HOSTNAME_SUM + 3) % 6 + 31))
USERNAME_SUM=$(($(cksum <(whoami) | cut -d' ' -f1) + 5)) # + 5 so root gots red.
2014-06-22 16:16:14 +00:00
USERNAME_BOLD=$(( ($USERNAME_SUM + 1) % 2))
USERNAME_HUE=$(( ($USERNAME_SUM + 2) % 6 + 31))
HOSTNAME_COLOR=$'\E'"[$HOSTNAME_BOLD;${HOSTNAME_HUE}m"
USERNAME_COLOR=$'\E'"[$USERNAME_BOLD;${USERNAME_HUE}m"
WHITE=$'\E[00m'
# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(lesspipe)"
2018-08-12 09:54:14 +00:00
2018-09-25 22:34:18 +00:00
if ! [ -f "$HOME/.git-prompt.sh" ]
2014-09-17 10:19:39 +00:00
then
2018-09-25 22:34:18 +00:00
wget -q -O "$HOME/.git-prompt.sh" https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh
2014-09-17 10:19:39 +00:00
fi
2017-08-30 08:05:14 +00:00
2017-04-29 09:28:30 +00:00
alias ls='ls --color=auto'
2014-06-22 16:16:14 +00:00
alias fingerprint='find /etc/ssh -name "*.pub" -exec ssh-keygen -l -f {} \;'
2021-10-05 02:19:20 +00:00
alias rekey='ssh-add -e /usr/lib/x86_64-linux-gnu/opensc-pkcs11.so >/dev/null 2>&1; ssh-add -s /usr/lib/x86_64-linux-gnu/opensc-pkcs11.so'
2014-06-22 16:16:14 +00:00
2019-10-16 14:02:39 +00:00
export PYTHONDEVMODE=y
2018-08-12 09:56:25 +00:00
for extra in /etc/bash_completion ~/.bash_aliases ~/.my_bashrc ~/.git-prompt.sh
do
if [ -f "$extra" ]
then
. $extra
fi
done
2014-06-22 16:16:14 +00:00
2020-07-28 20:32:53 +00:00
title()
{
local TITLE="\[\e]0;$1\a\]"
2020-07-28 20:32:53 +00:00
local PREV_FAIL="\`PREV_FAIL=\$?; if [ \$PREV_FAIL != 0 ]; then echo \[\e[31m\]\$PREV_FAIL \[\e[0m\]; fi\`"
PS1="$TITLE$PREV_FAIL\[$USERNAME_COLOR\]\u\[$WHITE\]@\[$HOSTNAME_COLOR\]\H\[$WHITE\]:\[\e[32m\]\w\[$WHITE\]"'$(__git_ps1 " (%s)")\n\$ '
2020-07-28 20:32:53 +00:00
}
[ "$TERM" != 'linux' -a z"$TERM" != z'eterm-color' ] && DEFAULT_TITLE="\H \W" || DEFAULT_TITLE=''
2020-07-28 20:32:53 +00:00
title "$DEFAULT_TITLE"
2014-06-22 16:16:14 +00:00
jsonpp()
{
input="$([ $# -gt 0 ] && printf "%s\n" "$*" || cat -)"
if ! [ z"$(which pygmentize)" = z"" ]
then
2017-04-29 09:28:30 +00:00
printf "%s" "$input" | python3 -mjson.tool | pygmentize -l js || printf "%s\n" "$input"
2014-06-22 16:16:14 +00:00
else
2017-04-29 09:28:30 +00:00
printf "%s" "$input" | python3 -mjson.tool || printf "%s\n" "$input"
2014-06-22 16:16:14 +00:00
fi
}
urldecode()
{
input="$([ $# -gt 0 ] && printf "%s\n" "$*" || cat -)"
2017-04-21 12:36:05 +00:00
python3 -c "import urllib.parse, sys; print(urllib.parse.unquote(sys.argv[1]))" "$input"
2014-06-22 16:16:14 +00:00
}
urlencode()
{
input="$([ $# -gt 0 ] && printf "%s\n" "$*" || cat -)"
2017-04-21 12:36:05 +00:00
python3 -c "import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1]))" "$input"
2014-06-22 16:16:14 +00:00
}
# Removes *~ and #*# files in curent folder, for a depth limited to 3 folders.
clean()
{
find -maxdepth 3 -name .emacs_backups -prune \
-o \( -type f -a \
\( -name '*~' -o -name '#*#' \) \
\) \
-print0 | xargs -0 rm -f
}
unalias venv 2>/dev/null
# Usage: `venv` to create a venv with the current `python` version.
# `venv 3.8` to create a venv with given version.
venv()
{
deactivate 2>/dev/null
if ! [[ -d .venv ]]
then
python$1 -m venv --prompt "$(basename "$PWD"))(py$(python$1 --version | cut -d' ' -f2)" .venv
fi
source .venv/bin/activate
# python -m pip install --upgrade --pre black jedi wheel pip
}
dotfiles()
{
local CLONE="$HOME/.config/dotfiles-repo/"
mkdir -p ~/.config/
if [ -d "$CLONE" ]
then
git -C "$CLONE" pull --ff-only
else
git clone -q https://github.com/JulienPalard/dotfiles.git "$CLONE"
fi
"$CLONE"/interactive_copy.py "$CLONE" ~/ --exclude README.md .git interactive_copy.py install.sh __pycache__
2018-08-12 10:04:59 +00:00
rm -f "$HOME/.git-prompt.sh"
wget -q -O "$HOME/.git-prompt.sh" https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh
}
2018-09-25 17:30:27 +00:00
2019-06-18 12:47:55 +00:00
pasee()
{
local LOGIN
local PASSWORD
2019-06-25 14:32:25 +00:00
local HOST="${1:-https://id.meltygroup.com/tokens/?idp=meltygroup}"
2019-07-02 14:14:20 +00:00
local TEMP_DIR="$(mktemp --directory --suffix=pasee)"
2019-06-18 12:47:55 +00:00
read -p 'Login: ' LOGIN
read -s -p "Password for $LOGIN: " PASSWORD
echo
2019-07-02 14:14:20 +00:00
curl -w '%{stderr}%{http_code}' -s -XPOST -d '{"login": "'"$LOGIN"'", "password": "'"$PASSWORD"'"}' "$HOST" > "$TEMP_DIR/stdout" 2> "$TEMP_DIR/stderr"
local HTTP_RESPONSE="$(<$TEMP_DIR/stdout)"
local STATUS_CODE="$(<$TEMP_DIR/stderr)"
JWT="$(jq -r ".access_token" <<< "$HTTP_RESPONSE")"
2019-07-02 14:15:52 +00:00
if [[ -z "$JWT" || "$STATUS_CODE" != "201" || "$JWT" == "null" ]]; then
2019-07-02 14:14:20 +00:00
printf "HTTP Error %s: %s\n" "$STATUS_CODE" "$HTTP_RESPONSE"
fi
2019-06-18 12:47:55 +00:00
AUTH="Authorization: Bearer $JWT"
2019-07-02 14:14:20 +00:00
rm -fr "$TEMP_DIR"
2019-06-18 12:47:55 +00:00
}
2019-06-25 14:32:25 +00:00
wyz()
{
curl https://wyz.fr/ -F"${1##*.}=@$1"
}
2019-10-17 08:18:37 +00:00
compile_python()
{
# Inspired from the great https://gitlab.com/python-devs/ci-images/
# Thanks Barry Warsaw.
local PY_VERSION="$1"
2020-06-25 15:33:21 +00:00
local BETA="$2"
local FLAGS=""
if dpkg --compare-versions "$PY_VERSION" ge 3.8.0 # Since 3.8.0 debug builds are ABI compatible, let's use them.
then
FLAGS="--with-pydebug"
fi
2019-10-17 08:50:27 +00:00
local URL="https://www.python.org/ftp/python"
(
cd /tmp
2020-06-25 15:33:21 +00:00
wget -qO- $URL/$PY_VERSION/Python-$PY_VERSION$BETA.tgz | tar -xzf - || (
2019-10-17 08:50:27 +00:00
echo "Version not found, check on $URL."
)
[ -d Python-$PY_VERSION$BETA ] && (cd Python-$PY_VERSION$BETA; ./configure $FLAGS --prefix=$HOME/.local/ && make -j $(nproc) && make altinstall) &&
2020-06-25 15:33:21 +00:00
rm -r Python-$PY_VERSION$BETA
2019-10-17 08:18:37 +00:00
)
}
2019-10-17 08:50:27 +00:00
compile_all_pythons()
{
2020-10-07 09:29:41 +00:00
compile_python 3.5.10 &
2021-09-19 10:02:11 +00:00
compile_python 3.6.15 &
compile_python 3.7.12 &
compile_python 3.8.12 &
compile_python 3.9.7 &
2021-10-05 02:19:29 +00:00
compile_python 3.10.0 &
2021-10-06 16:17:22 +00:00
compile_python 3.11.0 a1
wait
2019-10-17 08:50:27 +00:00
}
_compile_python()
{
COMPREPLY=( $( compgen -W '$( command curl -s https://www.python.org/ftp/python/ | grep -o ">[0-9.]\+/<" | sed "s/^>//;s|/<$||" )' -- "${COMP_WORDS[COMP_CWORD]}") )
}
2021-09-29 19:06:57 +00:00
myip()
{
dig +short myip.opendns.com @resolver1.opendns.com
}
2019-10-17 08:50:27 +00:00
complete -F _compile_python compile_python