Skip to content

Commit

Permalink
Adding preexec as a vendored library
Browse files Browse the repository at this point in the history
Added a vendored lib loading routine in bash-it.sh
Added documentation on how to vendor libs in bash-it
Added and fixed plugins using preexec
Added tests for two plugins
Removed the old preexec lib
  • Loading branch information
buhl committed Jan 23, 2021
1 parent 5ad4979 commit 59afe9f
Show file tree
Hide file tree
Showing 12 changed files with 252 additions and 260 deletions.
13 changes: 12 additions & 1 deletion bash_it.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ do
fi
done

# Load vendors
BASH_IT_LOG_PREFIX="vendor: "
for _bash_it_vendor_init in "${BASH_IT}"/vendor/init.d/*.bash
do
_log_debug "Loading \"$(basename "${_bash_it_vendor_init}" .bash)\"..."
# shellcheck disable=SC1090
source "${_bash_it_vendor_init}"
done
unset _bash_it_vendor_init

BASH_IT_LOG_PREFIX="core: main: "
# Load the global "enabled" directory
# "family" param is empty so that files get sources in glob order
# shellcheck source=./scripts/reloader.bash
Expand All @@ -62,7 +73,7 @@ do
done

# Load theme, if a theme was set
if [[ ! -z "${BASH_IT_THEME}" ]]; then
if [[ -n "${BASH_IT_THEME}" ]]; then
_log_debug "Loading \"${BASH_IT_THEME}\" theme..."
# Load colors and helpers first so they can be used in base theme
BASH_IT_LOG_PREFIX="themes: colors: "
Expand Down
3 changes: 3 additions & 0 deletions clean_files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@ themes/barbuk
themes/atomic
themes/axin
themes/base.theme.bash
themes/command_duration.theme.bash

# plugins
#
plugins/available/basher.plugin.bash
plugins/available/cmd-returned-notify.plugin.bash
plugins/available/xterm.plugin.bash

# completions
#
Expand Down
60 changes: 60 additions & 0 deletions docs/development.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ The main ``bash_it.sh`` script loads the frameworks individual components in the


* ``lib/composure.bash``
* ``vendor/init.d/*.bash``
* Files in ``lib`` with the exception of ``appearance.bash`` - this means that ``composure.bash`` is loaded again here (possible improvement?)
* Enabled ``aliases``
* Enabled ``plugins``
Expand Down Expand Up @@ -78,6 +79,65 @@ Having the order based on a numeric priority in a common directory allows for mo

These items are subject to change. When making changes to the internal functionality, this page needs to be updated as well.

Working with vendored libs
--------------------------

Vendored libs are external libraries, meaning source code not maintained by Bash-it
developers.
They are ``git subtrees`` curated in the ``vendor/`` folder. To ease the work with git
vendored libs as subtrees we use the `git-vendor <https://github.com/Tyrben/git-vendor>`_ tool.
The `original repo<https://github.com/brettlangdon/git-vendor>`_ for git vendor is
unmaintained so for now we are recommending Tyrben's fork.

For more information on ``git vendor`` there are a short `usage description <https://github.com/Tyrben/git-vendor#usage>`_
in the repositories ``README`` file and a website for the original repository has a `manual page <https://brettlangdon.github.io/git-vendor/>`_ which is also included in both
repositories.

To support a flexible loading of external libraries, a file unique to the vendored
library must be placed in ``vendor/init.d/`` with the ``.bash`` extension.

Rebasing a feature branch with an added/updated vendored library
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

If your feature branch with a newly added/updated vendored lib has fallen behind master
you might need to rebase it before creating a PR. However rebasing with dangling
subtree commits can cause problems.
The following rebase strategy will pause the rebase at the point where you added a
subtree and let you add it again before continuing the rebasing.

::

[feature/branch] $ git rebase --rebase-merges --strategy subtree master
fatal: refusing to merge unrelated histories
Could not apply 0d6a56b... Add-preexec-from-https-github-com-rcaloras-bash-preexec-0-4-1- # Add "preexec" from "https://github.com/rcaloras/[email protected]"
[feature/branch] $ git vendor add preexec https://github.com/rcaloras/bash-preexec 0.4.1
...
[feature/branch] $ git rebase --continue

If rebasing makes you a little uneasy (as it probably should). You can always test in
another branch.

::

[feater/branch] $ git checkout -b feature/branch-test-rebase
[feater/branch-test-rebase] $ git rebase --rebase-merges --strategy subtree master
...

Afterwards you can make sure the rebase was successful by running ``git vendor list``
to see if your library is still recognized as a vendored lib

::

[feature/branch] $ git vendor list
[email protected]:
name: preexec
dir: vendor/github.com/rcaloras/bash-preexec
repo: https://github.com/rcaloras/bash-preexec
ref: 0.4.1
commit: 8fe585c5cf377a3830b895fe26e694b020d8db1a
[feature/branch] $


Plugin Disable Callbacks
------------------------

Expand Down
199 changes: 0 additions & 199 deletions lib/preexec.bash

This file was deleted.

16 changes: 16 additions & 0 deletions plugins/available/cmd-returned-notify.plugin.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# shellcheck shell=bash
cite about-plugin
about-plugin 'Alert (BEL) when process ends after a threshold of seconds'

precmd_return_notification() {
export LAST_COMMAND_DURATION=$(($(date +%s) - ${LAST_COMMAND_TIME:=$(date +%s)}))
[[ ${LAST_COMMAND_DURATION} -gt ${NOTIFY_IF_COMMAND_RETURNS_AFTER:-5} ]] && echo -e "\a"
export LAST_COMMAND_TIME=
}

preexec_return_notification() {
[ -z "${LAST_COMMAND_TIME}" ] && export LAST_COMMAND_TIME=$(date +%s)
}

precmd_functions+=(precmd_return_notification)
preexec_functions+=(preexec_return_notification)
33 changes: 18 additions & 15 deletions plugins/available/xterm.plugin.bash
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
# shellcheck shell=bash
cite about-plugin
about-plugin 'automatically set your xterm title with host and location info'


_short-dirname () {
local dir_name=`dirs +0`
[ "$SHORT_TERM_LINE" = true ] && [ ${#dir_name} -gt 8 ] && echo ${dir_name##*/} || echo $dir_name
_short-dirname() {
local dir_name=$(dirs +0)
[ "$SHORT_TERM_LINE" = true ] && [ "${#dir_name}" -gt 8 ] && echo "${dir_name##*/}" || echo "${dir_name}"
}

