Skip to content

Commit

Permalink
fancy login display logic, à la the AMSTRAD CPC, with colours
Browse files Browse the repository at this point in the history
  • Loading branch information
sinewalker committed Jun 14, 2019
1 parent 9f39c70 commit 2a30401
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 20 deletions.
104 changes: 84 additions & 20 deletions link/.bashrc
Original file line number Diff line number Diff line change
@@ -1,29 +1,70 @@
# Where the magic happens.
# Bash Run Commands
# Run by login or interactive shells, and also by Bash when called as /bin/sh
# in some situations. So this needs to be POSIX syntax or guard Bashisms

# There's a lot of nonsense display logic. All of this is opinionated, so
# it's guarded with tests for flag files - by default you'll see nothing when
# bash loads. See ${BASH_MODULES}/20_env.sh for bashrc_ aliases to turn it on.

# Where the magic happens
export DOTFILES=~/.dotfiles
export BASH_MODULES=${DOTFILES}/source

PATH=${DOTFILES}/bin:${PATH}
export PATH

function __load_modules() {
local MODULE
if test -t 1 && test -f ~/etc/.bashrc_loading; then # is a TTY and show?
local HIGH='\033[1;32m' MED='\033[0;36m' LOW='\033[0;37m'
echo -e " ${HIGH}Loading bash modules...${LOW}"
for MODULE in ${BASH_MODULES}/*.sh; do
if test -f ~/etc/.bashrc_debug; then
echo "${MODULE}"
source "${MODULE}"
else
printf "\r%${COLUNMS}s"
printf "\r${MED}<-- $(basename -s .sh ${MODULE})${LOW}%$((${COLUMNS}-${#MODULE}+${#BASH_MODULES}))s"
source "${MODULE}"
if test -f ~/etc/.bashrc_slow ; then
sleep 0.25
fi
fi
done
else # no TTY/show, just load
for MODULE in ${BASH_MODULES}/*.sh; do
source "${MODULE}"
done
fi
}
function src() {
local FUNCDESC='Source all files in ${DOTFILES}/source/, or a specified file'
local FILE
local FUNCDESC="Source all modules in ${BASH_MODULES}, or a specified module.
If there is a file ~/etc/.bashrc_loading then show the modules as they load,
with a summary at the end of how many modules there are, and how long they
took to load.
If there is a file ~/etc/.bashrc_debug then the module names will be listed
while sourced, instead of overwritten.
If no attached TTY, or no loading file, then just source the modules."
if [[ "${1}" ]]; then
source "${DOTFILES}/source/${1}.sh"
else
echo "Loading environment..."
for FILE in ${DOTFILES}/source/*.sh; do
printf "\r%${COLUNMS}s"
printf "\r<-- ${FILE}"
source "${FILE}"
done
printf "\rReady%$((${COLUMNS}-5))s"
source "${BASH_MODULES}/${1}.sh"
elif test -t 1 && test -f ~/etc/.bashrc_loading; then # is a TTY and show?
local TIMER=$(mktemp -t loadtime)
local RED='\033[0;31m' GREEN='\033[0;32m' LOW='\033[0;37m'
local PR_COLOUR=${RED}
{ time __load_modules; } 2> ${TIMER} && PR_COLOUR=${GREEN}
local NOMODS=(${BASH_MODULES}/*.sh); NOMODS=${#NOMODS[@]}
local SUMMARY=" (${NOMODS} modules, $(awk '/real/{print $2}' ${TIMER}))"
rm ${TIMER}
printf "\r${PR_COLOUR}${SUMMARY}${LOW}%$((${COLUMNS}-${#SUMMARY}))s";
else # no TTY, just load
__load_modules
fi
}

function lssrc(){
local FUNCDESC='List the Dotfiles source modules available for the src function.
The .sh suffix is stripped.'
\ls ${DOTFILES}/source|awk '/.sh$/ { gsub(/\.sh/, ""); print }'
local FUNCDESC="List the Dotfiles bash modules available for the src function.
The .sh suffix is stripped."
\ls ${BASH_MODULES}|awk '/.sh$/ { gsub(/\.sh/, ""); print }'
}

# Completion for src function (requires bash_completion)
Expand All @@ -37,12 +78,35 @@ _src() {
complete -F _src src

function dotfiles() {
local FUNCDESC='Run dotfiles refresh script, then reload.
This causes the Copy, Link and Init step to be run.'
local FUNCDESC="Run dotfiles refresh script, then reload.
This causes the Copy, Link and Init step to be run."
${DOTFILES}/bin/dotfiles "${@}" && src
}

#Load the rest of Dotfiles bash modules, unless in POSIX mode
if kill -l|grep SIG > /dev/null; then
src
# In interactive shells, unless in POSIX mode, load the rest of the
# Dotfiles bash modules into the environment. (POSIX will fail, non-interactive
# shells don't need the Dotfiles bash modules)
#
# For a login shell, also print a bash banner, à la the AMSTRAD CPC
# if enabled by presence of ~/etc/.bashrc_banner ;-)
#
# If there's a ~/etc/.bashrc_lols file then use lolcat for the banner.
if kill -l|grep SIG &> /dev/null; then #is not POSIX?
if ( shopt -q login_shell && test -f ~/etc/.bashrc_banner ); then
echo " $(hostname -s) $(uname -srm)"
echo ' '
BANNER=$(bash --version|head -n2\
|sed 's/Copyright (C) /©/'|awk '{print " " $0}')
if ( type -p lolcat &> /dev/null && test -f ~/etc/.bashrc_lols ); then
echo "${BANNER}"|lolcat
else
echo "${BANNER}"
fi
unset BANNER
echo ' '
src
printf '\n\n\033[0;33mReady\033[0;37m\n'
elif [[ $- == *i* ]] ; then # is interactive?
src
fi
fi
14 changes: 14 additions & 0 deletions source/20_env.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#Opinionated or personality environment settings

# Case-insensitive globbing (used in pathname expansion)
shopt -s nocaseglob

Expand Down Expand Up @@ -51,3 +53,15 @@ export TEMP=${TMP}
export TMPDIR=${TMP}
#MJL20170226 in case ~/tmp is missing...
[[ -d ${TMP} ]] || mkdir ${TMP}

#MJL20190614 turn on my bashrc banner loading bells and whistles (see link/.bashrc)
alias bashrc_pretty="touch ~/etc/.bashrc_banner ~/etc/.bashrc_loading ~/etc/.bashrc_lols"
alias bashrc_boring="rm -f ~/etc/.bashrc_banner ~/etc/.bashrc_loading ~/etc/.bashrc_lols ~/etc/.bashrc_slow"
alias bashrc_lols=bashrc_pretty
alias bashrc_banner="touch ~/etc/.bashrc_banner ~/etc/.bashrc_loading"
alias cpcify="bashrc_boring; bashrc_banner"
alias uncpcify=bashrc_boring
alias bashrc_debug="touch ~/etc/.bashrc_debug"
alias bashrc_undebug="rm -f ~/etc/.bashrc_debug"
alias bashrc_slow="touch ~/etc/.bashrc_slow"
alias bashrc_fast="rm -f ~/etc/.bashrc_slow"

1 comment on commit 2a30401

@sinewalker
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue #50

Please sign in to comment.