Ben Grande d13a21a734
fix: avoid echo usage
Echo can interpret operand as an option and checking every variable to
be echoed is troublesome while with printf, if the format specifier is
present before the operand, printing as string can be enforced.
2024-08-06 18:12:46 +02:00

143 lines
3.6 KiB
Bash

#!/usr/bin/env bash
## SPDX-FileCopyrightText: 2023 Benjamin Grande M. S. <ben.grande.b@gmail.com>
##
## SPDX-License-Identifier: AGPL-3.0-or-later
## {{{ Requirements
## If not running interactively, return.
case $- in
*i*) ;;
*) return;;
esac
## Source default files.
# shellcheck disable=SC1090,SC1091
source "${HOME}/.profile"
# shellcheck disable=SC1090
source "${ENV}"
## }}}
## {{{ Options
HISTCONTROL=ignoredups
shopt -s autocd
shopt -s direxpand
shopt -s cdspell
shopt -s dirspell
shopt -s histappend
shopt -s checkwinsize
shopt -s extglob
shopt -s globstar
## }}}
## {{{ Alias
alias reload="exec bash"
## }}}
## {{{ Prompt
if test -z "${debian_chroot:-}" && test -r /etc/debian_chroot; then
debian_chroot="$(cat -- /etc/debian_chroot)"
fi
_reset_line() {
case "${TERM-}" in
""|dumb|linux*|vt100*|vt220*) return;;
*) ;;
esac
## Credit: Can't find the source, posted on StackExchange or alike.
## Does not work well on Bash 5.0 and older.
bash_version_clean="$(printf '%s\n' "${BASH_VERSION%.*}" | tr -d ".")"
if test "${bash_version_clean}" -lt 51; then
unset bash_version_clean
return
fi
unset bash_version_clean
## Ask the terminal for any pending (line buffered) input.
termios="$(stty -g)" && stty -icanon && stty "${termios}"
unset termios
## On pending input, assume it's been echoed and we're not in first column.
## Otherwise ask the terminal for current column and read it from input.
if read -t 0 || {
IFS='[;' read -s -r -d'R' -p$'\033[6n' _ _ cur_y && [[ ${cur_y} != 1 ]]
}
then
unset cur_y
## Print line ending char with reversed video and end with newline.
if test "${color_prompt-}" = "yes"; then
printf '%b' "\033[41m\033[0m\033[7m%\033[m\n\r"
else
printf '%b' "%\n\r"
fi
fi
}
_print_ec(){
test "${_ec_ps1}" = "0" && return
if test "${color_prompt:-}" = "yes"; then
printf '%b%s%b' "(\001\033[31m\002" "${_ec_ps1}" "\001\033[0m\002)"
else
printf '%s' "(${_ec_ps1})"
fi
}
_save_ec() { _ec_ps1=$?; }
PROMPT_COMMAND=(_save_ec)
newline=$'\n'
# shellcheck disable=SC2154
if test "${color_prompt:-}" = "yes"; then
# shellcheck disable=SC2154
PS1="\$(_reset_line)\$(resize-terminal)\[\033[35m\][\[${reset_color}\]"
PS1="${PS1}${debian_chroot:+(${debian_chroot})}\[${usercolor}\]\u@\h "
PS1="${PS1}\[${dircolor}\]\w\[${reset_color}\]\$(_git_prompt_info)"
PS1="${PS1}\[\033[35m\]]\[${reset_color}\]${newline-}\$(_print_ec)"
PS1="${PS1}${ps1_symbol} "
else
PS1="\$(_reset_line)\$(resize-terminal)"
PS1="${PS1}[${debian_chroot:+(${debian_chroot})}"
PS1="${PS1}\u@\h:\w\$(_git_prompt_info)]${newline-}\$(_print_ec)"
PS1="${PS1}${ps1_symbol} "
fi
case "${TERM-}" in
screen*|xterm*|rxvt*)
## Set window title
PS1="\[\e]0;${debian_chroot:+(${debian_chroot})}\u@\h: \w\a\]${PS1}"
;;
*) ;;
esac
case "${TERM-}" in
""|dumb|linux*|vt100*|vt220*)
PS0=""
;;
*)
## Reset cursor to steady block after command input and before execution.
# shellcheck disable=SC2034
PS0="\e[2 q\2"
;;
esac
if ! shopt -oq posix; then
source_readable /usr/share/bash-completion/bash_completion
fi
unset newline ps1_symbol
## }}}
## {{{ Plugins
if has zoxide; then
zoxide_bash_completion="$(zoxide init bash)"
eval "${zoxide_bash_completion}"
unset zoxide_bash_completion
fi
if has gitlint; then
gitlint_bash_completion="$(_GITLINT_COMPLETE=bash_source gitlint)"
eval "${gitlint_bash_completion}"
unset gitlint_bash_completion
fi
source_readable /usr/share/doc/fzf/examples/key-bindings.bash
source_readable /usr/share/doc/fzf/examples/completion.bash
## }}}
## {{{ End
## Source local bash configuration.
source_readable "${HOME}/.bashrc.local"
## }}}