[zsh] Update shell init; use ZSH autoloaded functions instead of a bunch of files

This commit is contained in:
Eryn Wells 2021-12-31 10:27:27 -08:00
parent 2b584f97cf
commit b09d523218
13 changed files with 208 additions and 138 deletions

View file

@ -12,11 +12,19 @@ import os.path
_LOGFILE = os.path.expanduser('~/.shell.log')
_LOGGER = None
LOG_LEVELS = {
'crit': logging.CRITICAL,
'error': logging.ERROR,
'warn': logging.WARNING,
'info': logging.INFO,
'debug': logging.DEBUG
}
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('-b', '--bold', action='store_true')
parser.add_argument('-f', '--file', default=LOGFILE)
parser.add_argument('-l', '--level', type=int, default=logging.INFO)
parser.add_argument('-f', '--file', default=_LOGFILE)
parser.add_argument('-l', '--level', default='info', choices=list(LOG_LEVELS.keys()))
parser.add_argument('-n', '--noeol', action='store_true')
parser.add_argument('msg')
parser.add_argument('args', nargs=argparse.REMAINDER)
@ -31,19 +39,23 @@ def logger(args):
stdout_handler.setFormatter(formatter)
logger.addHandler(stdout_handler)
# TODO: Add file handler
return logger
def main():
# Set up.
args = parse_args()
log = logger(args)
# Set the level from the shell.
level = int(os.environ.get('NOISY', logging.INFO))
level = os.environ.get('SHELL_LOG_LEVEL', logging.INFO)
log.setLevel(level)
# Log it.
log.log(args.level, args.msg, *args.args)
try:
message_log_level = LOG_LEVELS[args.level]
except KeyError:
message_log_level = logging.INFO
log.log(message_log_level, args.msg, *args.args)
if __name__ == '__main__':
main()

87
env
View file

@ -1,87 +0,0 @@
# .env
# vim: ft=zsh:
#
# Environment settings for bash and derivatives. The env scripts are sourced by
# Zsh for every shell, even the non-interactive ones, so this needs to be small
# and quick. Any configuration that will only be used for interactive sessions
# should be in the rc scripts.
#
# Eryn Wells <eryn@erynwells.me>
[[ -e "$HOME/.shell-functions" ]] && source "$HOME/.shell-functions"
export SYS=`uname -s | tr A-Z a-z`
# Set this to a non-zero integer to see startup messages
export NOISY=0
print_heading -l 1 'Initializing environment'
export PAGER="less"
export MANPAGER=$PAGER
export EDITOR="vim"
export VISUAL=$EDITOR
export LESSHISTFILE="-"
export GREP_OPTIONS="--color=auto"
export GREP_COLOR="1;32"
function prepend-to-path
{
if [[ -d "$1" ]]; then
path=("$1" $path)
export path
fi
}
function append-to-path
{
if [[ -d "$1" ]]; then
path+="$1"
export path
fi
}
function setup-path
{
export path=()
append-to-path "/usr/local/bin"
append-to-path "/usr/bin"
append-to-path "/bin"
append-to-path "/usr/local/sbin"
append-to-path "/usr/sbin"
append-to-path "/sbin"
prepend-to-path "/usr/X11/bin"
prepend-to-path "/opt/local/bin"
prepend-to-path "$HOME/.local/bin"
prepend-to-path "$HOME/.gem/ruby/2.2.0/bin"
prepend-to-path "$HOME/.cargo/bin"
prepend-to-path "$HOME/bin"
}
setup-path
if which virtualenvwrapper.sh 1>/dev/null 2>&1; then
export WORKON_HOME="$HOME/src/py/.envs"
source `which virtualenvwrapper.sh`
fi
# Make sure gpg2 knows what to do with the curses-based smartcard PIN prompt.
export GPG_TTY=`tty`
# System-specific settings
if [[ -e "$HOME/.env.$SYS" ]]; then
print_info -l 2 "Sourcing system-specific environment settings for $SYS"
source "$HOME/.env.$SYS"
fi
host_env="$HOME/.env.`hostname -s`"
if [[ -e "$host_env" ]]; then
print_info -l 2 "Sourcing host-specific environment settings: $host_env"
source "$host_env"
fi
# Local environment settings
if [[ -e "$HOME/.env.local" ]]; then
print_info -l 2 "Sourcing local environment setup"
source "$HOME/.env.local"
fi