_short-command () {
local input_command="$@"
[ "$SHORT_TERM_LINE" = true ] && [ ${#input_command} -gt 8 ] && echo ${input_command%% *} || echo $input_command
_short-command() {
local input_command="$*"
[ "$SHORT_TERM_LINE" = true ] && [ "${#input_command}" -gt 8 ] && echo "${input_command%% *}" || echo "${input_command}"
}

set_xterm_title () {
local title="$1"
echo -ne "\033]0;$title\007"
set_xterm_title() {
local title="$1"
echo -ne "\033]0;$title\007"
}

precmd () {
set_xterm_title "${SHORT_USER:-${USER}}@${SHORT_HOSTNAME:-${HOSTNAME}} `_short-dirname` $PROMPTCHAR"
precmd_xterm_title() {
set_xterm_title "${SHORT_USER:-${USER}}@${SHORT_HOSTNAME:-${HOSTNAME}} $(_short-dirname) $PROMPT_CHAR"
}

preexec () {
set_xterm_title "`_short-command $1` {`_short-dirname`} (${SHORT_USER:-${USER}}@${SHORT_HOSTNAME:-${HOSTNAME}})"
preexec_xterm_title() {
set_xterm_title "$(_short-command "${1}") {$(_short-dirname)} (${SHORT_USER:-${USER}}@${SHORT_HOSTNAME:-${HOSTNAME}})"
}

case "$TERM" in
xterm*|rxvt*) preexec_install;;
xterm* | rxvt*)
precmd_functions+=(precmd_xterm_title)
preexec_functions+=(preexec_xterm_title)
;;
esac
Empty file.
Empty file.
Loading

0 comments on commit 59afe9f

Please sign in to comment.