301 lines
7.9 KiB
Text
Executable file
301 lines
7.9 KiB
Text
Executable file
set --extended-glob
|
||
set --no-clobber
|
||
set --no-unset
|
||
|
||
. "$HOME/.alias"
|
||
|
||
if command --identify --builtin-command history >/dev/null; then
|
||
|
||
# don't save commands starting with a space in history
|
||
set --hist-space
|
||
|
||
# prevent clearing history by accident
|
||
history()
|
||
if [ -t 0 ] && (
|
||
for arg do
|
||
case "${arg}" in
|
||
(-[drsw]?* | --*=*) ;;
|
||
(-*c*) exit;;
|
||
esac
|
||
done
|
||
false
|
||
) then
|
||
printf 'history: seems you are trying to clear the whole history.\n' >&2
|
||
printf 'are you sure? (yes/no) ' >&2
|
||
case "$(head -n 1)" in
|
||
([Yy]*) command history "$@";;
|
||
(*) printf 'history: cancelled.\n' >&2;;
|
||
esac
|
||
else
|
||
command history "$@"
|
||
fi
|
||
|
||
fi
|
||
|
||
cp() if [ -t 0 ]; then command cp -i "$@"; else command cp "$@"; fi
|
||
mv() if [ -t 0 ]; then command mv -i "$@"; else command mv "$@"; fi
|
||
rm() if [ -t 0 ]; then command rm -i "$@"; else command rm "$@"; fi
|
||
|
||
# normally yash is more POSIX-compliant than /bin/sh :-)
|
||
sh() { yash --posix "$@"; }
|
||
yash() { command yash "$@"; }
|
||
# By re-defining 'yash' using the 'command' built-in, the 'jobs' built-in
|
||
# prints a command name that exposes the arguments like
|
||
# 'yash --posix -n foo.sh' rather than a command name that hides the
|
||
# arguments like 'yash --posix "${@}"'. This applies to the 'yash' command
|
||
# invoked via the 'sh' function.
|
||
|
||
# ensure job control works as expected
|
||
case $- in (*m*)
|
||
trap - TSTP TTIN TTOU
|
||
esac
|
||
|
||
# avoid removing existing crontab by accident
|
||
crontab()
|
||
if [ -t 0 ] && (
|
||
for arg do
|
||
case "${arg}" in
|
||
(-*r*) exit;;
|
||
esac
|
||
done
|
||
false
|
||
) then
|
||
printf 'crontab: seems you are trying to clear your crontab.\n' >&2
|
||
printf 'are you sure? (yes/no) ' >&2
|
||
case "$(head -n 1)" in
|
||
([Yy]*) command crontab "$@";;
|
||
(*) printf 'crontab: cancelled.\n' >&2;;
|
||
esac
|
||
else
|
||
command crontab "$@"
|
||
fi
|
||
|
||
# an alias that opens a file
|
||
if command --identify xdg-open >/dev/null 2>&1; then
|
||
alias o='xdg-open'
|
||
elif command --identify cmd.exe wslpath >/dev/null 2>&1; then
|
||
alias o='open'
|
||
open() (
|
||
target="$(wslpath -w "$1")"
|
||
cd /mnt/c # suppress UNC path warning
|
||
cmd.exe /c start "$target"
|
||
)
|
||
elif command --identify cygstart >/dev/null 2>&1; then
|
||
alias o='cygstart'
|
||
elif [ "$(uname)" = Darwin ] 2>/dev/null; then
|
||
alias o='open'
|
||
fi
|
||
|
||
# define some basic variables if missing
|
||
: ${PAGER:=less} ${EDITOR:=nvim} ${FCEDIT:=$EDITOR}
|
||
: ${LOGNAME:=$(logname)} ${HOSTNAME:=$(uname -n)}
|
||
|
||
# disable confusing treatment of arguments in the echo command
|
||
: ${ECHO_STYLE:=RAW}
|
||
|
||
# variables needed for command history
|
||
|
||
if ! [ "${HISTFILE-}" ]; then
|
||
HISTFILE=${XDG_STATE_HOME:-~/.local/state}/yash/history
|
||
fi
|
||
|
||
# create HISTFILE parent directory if missing
|
||
! [ -d "${HISTFILE%/*}" ] && mkdir -p "${HISTFILE%/*}"
|
||
|
||
HISTSIZE=5000
|
||
|
||
# emulate bash's $SHLVL
|
||
if [ "${_old_shlvl+set}" != set ]; then
|
||
_old_shlvl=${SHLVL-}
|
||
fi
|
||
SHLVL=$((_old_shlvl+1)) 2>/dev/null || SHLVL=1
|
||
export SHLVL
|
||
|
||
# initialize event handlers
|
||
COMMAND_NOT_FOUND_HANDLER=()
|
||
PROMPT_COMMAND=()
|
||
POST_PROMPT_COMMAND=()
|
||
YASH_AFTER_CD=()
|
||
|
||
# find escape sequence to change terminal window title
|
||
case "$TERM" in
|
||
(xterm|xterm[+-]*|gnome|gnome[+-]*|putty|putty[+-]*|cygwin)
|
||
_tsl='\033];' _fsl='\a' ;;
|
||
(*)
|
||
_tsl=$( (tput tsl 0; echo) 2>/dev/null |
|
||
sed -e 's;\\;\\\\;g' -e 's;;\\033;g' -e 's;;\\a;g' -e 's;%;%%;g')
|
||
_fsl=$( (tput fsl ; echo) 2>/dev/null |
|
||
sed -e 's;\\;\\\\;g' -e 's;;\\033;g' -e 's;;\\a;g' -e 's;%;%%;g') ;;
|
||
esac
|
||
|
||
# if terminal window title can be changed...
|
||
if [ "$_tsl" ] && [ "$_fsl" ]; then
|
||
|
||
# set terminal window title on each prompt
|
||
_set_term_title()
|
||
if [ -t 2 ]; then
|
||
printf "$_tsl"'%s@%s:%s'"$_fsl" "${LOGNAME}" "${HOSTNAME%%.*}" \
|
||
"${${PWD:/$HOME/\~}/#$HOME\//\~\/}" >&2
|
||
fi
|
||
PROMPT_COMMAND=("$PROMPT_COMMAND" '_set_term_title')
|
||
|
||
# reset window title when changing host or user
|
||
ssh() {
|
||
if [ -t 2 ]; then printf "$_tsl"'ssh %s'"$_fsl" "$*" >&2; fi
|
||
command ssh "$@"
|
||
}
|
||
su() {
|
||
if [ -t 2 ]; then printf "$_tsl"'su %s'"$_fsl" "$*" >&2; fi
|
||
command su "$@"
|
||
}
|
||
sudo() {
|
||
if [ -t 2 ]; then printf "$_tsl"'sudo %s'"$_fsl" "$*" >&2; fi
|
||
command sudo "$@"
|
||
}
|
||
doas() {
|
||
if [ -t 2 ]; then printf "$_tsl"'doas %s'"$_fsl" "$*" >&2; fi
|
||
command doas "$@"
|
||
}
|
||
|
||
fi
|
||
|
||
# define function that updates $_vcs_info and $_vcs_root
|
||
_update_vcs_info() {
|
||
typeset type branch
|
||
{
|
||
read --raw-mode type
|
||
read --raw-mode _vcs_root
|
||
read --raw-mode branch
|
||
} <(
|
||
exec 2>/dev/null
|
||
typeset COMMAND_NOT_FOUND_HANDLER=
|
||
while true; do
|
||
if [ -e .git ] || [ . -ef "${GIT_WORK_TREE-}" ]; then
|
||
printf 'git\n%s\n' "${GIT_WORK_TREE:-$PWD}"
|
||
git branch --no-color | sed -n '/^\*/s/^..//p'
|
||
exit
|
||
elif [ -d .hg ]; then
|
||
printf 'hg\n%s\n' "$PWD"
|
||
exec cat .hg/branch
|
||
elif [ -d .svn ]; then
|
||
printf 'svn\n'
|
||
_vcs_root=$(svn info --show-item=wc-root)
|
||
printf '%s\n' "$_vcs_root"
|
||
path=$(svn info --show-item=relative-url)
|
||
case $path in
|
||
(*/branches/*)
|
||
printf '%s\n' "${${path#*/branches/}%%/*}"
|
||
esac
|
||
exit
|
||
fi
|
||
if [ / -ef . ] || [ . -ef .. ]; then
|
||
exit
|
||
fi
|
||
\command cd -P ..
|
||
done
|
||
)
|
||
case "$type#$branch" in
|
||
(hg#default) _vcs_info='hg';;
|
||
(git#master) _vcs_info='git';;
|
||
(*# ) _vcs_info="$type";;
|
||
(* ) _vcs_info="$type@$branch";;
|
||
esac
|
||
}
|
||
# update $_vcs_info on each prompt
|
||
PROMPT_COMMAND=("$PROMPT_COMMAND" '_update_vcs_info')
|
||
|
||
# Firstly, load the common customization script.
|
||
# If you don't like settings applied in this script, remove this line.
|
||
#. --autoload --no-alias initialization/common
|
||
|
||
# These are additional aliases that are not defined in the common script.
|
||
# Uncomment to enable them.
|
||
alias ls='ls --color=auto'
|
||
alias gitlog='git log --graph --branches --oneline'
|
||
alias sc='shellcheck -s sh'
|
||
|
||
# And add your own customization below.
|
||
|
||
|
||
|
||
# Prompt:
|
||
if [ -n "${SSH_CONNECTION-}" ]; then
|
||
_hc='\fy.' # yellow hostname for SSH remote
|
||
else
|
||
_hc='\fg.' # green hostname for local
|
||
fi
|
||
|
||
if [ "$(id -u)" -eq 0 ]; then
|
||
_uc='\fr.' # red username for root
|
||
_2c='\fr.' # red PS2 for root
|
||
else
|
||
_uc='\fy.' # same username color as hostname for non-root user
|
||
_2c= # PS2 in normal color for non-root user
|
||
fi
|
||
_dc='\fc.'
|
||
|
||
YASH_PS1=$_uc'${LOGNAME}'$_hc'@${HOSTNAME%%.*}\fd. '$_dc'${PWD/#$HOME/\~} ${{?:/0/}:+\\fr.$?\\fd. }'$_uc'\$ '
|
||
YASH_PS1R='\fm.${_vcs_info}'
|
||
YASH_PS1S='\fo.'
|
||
YASH_PS2=$_2c'> '
|
||
#YASH_PS2R=
|
||
YASH_PS2S=$YASH_PS1S
|
||
YASH_PS4='\fm.+ '
|
||
YASH_PS4S='\fmo.'
|
||
unset _hc _uc _2c _dc
|
||
# No escape sequences allowed in the POSIXly-correct mode.
|
||
PS1='${LOGNAME}@${HOSTNAME%%.*} '$PS1
|
||
|
||
_update_direnv() {
|
||
eval "$(
|
||
direnv export json |
|
||
jq -r 'to_entries | .[] |
|
||
if .value == null then
|
||
@sh "unset \(.key)"
|
||
else
|
||
@sh "export \(.key)=\(.value)"
|
||
end'
|
||
)"
|
||
}
|
||
_update_direnv
|
||
YASH_AFTER_CD=("$YASH_AFTER_CD" '_update_direnv')
|
||
|
||
replace() {
|
||
[ "$#" -eq 0 ] && { printf 'Usage: replace <command> [args...]\n' >&2; return 1; }
|
||
|
||
# Get the tty device
|
||
TTY=$(tty) || { printf 'Failed to get tty\n' >&2; return 1; }
|
||
|
||
# Ensure the script is running in an interactive terminal
|
||
case "$TTY" in
|
||
/dev/tty*|/dev/pts/*) ;;
|
||
*) printf 'Not running in a valid terminal\n' >&2; return 1 ;;
|
||
esac
|
||
|
||
# Detach from the current terminal and execute the command
|
||
nohup "$@" </dev/null >"$TTY" 2>&1 &
|
||
disown
|
||
exit
|
||
}
|
||
|
||
disowned() {
|
||
[ "$#" -eq 0 ] && { printf 'Usage: disowned <command> [args...]\n' >&2; return 1; }
|
||
|
||
# Get the tty device
|
||
TTY=$(tty) || { printf 'Failed to get tty\n' >&2; return 1; }
|
||
|
||
# Ensure the script is running in an interactive terminal
|
||
case "$TTY" in
|
||
/dev/tty*|/dev/pts/*) ;;
|
||
*) printf 'Not running in a valid terminal\n' >&2; return 1 ;;
|
||
esac
|
||
|
||
# Detach from the current terminal and execute the command
|
||
nohup "$@" </dev/null >"$TTY" 2>&1 &
|
||
disown
|
||
}
|
||
|
||
alias foliate='replace foliate'
|
||
alias vpager="nvim -R -c 'setlocal nobackup noswapfile nowrap' -"
|
||
export LESS='-R --mouse --wheel-lines=3'
|
||
. "$HOME/.cargo/env"
|