Skip to content

Commit

Permalink
Provide unified functions for formatting text output
Browse files Browse the repository at this point in the history
`text` plugin provides all needed for making beautiful logging.
It provides several functions which produce formatted and colored
output.
The `box` function (a previous primary method of logging) is replaced
with the new one.

#167
  • Loading branch information
extsoft committed Sep 14, 2019
1 parent 472327c commit 4565878
Show file tree
Hide file tree
Showing 16 changed files with 173 additions and 110 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,13 @@ it. Each command file has to provide the following BASH functions:
### Coding rules
We enforce having a consistent implementation by following the next strict rules:
- add `#!/usr/bin/env bash` at the beginning of each script
- use `boxtee` to execute each original `git` command
- use `git-verbose` instead of `git` if you need to print a `git`'s command to CLI
- a private function (a usage in the scope of current script) should be prefixed with `--`

If you need to write a message to the system output, please use public functions in
[libexec/plugins/text](libexec/plugins/text). All help messages have to use `cat`
for printing them.

### Debug mode
You can enable debug mode by running `export GED=1` (the equivalent of `set -x` for `bash`).
Run `unset GED` to switch debug off.
Expand Down
6 changes: 4 additions & 2 deletions install.bash
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ copy(){
local FROM=${1}
local INTO=${2}
install -d -m 755 ${INTO}/{bin,libexec,completions}
install -m 755 ${FROM}/bin/* ${INTO}/bin
install -m 755 ${FROM}/libexec/* ${INTO}/libexec
install -d -m 755 ${INTO}/libexec/plugins
install -m 755 ${FROM}/bin/git* ${INTO}/bin
install -m 755 ${FROM}/libexec/git* ${INTO}/libexec
install -m 755 ${FROM}/libexec/plugins/* ${INTO}/libexec/plugins
install -m 644 ${FROM}/completions/* ${INTO}/completions
install -m 644 ${FROM}/LICENSE ${INTO}
install -m 644 ${FROM}/README.md ${INTO}
Expand Down
36 changes: 7 additions & 29 deletions libexec/git-elegant
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,14 @@ set -e
# It registers all "libexec" scripts.
BINS=$(dirname ${0})
export PATH=${BINS}:${PATH}
source ${BINS}/plugins/text

__red=`tput setaf 1`
__green=`tput setaf 2`
__magenta=`tput setaf 5`
__reset=`tput sgr0`
__site="https://elegant-git.bees-hive.org"

box(){
t="$@xxxxxx"
c=${replace:-=}
echo ${t//?/$c}
echo "$c$c $@ $c$c"
echo ${t//?/$c}
}

boxtee() {
box "$@"
"$@"
}

__gm() {
echo "${__green}$@${__reset}"
}

__rm() {
echo "${__red}$@${__reset}"
}

__mmn() {
echo -n "${__magenta}$@${__reset}"
git-verbose() {
command-text "git $@"
git "$@"
}

MASTER="master"
Expand All @@ -45,7 +23,7 @@ RMASTER="${REMOTE_NAME}/master"
_error-if-empty() {
# _error-if-empty <a value to check> <error message>
if [[ -z "$1" ]]; then
__rm "$2"
error-text "$2"
exit 45
fi
}
Expand All @@ -61,7 +39,7 @@ __loop_ask() {
local m="$1"; shift
[ -z "$1" ] && return 0
for i in $@; do
__mmn "$m [$i] "
info-text "$m [$i] "
read answer
if [ -z "$answer" ]; then
eval "$c $i"
Expand All @@ -81,7 +59,7 @@ __batch() {
local MM="$1"; shift
local AM="$1"; shift
local CM="$1"; shift
__mmn "$MM "
info-text "$MM "
read answer
if [ -z "$answer" ]; then
__loop "$CM" $@
Expand Down
14 changes: 7 additions & 7 deletions libexec/git-elegant-accept-work
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ MESSAGE
default() {
local WORK_BRANCH="__eg"
git elegant obtain-work "${1}" "${WORK_BRANCH}"
boxtee git status
boxtee git rebase ${RMASTER}
git-verbose status
git-verbose rebase ${RMASTER}
# @todo #137 Alternative flows of `accept-work`
# If this command is executed with `${WORK_BRANCH}` and
# 1. `git rebase` is in progress, it has to run `git rebase --continue` prior working with
# `${MASTER}` branch.
# 2. `git rebase` is completed (history of `${WORK_BRANCH}` and `${RMASTER}` are different),
# just work with `${MASTER}` branch.
local ACTUAL_REMOTE=$(git for-each-ref --format='%(upstream:short)' refs/heads/${WORK_BRANCH})
boxtee git checkout ${MASTER}
boxtee git merge --ff-only ${WORK_BRANCH}
boxtee git push ${REMOTE_NAME} ${MASTER}:${MASTER}
boxtee git branch --delete --force ${WORK_BRANCH}
boxtee git push ${REMOTE_NAME} --delete $(branch-from-remote-reference ${ACTUAL_REMOTE})
git-verbose checkout ${MASTER}
git-verbose merge --ff-only ${WORK_BRANCH}
git-verbose push ${REMOTE_NAME} ${MASTER}:${MASTER}
git-verbose branch --delete --force ${WORK_BRANCH}
git-verbose push ${REMOTE_NAME} --delete $(branch-from-remote-reference ${ACTUAL_REMOTE})
}
16 changes: 8 additions & 8 deletions libexec/git-elegant-acquire-repository
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ __ask_question() {
}

__interactive-configuration() {
box "Interactive configuration. Please hit enter if you wish {default value}."
info-box "Interactive configuration. Please hit enter if you wish {default value}."
FUNCTIONS=$@
for f in ${FUNCTIONS[@]}; do
unset ANSWER
while [ -z "${ANSWER}" ]; do
__ask_question ${f}
if [[ -n "${ANSWER}" ]]; then
boxtee git config --local $(eval "echo -n \$${f}_key") "${ANSWER}"
git-verbose config --local $(eval "echo -n \$${f}_key") "${ANSWER}"
fi
done
done
Expand All @@ -133,29 +133,29 @@ __mandatory-configuration() {
[[ "${config}" =~ "darwin" || "${config}" =~ "linux" ]] && continue
fi
local data=($(eval "echo -n \${${config}[@]}"))
boxtee git config --local ${data[0]} ${data[1]}
git-verbose config --local ${data[0]} ${data[1]}
done
}
__remove-old-aliases() {
old_aliases=($(git config --get-regexp ^alias\. | grep "elegant " | cut -f 1 -d " "))
if [[ ${#old_aliases[@]} -ne 0 ]]; then
local counter=0
for old in ${old_aliases[@]}; do
boxtee git config --local --unset ${old} &&
git-verbose config --local --unset ${old} &&
counter=$((counter+1)) ||
box "Non-local alias! Remove it if needed using 'git config --global --unset ${old}'"
info-box "Non-local alias! Remove it if needed using 'git config --global --unset ${old}'"
done
box "${counter} git aliases were removed that contained 'elegant git' reference."
info-box "${counter} git aliases were removed that contained 'elegant git' reference."
else
box "There are no git aliases which contain 'elegant git' reference."
info-box "There are no git aliases which contain 'elegant git' reference."
fi
}

__aliases-configuration() {
for command in ${@}; do
local alias=${command}
local origin="elegant ${command}"
boxtee git config --local "alias.${alias}" "${origin}"
git-verbose config --local "alias.${alias}" "${origin}"
done
}

Expand Down
10 changes: 5 additions & 5 deletions libexec/git-elegant-amend-work
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ MESSAGE
default(){
local BRANCH=$(__branches 'git branch | grep \*')
if [[ "$BRANCH" == "master" ]]; then
box "No commits to 'master' branch. Please read more on ${__site}"
box "Try 'git elegant start-work' prior to retrying this command."
error-box "No commits to 'master' branch. Please read more on ${__site}"
error-box "Try 'git elegant start-work' prior to retrying this command."
exit 42
fi
boxtee git add --interactive
boxtee git diff --cached --check
boxtee git commit --amend
git-verbose add --interactive
git-verbose diff --cached --check
git-verbose commit --amend
}
4 changes: 2 additions & 2 deletions libexec/git-elegant-clear-local
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ default() {
local cmd="git branch -lvv | grep gone | awk {'print \$1'}"
__loop "git branch -d" $(eval "$cmd") || \
(
__gm "There are unmerged branches:" && \
__loop "__gm -" $(eval "$cmd") && \
info-text "There are unmerged branches:" && \
__loop "info-text -" $(eval "$cmd") && \
__batch "Do you want to delete all unmerged branches?" "Delete this?" "git branch -D" $(eval "$cmd")
)
}
2 changes: 1 addition & 1 deletion libexec/git-elegant-clone-repository
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ MESSAGE

default() {
_error-if-empty "$1" "Cloneable URL is not set."
boxtee git clone "$1"
git-verbose clone "$1"
cd $(basename -s .git $1)
git elegant acquire-repository
}
8 changes: 4 additions & 4 deletions libexec/git-elegant-deliver-work
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ MESSAGE
default() {
local BRANCH=$(__branches 'git branch | grep \*')
if [[ "$BRANCH" == "master" ]]; then
box "No pushes to 'master' branch. Please read more on ${__site}"
error-box "No pushes to 'master' branch. Please read more on ${__site}"
exit 42
fi
boxtee git fetch
boxtee git rebase ${RMASTER}
boxtee git push --set-upstream --force origin ${BRANCH}:${BRANCH}
git-verbose fetch
git-verbose rebase ${RMASTER}
git-verbose push --set-upstream --force origin ${BRANCH}:${BRANCH}
}
2 changes: 1 addition & 1 deletion libexec/git-elegant-init-repository
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ MESSAGE
}

default() {
boxtee git init
git-verbose init
git elegant acquire-repository
}
8 changes: 4 additions & 4 deletions libexec/git-elegant-obtain-work
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ MESSAGE
default() {
local PATTERN=${1}
_error-if-empty "${PATTERN}" "Please provide a branch name or its part."
boxtee git fetch --all
git-verbose fetch --all
local REMOTE_BRANCHES=(
$(git for-each-ref --format='%(refname:strip=2)' refs/remotes/** | grep "${PATTERN}")
)
Expand All @@ -41,7 +41,7 @@ default() {
for branch in ${REMOTE_BRANCHES[@]}; do
echo " - ${branch}"
done
box "Please re-run the command with concrete branch name from the list above!"
error-box "Please re-run the command with concrete branch name from the list above!"
exit 43
fi
if [[ ${#REMOTE_BRANCHES[@]} = 1 ]]; then
Expand All @@ -50,9 +50,9 @@ default() {
if [[ -z ${LOCAL} ]]; then
LOCAL=$(branch-from-remote-reference ${REMOTE})
fi
boxtee git checkout -B ${LOCAL} ${REMOTE}
git-verbose checkout -B ${LOCAL} ${REMOTE}
else
box "There is no branch that matches the '${PATTERN}' pattern."
error-box "There is no branch that matches the '${PATTERN}' pattern."
exit 43
fi
}
10 changes: 5 additions & 5 deletions libexec/git-elegant-save-work
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ MESSAGE
default(){
local BRANCH=$(__branches 'git branch | grep \*')
if [[ "$BRANCH" == "master" ]]; then
box "No commits to 'master' branch. Please read more on ${__site}"
box "Try 'git elegant start-work' prior to retrying this command."
error-box "No commits to 'master' branch. Please read more on ${__site}"
error-box "Try 'git elegant start-work' prior to retrying this command."
exit 42
fi
boxtee git add --interactive
boxtee git diff --cached --check
boxtee git commit
git-verbose add --interactive
git-verbose diff --cached --check
git-verbose commit
}
12 changes: 6 additions & 6 deletions libexec/git-elegant-start-work
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ MESSAGE

default() {
_error-if-empty "$1" "Please give a name for the new branch."
status=$(boxtee git stash save elegant-git)
git checkout ${MASTER}
git pull
boxtee git checkout -b "$1"
status=$(git-verbose stash save elegant-git)
git-verbose checkout ${MASTER}
git-verbose pull
git-verbose checkout -b "$1"

if [[ "$status" =~ "Saved working directory" ]]; then
boxtee git stash apply stash^{/elegant-git}
boxtee git stash drop stash@{0}
git-verbose stash apply stash^{/elegant-git}
git-verbose stash drop stash@{0}
fi
}
79 changes: 79 additions & 0 deletions libexec/plugins/text
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env bash

# 8 ANSI colors
TEXT_COLOR_BLACK=30
TEXT_COLOR_RED=31
TEXT_COLOR_GREEN=32
TEXT_COLOR_BROWN=33
TEXT_COLOR_BLUE=34
TEXT_COLOR_PURPLE=35
TEXT_COLOR_CYAN=36
TEXT_COLOR_GRAY=37
TEXT_COLOR_DEFAULT=0

# 4 ANSI text formats
TEXT_FORMAT_NORMAL=0
TEXT_FORMAT_BOLD=1
TEXT_FORMAT_UNDERLINE=4
TEXT_FORMAT_BLINKING=5

--colored-text() {
# usage: --colored-text <format> <color> <message>...
local COLOR=""
local RESET=""
if [[ -t 1 ]]; then
COLOR="\e[${1};${2}m"
RESET="\e[m"
fi
shift; shift
printf "${COLOR}"
local prefix=""
for part in "$@"; do
printf "%s" "${prefix}${part}"
prefix=" "
done
printf "${RESET}"
printf "\n"
}

command-text() {
# Display a CLI command
local COLOR=""
local RESET=""
if [[ -t 1 ]]; then
COLOR="\e[${TEXT_FORMAT_BOLD};${TEXT_COLOR_GREEN}m"
RESET="\e[m"
fi
printf "${COLOR}==>> ${RESET}"
--colored-text ${TEXT_FORMAT_BOLD} ${TEXT_COLOR_BLUE} "$@"
}

info-text() {
# Display a regular message
--colored-text ${TEXT_FORMAT_NORMAL} ${TEXT_COLOR_GREEN} "$@"
}

--box-text(){
local LAYOUT=${1}
shift
t="$@xxxxxx"
c=${replace:-=}
${LAYOUT} ${t//?/$c}
${LAYOUT} "$c$c $@ $c$c"
${LAYOUT} ${t//?/$c}
}

info-box() {
# Display an important message
--box-text info-text "$@"
}

error-text() {
# Display an error message within a single-line log
--colored-text ${TEXT_FORMAT_BOLD} ${TEXT_COLOR_RED} "$@"
}

error-box() {
# Display an error message within a multi-line log
--box-text error-text "$@"
}
Loading

0 comments on commit 4565878

Please sign in to comment.