View file

@ -1,26 +0,0 @@
# vim: set ft=zsh:
# Eryn Wells <eryn@erynwells.me>
export OSBUILD=`sysctl -n kern.osversion`
export OSVERSION=`sysctl -n kern.osproductversion`
export HWMODEL=`sysctl -n hw.model`
pythonroot="$HOME/Library/Python"
if [[ -d "$pythonroot" ]]; then
for f in `ls "$pythonroot"`; do
prepend-to-path "$pythonroot/$f/bin"
done
fi
prepend-to-path "/usr/local/go/bin"
py27local="$pythonroot/2.7/lib/python/site-packages"
if [[ -d "$py27local" ]]; then
if [[ ! -z $PYTHONPATH ]]; then
PYTHONPATH=$py27local:$PYTHONPATH
else
PYTHONPATH=$py27local
fi
fi
unset py27local
unset pythonroot
export PYTHONPATH

View file

@ -1,5 +0,0 @@
# vim: set ft=zsh:
# Linux specific environment settings
# Eryn Wells <eryn@erynwells.me>
export MAIL="/var/mail/$USER"

View file

@ -1,5 +0,0 @@
# .env.local
# vim: set ft=zsh:
# Local environment customizations
# Eryn Wells <eryn@erynwells.me>

13
zsh/func/append_to_path Normal file
View file

@ -0,0 +1,13 @@
#!/usr/bin/env zsh
# vim:ft=zsh:
# Eryn Wells <eryn@erynwells.me>
function append_to_path
{
if [[ -d "$1" ]]; then
path+="$1"
export path
fi
}
append_to_path "$@"

21
zsh/func/init_env Normal file
View file

@ -0,0 +1,21 @@
#!/usr/bin/env zsh
# vim:ft=zsh:
# Eryn Wells <eryn@erynwells.me>
function init_env
{
shell-log 'Initializing basic environment'
export PAGER="less"
export MANPAGER=$PAGER
export EDITOR="vim"
export VISUAL=$EDITOR
export LESSHISTFILE="-"
export GREP_OPTIONS="--color=auto"
export GREP_COLOR="1;32"
# Make sure gpg2 knows what to do with the curses-based smartcard PIN prompt.
export GPG_TTY=`tty`
}
init_env "$@"

38
zsh/func/init_env_darwin Normal file
View file

@ -0,0 +1,38 @@
#!/usr/bin/env zsh
# vim:ft=zsh:
# Eryn Wells <eryn@erynwells.me>
autoload append_to_path
autoload prepend_to_path
function init_env_darwin
{
shell-log "Initializing Darwin environment"
export OSBUILD=`sysctl -n kern.osversion`
export OSVERSION=`sysctl -n kern.osproductversion`
export HWMODEL=`sysctl -n hw.model`
local pythonRoot
local python27SitePackages
pythonRoot="$HOME/Library/Python"
if [[ -d "$pythonRoot" ]]; then
for f in `ls "$pythonRoot"`; do
prepend_to_path "$pythonRoot/$f/bin"
done
fi
python27SitePackages="$pythonroot/2.7/lib/python/site-packages"
if [[ -d "$python27SitePackages" ]]; then
if [[ ! -z $PYTHONPATH ]]; then
PYTHONPATH=$python27SitePackages:$PYTHONPATH
else
PYTHONPATH=$python27SitePackages
fi
fi
export PYTHONPATH
}
init_env_darwin "$@"

View file

