From d1090feefe51f8e783fd9cfe55d942cde328d80e Mon Sep 17 00:00:00 2001 From: Buddy Sandidge Date: Sun, 13 Dec 2020 15:17:38 -0800 Subject: [PATCH] Allow .profile to load multiple config files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit • set environment variables in .profile load • move bash settings to be in profile if they are for POSIX (sh/zsh/etc) • use .d files in bash customizations --- base/.config/profile/env.d/base.env | 7 +++ base/.config/profile/profile.d/base.sh | 24 +++++++++ base/.profile | 43 ++++++++++++--- bash/.bashrc | 52 ++----------------- bash/.config/bash/{ => bashrc.d}/aliases.sh | 1 + bash/.config/bash/bashrc.d/bash.sh | 18 +++++++ .../.config/bash/{ => bashrc.d}/completion.sh | 12 +++-- bash/.config/bash/{ => bashrc.d}/functions.sh | 17 +++--- bash/.config/bash/bashrc.d/opts.sh | 11 ++++ bash/.config/bash/env.d/bash.env | 3 ++ bash/.config/bash/environment.sh | 13 ----- bash/.config/bash/path.sh | 38 -------------- deno/.config/profile/env.d/deno.env | 1 + go/.config/profile/profile.d/go.sh | 6 +++ .../.config/bash/env.d/hellotech-config.env | 4 +- hellotech/.config/profile/env.d/hellotech.env | 3 ++ ripgrep/.config/profile/env.d/ripgrep.env | 2 + rust/.config/profile/env.d/rust.env | 1 + 18 files changed, 134 insertions(+), 122 deletions(-) create mode 100644 base/.config/profile/env.d/base.env create mode 100644 base/.config/profile/profile.d/base.sh rename bash/.config/bash/{ => bashrc.d}/aliases.sh (99%) create mode 100644 bash/.config/bash/bashrc.d/bash.sh rename bash/.config/bash/{ => bashrc.d}/completion.sh (71%) rename bash/.config/bash/{ => bashrc.d}/functions.sh (62%) create mode 100644 bash/.config/bash/bashrc.d/opts.sh create mode 100644 bash/.config/bash/env.d/bash.env delete mode 100644 bash/.config/bash/environment.sh delete mode 100644 bash/.config/bash/path.sh create mode 100644 deno/.config/profile/env.d/deno.env create mode 100644 go/.config/profile/profile.d/go.sh create mode 100644 hellotech/.config/profile/env.d/hellotech.env create mode 100644 ripgrep/.config/profile/env.d/ripgrep.env create mode 100644 rust/.config/profile/env.d/rust.env diff --git a/base/.config/profile/env.d/base.env b/base/.config/profile/env.d/base.env new file mode 100644 index 0000000..dfce454 --- /dev/null +++ b/base/.config/profile/env.d/base.env @@ -0,0 +1,7 @@ +#shellcheck disable=SC2034 +EDITOR=vim +BROWSER=firefox +PATH="${XDG_BIN_HOME}:${PATH}" +HISTCONTROL=ignoredups:erasedups:ignorespace +HISTFILESIZE= +HISTSIZE= diff --git a/base/.config/profile/profile.d/base.sh b/base/.config/profile/profile.d/base.sh new file mode 100644 index 0000000..5ed57a8 --- /dev/null +++ b/base/.config/profile/profile.d/base.sh @@ -0,0 +1,24 @@ +#ft=sh + +add_date_prefix () { + DIR=$(dirname "$1") + FILE=$(basename "$1") + DATE=$(date -r "$1" +"%F") + if [ ! -f "$1" ] ; then + echo "unknown file: $1" + return + fi + mv "$1" "$DIR/${DATE}_${FILE}" +} + +get_bitrate () { + if [ ! -f "$1" ] ; then + echo "[ERROR] unknown file: $1" + return + fi + if ! command -v exiftool > /dev/null ; then + echo "[ERROR] exiftool not installed" + return + fi + exiftool -AudioBitrate "$1" | awk '{print $4}' +} diff --git a/base/.profile b/base/.profile index 3f4b792..87626b4 100644 --- a/base/.profile +++ b/base/.profile @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#ft=sh export LANGUAGE="en_US:en" export LC_MESSAGES="en_US.UTF-8" @@ -10,13 +10,42 @@ export XDG_CACHE_HOME=${XDG_CACHE_HOME:="$HOME/.cache"} export XDG_CONFIG_HOME=${XDG_CONFIG_HOME:="$HOME/.config"} export XDG_DATA_HOME=${XDG_DATA_HOME:="$HOME/.local/share"} -[[ -f "${XDG_CONFIG_HOME}/user-dirs.dirs" ]] && eval "$( - awk '/^XDG_/ {print "export " $1}' "${XDG_CONFIG_HOME}/user-dirs.dirs" -)" +env_file() { + file="${1}" + if [ ! -f "${file}" ]; then + return + fi + eval "$( + grep -v '^\s*\#' "${file}" | + grep -v '^\s*$' | + sed 's/^\s*export//g' | + sed 's/^/export /g' + )" +} + +env_file "${XDG_CONFIG_HOME}/user-dirs.dirs" + +if [ -d "${XDG_CONFIG_HOME}/profile/env.d" ]; then + for file in "${XDG_CONFIG_HOME}"/profile/env.d/*.env; do + env_file "${file}" + done +fi + +unset env_file + +if [ -d "${XDG_CONFIG_HOME}/profile/profile.d" ]; then + for file in "${XDG_CONFIG_HOME}"/profile/profile.d/*.sh; do + if [ -r "$file" ]; then + #shellcheck disable=1090 + . "$file" + fi + done +fi + -if [[ ${SHELL} = /bin/bash ]] || [[ ${SHELL} = /usr/local/bin/bash ]]; then +if [ "${SHELL}" = /bin/bash ] || [ "${SHELL}" = /usr/local/bin/bash ]; then # shellcheck disable=SC1090 - if [[ -f "${HOME}/.bashrc" ]]; then - source "${HOME}/.bashrc" + if [ -f "${HOME}/.bashrc" ]; then + . "${HOME}/.bashrc" fi fi diff --git a/bash/.bashrc b/bash/.bashrc index 2052da8..9e0dc01 100644 --- a/bash/.bashrc +++ b/bash/.bashrc @@ -1,56 +1,10 @@ # If not running interactively, don't do anything [ -z "$PS1" ] && return -function source-file { - local file - file="$1" - if [[ -f "$file" ]]; then - # shellcheck disable=SC1090 - source "$file" - fi -} -if [[ ! "${BASH_VERSION}" = "3.2*" ]]; then - shopt -s autocd -fi - -CONFIG="${XDG_CONFIG_HOME:-$HOME/.config}/bash" - -source-file /etc/bash_completion -source-file "${CONFIG}/environment.sh" -source-file "${CONFIG}/aliases.sh" -source-file "${CONFIG}/functions.sh" -source-file "${CONFIG}/path.sh" -source-file "${CONFIG}/prompt.sh" -source-file "${CONFIG}/completion.sh" -source-file ~/opt/bash/env - -unset -f source-file - -# append to the history file, don't overwrite it -shopt -s histappend - -# check the window size after each command and, if necessary, -# update the values of LINES and COLUMNS. -shopt -s checkwinsize - -# Disable capslock -if [[ -x $(which setxkbmap) ]]; then - if [[ $(uname) == Linux ]]; then - setxkbmap -option ctrl:nocaps - fi -fi - -# make less more friendly for non-text input files, see lesspipe(1) -[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)" - -# enable color support of ls and also add handy aliases -if [ -x /usr/bin/dircolors ]; then - test -r ~/.dircolors && (eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)") -fi - -if [ "$TERM" == xterm ]; then - export TERM=xterm-256color +if [[ -f "${XDG_CONFIG_HOME}/bash/prompt.sh" ]]; then + # shellcheck disable=SC1090 + source "${XDG_CONFIG_HOME}/bash/prompt.sh" fi if [ -d "${XDG_CONFIG_HOME}"/bash/bashrc.d ]; then diff --git a/bash/.config/bash/aliases.sh b/bash/.config/bash/bashrc.d/aliases.sh similarity index 99% rename from bash/.config/bash/aliases.sh rename to bash/.config/bash/bashrc.d/aliases.sh index 29dad0e..7b09463 100644 --- a/bash/.config/bash/aliases.sh +++ b/bash/.config/bash/bashrc.d/aliases.sh @@ -1,3 +1,4 @@ +#ft=bash alias ll='ls -alF' alias curl='curl --proto-default https --silent ' diff --git a/bash/.config/bash/bashrc.d/bash.sh b/bash/.config/bash/bashrc.d/bash.sh new file mode 100644 index 0000000..4b0b4f9 --- /dev/null +++ b/bash/.config/bash/bashrc.d/bash.sh @@ -0,0 +1,18 @@ +#ft=bash + +# Disable capslock +if [[ -x $(which setxkbmap) ]] && [[ $(uname) == Linux ]]; then + setxkbmap -option ctrl:nocaps +fi + +# make less more friendly for non-text input files, see lesspipe(1) +if [ -x /usr/bin/lesspipe ]; then + eval "$(SHELL=/bin/sh lesspipe)" +fi + +# enable color support of ls and also add handy aliases +if [ -x /usr/bin/dircolors ]; then + if test -r ~/.dircolors ; then + eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)" + fi +fi diff --git a/bash/.config/bash/completion.sh b/bash/.config/bash/bashrc.d/completion.sh similarity index 71% rename from bash/.config/bash/completion.sh rename to bash/.config/bash/bashrc.d/completion.sh index 87b34a8..84b1cd3 100644 --- a/bash/.config/bash/completion.sh +++ b/bash/.config/bash/bashrc.d/completion.sh @@ -1,5 +1,14 @@ #!/usr/bin/env bash +if [ -f /etc/bash_completion ]; then + source /etc/bash_completion +fi + +# shellcheck disable=SC1091 +if [ -f /usr/local/etc/bash_completion ]; then + source /usr/local/etc/bash_completion +fi + COMPLETION_DIR=/usr/local/etc/bash_completion.d/ if [ -d "$COMPLETION_DIR" ]; then @@ -15,7 +24,4 @@ if [ -d "$COMPLETION_DIR" ]; then unset i fi -# shellcheck disable=SC1091 -[[ -f /usr/local/etc/bash_completion ]] && source /usr/local/etc/bash_completion - unset COMPLETION_DIR diff --git a/bash/.config/bash/functions.sh b/bash/.config/bash/bashrc.d/functions.sh similarity index 62% rename from bash/.config/bash/functions.sh rename to bash/.config/bash/bashrc.d/functions.sh index 4e42fc5..7e71774 100644 --- a/bash/.config/bash/functions.sh +++ b/bash/.config/bash/bashrc.d/functions.sh @@ -1,6 +1,9 @@ -#!/usr/bin/env bash +#ft=bash function min-jpg { + if ! command -v jpegtran &> /dev/null ; then + echo "jpegtran not installed" + fi tmpfile="$(mktemp)" ogfile="$1" jpegtran -optimize -perfect -outfile "$tmpfile" "$ogfile" @@ -8,6 +11,9 @@ function min-jpg { } function min-png { + if ! command -v pngcrush &> /dev/null ; then + echo "pngcrush not installed" + fi tmpfile="$(mktemp)" ogfile="$1" pngcrush -rem alla -reduce -brute "$ogfile" "$tmpfile" @@ -19,12 +25,5 @@ function get-create-date { } function add-date-prefix { - DIR=$(dirname "$1") - FILE=$(basename "$1") - DATE=$(date -r "$1" +"%F") - mv "$1" "$DIR/${DATE}_${FILE}" -} - -function get-bitrate { - exiftool -AudioBitrate "$1" | awk '{print $4}' + add_date_prefix "$1" "$2" } diff --git a/bash/.config/bash/bashrc.d/opts.sh b/bash/.config/bash/bashrc.d/opts.sh new file mode 100644 index 0000000..4aef955 --- /dev/null +++ b/bash/.config/bash/bashrc.d/opts.sh @@ -0,0 +1,11 @@ +#ft=bash +if [[ ! "${BASH_VERSION}" = "3.2*" ]]; then + shopt -s autocd +fi + +# append to the history file, don't overwrite it +shopt -s histappend + +# check the window size after each command and, if necessary, +# update the values of LINES and COLUMNS. +shopt -s checkwinsize diff --git a/bash/.config/bash/env.d/bash.env b/bash/.config/bash/env.d/bash.env new file mode 100644 index 0000000..67032b1 --- /dev/null +++ b/bash/.config/bash/env.d/bash.env @@ -0,0 +1,3 @@ +#shellcheck disable=SC2034 +HISTTIMEFORMAT='%y-%m-%d %T λ ' +QUOTING_STYLE=literal diff --git a/bash/.config/bash/environment.sh b/bash/.config/bash/environment.sh deleted file mode 100644 index f100c13..0000000 --- a/bash/.config/bash/environment.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env bash - -export EDITOR=vim -export BROWSER=firefox - -export HISTCONTROL=ignoredups:erasedups:ignorespace -export HISTFILESIZE= -export HISTSIZE= -export HISTTIMEFORMAT='%y-%m-%d %T λ ' - -# QUOTING_STYLE so ls does not -export QUOTING_STYLE=literal -export RIPGREP_CONFIG_PATH="${XDG_CONFIG_HOME}/ripgrep/config" diff --git a/bash/.config/bash/path.sh b/bash/.config/bash/path.sh deleted file mode 100644 index bd99091..0000000 --- a/bash/.config/bash/path.sh +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/env bash - -function add-path { - dir="$1" - if [[ $PATH =~ $dir ]]; then - return - fi - if [[ -d $dir ]]; then - PATH=$dir:$PATH - fi -} - -function prefix-path { - dir="$1" - if [[ $PATH =~ $dir ]]; then - return - fi - if [[ -d $dir ]]; then - PATH=$PATH:$dir - fi -} - -add-path /usr/local/go/bin -add-path ~/.bin -add-path ~/.cargo/bin -add-path ~/.deno/bin -prefix-path ~/.local/bin -add-path ~/bin -add-path ~/go/bin -add-path ~/opt/bin - -# work -add-path ~/.rbenv/shims -add-path /usr/local/opt/mysql-client/bin -add-path ~/google-cloud-sdk/bin/ - -unset -f add-path -unset -f prefix-path diff --git a/deno/.config/profile/env.d/deno.env b/deno/.config/profile/env.d/deno.env new file mode 100644 index 0000000..359e1d3 --- /dev/null +++ b/deno/.config/profile/env.d/deno.env @@ -0,0 +1 @@ +PATH="${PATH}:${HOME}/.deno/bin" diff --git a/go/.config/profile/profile.d/go.sh b/go/.config/profile/profile.d/go.sh new file mode 100644 index 0000000..4a9dcf4 --- /dev/null +++ b/go/.config/profile/profile.d/go.sh @@ -0,0 +1,6 @@ +if [ -d /usr/local/go/bin ] ; then + PATH="${PATH}:/usr/local/go/bin" +fi +if [ -d "${HOME}/go/bin" ] ; then + PATH="${PATH}:${HOME}/go/bin" +fi diff --git a/hellotech/.config/bash/env.d/hellotech-config.env b/hellotech/.config/bash/env.d/hellotech-config.env index 2d1c679..345ba64 100644 --- a/hellotech/.config/bash/env.d/hellotech-config.env +++ b/hellotech/.config/bash/env.d/hellotech-config.env @@ -1,5 +1,3 @@ -#!/usr/bin/env bash # shellcheck disable=SC2034 BASH_SILENCE_DEPRECATION_WARNING=1 -GOPRIVATE=github.com/HelloTech -GO_TEST=gotest +PATH="${PATH}:${HOME}/.rbenv/shims:/usr/local/opt/mysql-client/bin:${HOME}/google-cloud-sdk/bin" diff --git a/hellotech/.config/profile/env.d/hellotech.env b/hellotech/.config/profile/env.d/hellotech.env new file mode 100644 index 0000000..c914eb7 --- /dev/null +++ b/hellotech/.config/profile/env.d/hellotech.env @@ -0,0 +1,3 @@ +# shellcheck disable=SC2034 +GOPRIVATE=github.com/HelloTech +GO_TEST=gotest diff --git a/ripgrep/.config/profile/env.d/ripgrep.env b/ripgrep/.config/profile/env.d/ripgrep.env new file mode 100644 index 0000000..a40db09 --- /dev/null +++ b/ripgrep/.config/profile/env.d/ripgrep.env @@ -0,0 +1,2 @@ +#shellcheck disable=SC2034 +RIPGREP_CONFIG_PATH="${XDG_CONFIG_HOME}/ripgrep/config" diff --git a/rust/.config/profile/env.d/rust.env b/rust/.config/profile/env.d/rust.env new file mode 100644 index 0000000..2d50150 --- /dev/null +++ b/rust/.config/profile/env.d/rust.env @@ -0,0 +1 @@ +PATH="${PATH}:${HOME}/.cargo/bin"