Ben Grande a7d2876bb4
fix: standardize shell escape character to octal
URxvt requires octal for keysym while others do not and querying a
single one is easier.
2025-01-22 17:42:01 +01:00

348 lines
9.4 KiB
Bash

#!/bin/sh
## SPDX-FileCopyrightText: 2004 - 2022 Tim Pope <https://tpo.pe>
## SPDX-FileCopyrightText: 2023 - 2024 Benjamin Grande M. S. <ben.grande.b@gmail.com>
##
## SPDX-License-Identifier: AGPL-3.0-or-later
## Credits: https://github.com/tpope/dotfiles/blob/master/.shrc
## {{{ Requirements
## If not running interactively, return.
case $- in
*i*) ;;
*) return;;
esac
## Source profile.
# shellcheck disable=SC1091
. "${HOME}/.profile"
## }}}
## {{{ Options
stty -ixon
set -o noclobber
set -o notify
#stty quit undef
stty werase undef
## }}}
## {{{ Alias
## Standard commands.
test -n "${VISUAL-}" && alias vi='${VISUAL}'
alias du="du -h"
alias df="df -h"
alias cp="cp -i"
alias mv="mv -i"
alias ls="ls -hF --group-directories-first --color=auto"
if has lsblk; then
_lsblk_options="NAME,FSTYPE,LABEL,SIZE,FSAVAIL,FSUSE%,MOUNTPOINT"
alias lsblk='lsblk -o ${_lsblk_options}'
alias lsblku='lsblk -o ${_lsblk_options},UUID,PARTUUID'
fi
if ! grep --color 2>&1 | grep -qE -e "(unrecognized|unknown) option" &&
! grep --exclude 2>&1 | grep -qE -e "(unrecognized|unknown) option"
then
alias grep="grep --exclude='.*.swp' --exclude='*~' --color=auto"
fi
alias fgrep="grep -F"
alias egrep="grep -E"
alias diff="diff --color=auto"
has dir && alias dir="dir --color=auto"
has vdir && alias vdir="vdir --color=auto"
alias ip="ip -color=auto"
has highlight && alias highlight="highlight --out-format=ansi"
has pygmentize && alias pygmentize="pygmentize -f terminal"
## Helpers.
alias reload='. ${ENV}' r="reload"
if test -f "${XDG_CONFIG_HOME:-${HOME}/.config}/x11/xprofile"; then
alias reload-xprofile='. ${XDG_CONFIG_HOME:-${HOME}/.config}/x11/xprofile'
alias rx="reload-xprofile"
fi
cd_up(){
# SPDX-FileCopyrightText: 2014 Grigory K <https://stackoverflow.com/users/2937875/grigory-k>
# SPDX-License-Identifier: CC-BY-SA-3.0
# Credits: https://stackoverflow.com/a/26134858
case "${1-}" in
[1-9])
has seq || return
# shellcheck disable=SC2312
cd "$(printf '%0.0s../' $(seq 1 "${1}"))" || return
;;
"")
cd .. || return;;
*)
# shellcheck disable=SC2312
cd "$(pwd | sed -r "s|(.*/${1}[^/]*/).*|\1|")" || return
;;
esac
}
alias up="cd_up"
## POSIX cd does not allow '--' and other shells breaks when not using it.
alias -- -="cd -" 2>/dev/null || true
test -n "${EDITOR-}" && alias e='${EDITOR}'
test -n "${VISUAL-}" && alias v='${VISUAL}'
alias j="jobs"
alias h="history"
alias l="ls -CF"
alias ll="ls -Fl"
alias la="ls -Fa"
alias lla="ls -Fla"
alias laa='ls -Fd .* 2>/dev/null'
alias llaa='ls -Fdl .* 2>/dev/null'
alias lr='ls -R'
alias lx='ll -BX'
alias lz='ll -rS'
alias lt='ll -rt'
alias lm='la | more'
alias now="date +%a\ %Y-%m-%d\ %T"
alias mkd="mkdir -pv"
mkcd(){ mkd "$1" && cd "$1" || return; }
## Non-standard programs.
has info && alias info="info --vi-keys"
has htop && alias top="htop"
has yt-dlp && alias yt="yt-dlp --add-metadata -ic"
has fdfind && alias fd="fdfind"
has lynx && alias lynx="lynx -vikeys"
has qubesctl && {
alias qctl="qubesctl"
alias qctls="qctl state.apply"
alias qctlu="qctl --skip-dom0 --targets"
qctlus(){
test -n "${2-}" || return 1
qubesctl --skip-dom0 --targets "${1}" state.apply "${2}"
}
}
has gpg && alias gpgs="gpg --show-keys"
## {{{ Tmux
has tmux && {
alias t="tmux"
alias td="t detach"
alias tls="t list-sessions" tl="tls"
alias tlw="t list-windows"
alias tlp="t list-panes"
alias tcl="clear && t clear-history" # clear buffer cus memory
tcla(){
## Clear all the panes of the current session.
for _t_pane in $(tmux list-panes -s -F '#{pane_id}'); do
tmux clear-history -t "${_t_pane}"
done
}
}
## }}}
## {{{ Mutt
if has mutt; then
alias m="mutt"
alias mp="mutt -e 'set signature= pgp_autoinline=yes crypt_autosign=yes' -H"
fi
## }}}
## {{{ Git
if { ! test -f /usr/share/whonix/marker && has git; } ||
{ test -f /usr/share/whonix/marker && git -v >/dev/null 2>&1; }
then
## Whonix UWT wrapper circumvents the command existence check.
alias g="git"
alias gcd='cd "$(git rev-parse --show-toplevel)"'
## Use git aliases with 'g' prefix.
g_alias="$(git config --get-regexp 'alias.*' | sed 's/^alias\.//;s/ .*//')"
for key in $(printf '%s\n' "${g_alias}"); do
# shellcheck disable=SC2139,SC2140
alias "g${key}"="git ${key}"
done
unset g_alias
fi
## }}}
## }}}
## {{{ Prompt
## Set ability to color terminal.
# shellcheck disable=2034
case "${TERM-}" in
iterm|*-truecolor)
export COLORTERM=truecolor; color_prompt=yes
;;
*-color|*-256color|linux*)
color_prompt=yes
;;
""|dumb|vt100*|vt220*)
color_prompt=no
;;
*)
color_prompt=no
;;
esac
## Colorise "ls" output.
if test "${color_prompt-}" = "yes"; then
if has dircolors; then
## Use dircolors if available.
if test -r "${XDG_CONFIG_HOME}/dircolors/dircolors"; then
dircolors="$(dircolors -b "${XDG_CONFIG_HOME}/dircolors/dircolors")"
eval "${dircolors}"
else
dircolors="$(dircolors -b)"
eval "${dircolors}"
fi
unset dircolors
## Fix bold on some terminals.
case "${TERM-}" in
xterm*|screen*)
LS_COLORS="$(printf '%s\n' "${LS_COLORS}" | sed -e 's/01;3/00;9/g')"
;;
*) ;;
esac
export LS_COLORS
else
## BSD
CLICOLOR=1 CLICOLOR_FORCE=1 LSCOLORS=ExGxFxdxCxfxDxxbadacad
export CLICOLOR CLICOLOR_FORCE LSCOLORS
fi
## Set gcc colors.
GCC_COLORS="error=00;31:warning=01;35:note=01;36:caret=01;32:locus=01"
GCC_COLORS="${GCC_COLORS}:quote=01"
export GCC_COLORS
## Colors for programs that uses less such as 'man'.
GROFF_NO_SGR=1
LESS_TERMCAP_mb="$(tput bold; tput setaf 2)" # begin blink
LESS_TERMCAP_md="$(tput bold; tput setaf 6)" # begin bold
LESS_TERMCAP_me="$(tput sgr0)" # reset bold/blink
LESS_TERMCAP_so="$(tput bold; tput setaf 7; tput setab 4)" # begin rev video
LESS_TERMCAP_se="$(tput rmso; tput sgr0)" # reset rev video
LESS_TERMCAP_us="$(tput smul; tput bold; tput setaf 2)" # begin underline
LESS_TERMCAP_ue="$(tput rmul; tput sgr0)" # reset underline
LESS_TERMCAP_mr="$(tput rev)" # revert
LESS_TERMCAP_mh="$(tput dim)" # dimerize
export GROFF_NO_SGR
export LESS_TERMCAP_mb LESS_TERMCAP_md LESS_TERMCAP_me
export LESS_TERMCAP_so LESS_TERMCAP_se LESS_TERMCAP_us
export LESS_TERMCAP_ue LESS_TERMCAP_mr LESS_TERMCAP_mh
fi
# shellcheck disable=SC3028
_get_prompt_time(){
test -n "${SECONDS:-}" || return
# shellcheck disable=SC2154
_ptime="$((SECONDS-_saved_prompt_time))"
printf '%02d:%02d:%02d' \
"$((_ptime/3600))" "$(((_ptime%3600)/60))" "$((_ptime%60))"
}
_git_prompt_info(){
has git || return
## Whonix UWT wrapper circumvents the command existence check.
if test -f /usr/share/whonix/marker; then
git -v >/dev/null 2>&1 || return
fi
ref="$(git symbolic-ref HEAD 2>/dev/null | cut -d '/' -f3)"
test "${ref-}" ||
ref="$(git describe --tags --exact-match HEAD 2>/dev/null)"
test "${ref-}" || ref="$(git rev-parse HEAD 2>/dev/null | cut -c 1-7)"
test "${ref-}" || return
# shellcheck disable=2039,3003
case "${TERM-}" in
*-256color|xterm-kitty) branchcolor=$'\033[38;5;31m' ;;
*-88color|rxvt-unicode) branchcolor=$'\033[38;5;22m' ;;
*) branchcolor=$'\033[0;94m' ;;
esac
if test -n "${ZSH_VERSION-}"; then
# shellcheck disable=2016
if test "${color_prompt}" = "yes"; then
print -Pn '(%%{${branchcolor}%%}%20>...>${ref}%<<%%{\033[0m%%})'
else
print -Pn '(%20>...>${ref}%<<)'
fi
else
if test "${color_prompt}" = "yes"; then
printf '%s' "(${branchcolor}${ref}${reset_color})"
else
printf '%s' "(${ref})"
fi
fi
unset ref
}
# shellcheck disable=SC2034
reset_color="$(printf '%b' '\033[0m')"
# shellcheck disable=SC2034
## Use echotc Co? Only available by default for zsh, not bash.
case "${TERM-}" in
*-256color|xterm-kitty)
usercolor="$(printf '%b' "\033[38;5;184m")"
dircolor="$(printf '%b' "\033[38;5;27m")"
;;
*-88color|rxvt-unicode)
usercolor="$(printf '%b' "\033[38;5;56m")"
dircolor="$(printf '%b' "\033[38;5;23m")"
;;
*)
usercolor="$(printf '%b' "\033[0;93m")"
dircolor="$(printf '%b' "\033[1;34m")"
;;
esac
case "${ZSH_VERSION-}" in
"") ps1_symbol="$";;
*) ps1_symbol="%%";;
esac
# shellcheck disable=SC2034
uid="$(id -u)"
if test "${uid}" = "0"; then
usercolor="$(printf '%b' "\033[0;97m")"
ps1_symbol="#"
fi
unset uid
## Do not colorize this prompt, Sh fails to count the correct number of
## characters on the prompt leading to problems.
user="$(id -un)"
if test "${color_prompt-}" = "yes"; then
PS1="[${usercolor}${user} $(hostname -s)${reset_color}]${ps1_symbol} "
else
PS1="${user}@$(hostname -s)${ps1_symbol} "
fi
## }}}
## {{{ External Functions
## Source file if it is readable.
## Usage: source_readable FILE
source_readable(){
for _file in "${@}"; do
# shellcheck disable=SC1090
! test -r "${_file}" || . "${_file}"
done
}
_fzf_comprun() {
_fzf_command="$1"
shift
case "${_fzf_command}" in
cd) if has tree; then
fzf --preview 'tree -C -- {} | head -200' "${@}"
else
fzf "${@}"
fi
;;
export|unset)
fzf --preview "eval 'printf '%s\n' \$'{}" "${@}"
;;
ssh)
if has dig; then
fzf --preview 'dig {}' "${@}"
fi
;;
*)
fzf --preview 'test -d {} || cat -- {}' "${@}"
;;
esac
}
## }}}
## {{{ End
## Source local shell configuration file.
source_readable "${HOME}/.shrc.local"
## }}}