@ -0,0 +1,29 @@
#!/usr/bin/env zsh
# vim:ft=zsh:
# Eryn Wells <eryn@erynwells.me>
function init_env_darwin_python
{
local pythonRoot
local python27SitePackages
pythonRoot="$HOME/Library/Python"
if [[ -d "$pythonRoot" ]]; then
for f in `ls "$pythonRoot"`; do
prepend_to_path "$pythonRoot/$f/bin"
done
fi
python27SitePackages="$pythonroot/2.7/lib/python/site-packages"
if [[ -d "$python27SitePackages" ]]; then
if [[ ! -z $PYTHONPATH ]]; then
PYTHONPATH=$python27SitePackages:$PYTHONPATH
else
PYTHONPATH=$python27SitePackages
fi
fi
export PYTHONPATH
}
init_env_darwin_python "$@"

34
zsh/func/init_env_python Normal file
View file

@ -0,0 +1,34 @@
#!/usr/bin/env zsh
# vim:ft=zsh:
# Eryn Wells <eryn@erynwells.me>
function init_env_python
{
local pythonRoot
local python27SitePackages
pythonRoot="$HOME/Library/Python"
if [[ -d "$pythonRoot" ]]; then
for f in `ls "$pythonRoot"`; do
prepend_to_path "$pythonRoot/$f/bin"
done
fi
python27SitePackages="$pythonroot/2.7/lib/python/site-packages"
if [[ -d "$python27SitePackages" ]]; then
if [[ ! -z $PYTHONPATH ]]; then
PYTHONPATH=$python27SitePackages:$PYTHONPATH
else
PYTHONPATH=$python27SitePackages
fi
fi
export PYTHONPATH
if which virtualenvwrapper.sh 1>/dev/null 2>&1; then
export WORKON_HOME="$HOME/src/py/.envs"
source `which virtualenvwrapper.sh`
fi
}
init_env_python "$@"

25
zsh/func/init_path Normal file
View file

@ -0,0 +1,25 @@
#!/usr/bin/env zsh
# Initialize the path to a standard default
# Eryn Wells <eryn@erynwells.me>
autoload prepend_to_path
autoload append_to_path
function init_path
{
export path=()
append_to_path "/usr/local/bin"
append_to_path "/usr/bin"
append_to_path "/bin"
append_to_path "/usr/local/sbin"
append_to_path "/usr/sbin"
append_to_path "/sbin"
prepend_to_path "/usr/X11/bin"
prepend_to_path "/opt/local/bin"
prepend_to_path "$HOME/.local/bin"
prepend_to_path "$HOME/.gem/ruby/2.2.0/bin"
prepend_to_path "$HOME/.cargo/bin"
prepend_to_path "$HOME/bin"
}
init_path

13
zsh/func/prepend_to_path Normal file
View file

@ -0,0 +1,13 @@
#!/usr/bin/env zsh
# vim:ft=zsh:
# Eryn Wells <eryn@erynwells.me>
function prepend_to_path
{
if [[ -d "$1" ]]; then
path=("$1" $path)
export path
fi
}
prepend_to_path "$@"

24
zshenv
View file

@ -1,16 +1,24 @@
# .zshenv
# vim: ft=zsh
#
# Environment settings for zsh
#
# Eryn Wells <eryn@erynwells.me>
# Don't read global startup. It messes things up...
unsetopt GLOBAL_RCS
[ -e $HOME/.env ] && source $HOME/.env
export SYS=`uname -s | tr A-Z a-z`
# System specific environment settings
[ -e $HOME/.zshenv.$SYS ] && source $HOME/.zshenv.$SYS
# Local environment settings
[ -e $HOME/.zshenv.local ] && source $HOME/.zshenv.local
fpath=("$HOME/.zsh/func" $fpath)
autoload +X prepend_to_path
autoload +X append_to_path
autoload +X init_env
autoload +X init_env_python
autoload +X init_path
init_path
init_env
init_env_python
autoload +X init_env_$SYS
if [[ $? ]]; then
init_env_$SYS
fi