home/.profile
Salt f90c82bfcf .profile: Add dircolors support, make safe for POSIX sh
Though it's still not quite where I want it
2018-12-01 11:31:29 -06:00

228 lines
6.2 KiB
Bash
Executable File

#!/bin/sh
# Copyright (c) 2017 rehashedsalt/vintagesalt
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# POSIX NOTICE
# This script should be fully POSIX-compliant
# If it is not, open a bug report at gitlab.com/rehashedsalt/home and I'll take
# care of it
# SHELLCHECK
# Not finding these sources is none of my concern; they're out of scope
# shellcheck disable=1091
# shellcheck disable=1090
# I'm well aware of when functions are defined vs used
# Those choices are deliberate
# shellcheck disable=2139
# shellcheck disable=2016
# Quit being pedantic
# shellcheck disable=1117
# Environment variables
# First, what shell are we?
_baseshell="$(basename "$0")"
# Use Vim if we have it
export EDITOR="vi"
command -v vim > /dev/null 2>&1 && export EDITOR="vim"
alias e='$EDITOR'
# Patch PATH
desiredpath="/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/usr/local/games:/usr/games:$HOME/.bin:$HOME/.local/bin"
# Here, we eliminate the parts of the environment-supplied PATH that we already
# plan on using
IFS=":"
for path in $desiredpath; do
PATH="${PATH#*$path}"
done
unset IFS
# We remove any prepending colons to make it appear neater
while [ "${PATH#:}" != "$PATH" ]; do
PATH="${PATH#:}"
done
# Then finally set up our path
PATH="$desiredpath:$PATH"
# Grab dircolors, if it exists
if command -v dircolors > /dev/null 2>&1; then
dircolorsfile="$HOME/.config/dircolors"
if [ -r "$dircolorsfile" ]; then
eval "$(dircolors "$dircolorsfile")"
else
eval "$(dircolors -b)"
fi
fi
# Grab bash_completion, if it exists
[ -f "/etc/profile.d/bash_completion.sh" ] && . "/etc/profile.d/bash_completion.sh"
# Grab pip completion, if it exists
if command -v pip > /dev/null 2>&1; then
case "$_baseshell" in
*bash)
if ! [ -f "$HOME/.pip-completion-bash" ]; then
pip completion --bash > "$HOME/.pip-completion-bash"
echo ".profile: Created pip completion for bash"
fi
. "$HOME/.pip-completion-bash"
;;
zsh)
if ! [ -f "$HOME/.pip-completion-zsh" ]; then
pip completion --zsh > "$HOME/.pip-completion-zsh"
echo ".profile: Created pip completion for zsh"
fi
. "$HOME/.pip-completion-zsh"
;;
*)
;;
esac
fi
# If emerge exists, add emerge aliases
if command -v emerge > /dev/null 2>&1 ; then
alias e-depclean='sudo emerge -a --depclean'
alias e-inst='sudo emerge -a --jobs --tree --quiet-build y'
alias e-upgrade='sudo emerge -DNUua --jobs --tree --quiet-build y --with-bdeps=y --keep-going --backtrack=1000 @world'
alias e-newuse='sudo emerge -Uva --jobs --tree --quiet-build y @world'
alias e-search='emerge -s'
alias e-sync='sudo emerge --sync'
if command -v eclean > /dev/null 2>&1; then
alias e-cleanup='sudo eclean -d distfiles && sudo eclean -d packages'
fi
fi
# If we have instantmusic, add aliases for that
if command -v instantmusic > /dev/null 2>&1; then
alias song='instantmusic -p -s'
alias songp='instantmusic -s'
fi
# Alias for the dotfile setup
if [ -d "$HOME/.dotfiles" ]; then
dotcmd='git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME'
alias dot="$dotcmd"
alias dotupdate="\
printf 'Pulling...\n'; \
$dotcmd pull
printf 'Updating submodules...\n'; \
$dotcmd submodule update --init; \
printf 'Checking out masters...\n'; \
$dotcmd submodule -q foreach --recursive 'git checkout -q master && git pull' && \
$dotcmd status"
fi
# Aliases for common utilities
if [ "$(uname)" = "Linux" ]; then
# Assume we have GNU coreutils
lsarguments='--color=auto --group-directories-first'
alias l="ls -CF --file-type $lsarguments"
alias la="ls -AF --file-type $lsarguments"
alias ls="ls -F $lsarguments"
alias ll="ls -AhlF --file-type $lsarguments"
unset lsarguments
alias rm='rm -I'
else
# Else only assume POSIX/BSD
lsarguments='-F'
alias l="ls -$lsarguments"
alias la="ls -A $lsarguments"
alias ls="ls $lsarguments"
alias ll="ls -Ahl $lsarguments"
fi
alias cp='cp -i'
# Miscellaneous aliases
alias todo='$EDITOR "$HOME/Documents/todo"'
alias waitwhat='echo $?'
# Source ~/.functions, if it exists
[ -r "$HOME/.functions" ] && [ "$_baseshell" != "sh" ] && . "$HOME/.functions"
# Set up a default PS1
# This *should* work for all terminals. I know it works on ksh
_ps1() {
exitcode="$?"
r="\e[0m"
fg_red="\e[31m"
fg_green="\e[32m"
fg_yellow="\e[33m"
fg_grey="\e[37m"
fg_bold="\e[1m"
# Add hostname prefix in SSH sessions
if [ "$SSH_CLIENT" ]; then
prefix="${fg_bold}${fg_red}$(hostname)${r}${fg_red}:"
fi
# Append a "restricted" prefix in rbash
if [ "$0" = "rbash" ]; then
prefix="${fg_bold}${fg_grey}rbash${r}:"
fi
# Change PWD color depending on the shell
case $0 in
*bash)
prefix="${prefix}${fg_yellow}"
;;
/bin/ksh)
prefix="${prefix}${fg_green}"
;;
/bin/sh)
prefix="${prefix}${fg_grey}"
;;
*)
;;
esac
# Show the tilde instead of $HOME
if [ "${PWD#$HOME}" != "$PWD" ]; then
cpwd="~${PWD#$HOME}"
else
cpwd="$PWD"
fi
# Alert us if the last command failed
fail=""
if ! [ "$exitcode" = "0" ]; then
fail="${fg_bold}${fg_red}?"
fi
# shellcheck disable=2059
printf "[${prefix}${cpwd}${r}]${fail}${r}${fg_green}\$${r} "
}
# And export our PS1
case "$_baseshell" in
zsh)
# Don't do this on ZSH
# I have a custom theme for that
;;
sh)
export PS1='\$ '
;;
*)
export PS1='$(_ps1)'
;;
esac
# Clean up
unset _baseshell