Skip to content

Commit 4565878

Browse files
committed
Provide unified functions for formatting text output
`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
1 parent 472327c commit 4565878

16 files changed

+173
-110
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,13 @@ it. Each command file has to provide the following BASH functions:
8080
### Coding rules
8181
We enforce having a consistent implementation by following the next strict rules:
8282
- add `#!/usr/bin/env bash` at the beginning of each script
83-
- use `boxtee` to execute each original `git` command
83+
- use `git-verbose` instead of `git` if you need to print a `git`'s command to CLI
8484
- a private function (a usage in the scope of current script) should be prefixed with `--`
8585

86+
If you need to write a message to the system output, please use public functions in
87+
[libexec/plugins/text](libexec/plugins/text). All help messages have to use `cat`
88+
for printing them.
89+
8690
### Debug mode
8791
You can enable debug mode by running `export GED=1` (the equivalent of `set -x` for `bash`).
8892
Run `unset GED` to switch debug off.

install.bash

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ copy(){
1111
local FROM=${1}
1212
local INTO=${2}
1313
install -d -m 755 ${INTO}/{bin,libexec,completions}
14-
install -m 755 ${FROM}/bin/* ${INTO}/bin
15-
install -m 755 ${FROM}/libexec/* ${INTO}/libexec
14+
install -d -m 755 ${INTO}/libexec/plugins
15+
install -m 755 ${FROM}/bin/git* ${INTO}/bin
16+
install -m 755 ${FROM}/libexec/git* ${INTO}/libexec
17+
install -m 755 ${FROM}/libexec/plugins/* ${INTO}/libexec/plugins
1618
install -m 644 ${FROM}/completions/* ${INTO}/completions
1719
install -m 644 ${FROM}/LICENSE ${INTO}
1820
install -m 644 ${FROM}/README.md ${INTO}

libexec/git-elegant

+7-29
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,14 @@ set -e
66
# It registers all "libexec" scripts.
77
BINS=$(dirname ${0})
88
export PATH=${BINS}:${PATH}
9+
source ${BINS}/plugins/text
910

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

16-
box(){
17-
t="$@xxxxxx"
18-
c=${replace:-=}
19-
echo ${t//?/$c}
20-
echo "$c$c $@ $c$c"
21-
echo ${t//?/$c}
22-
}
23-
24-
boxtee() {
25-
box "$@"
26-
"$@"
27-
}
28-
29-
__gm() {
30-
echo "${__green}$@${__reset}"
31-
}
32-
33-
__rm() {
34-
echo "${__red}$@${__reset}"
35-
}
3613

37-
__mmn() {
38-
echo -n "${__magenta}$@${__reset}"
14+
git-verbose() {
15+
command-text "git $@"
16+
git "$@"
3917
}
4018

4119
MASTER="master"
@@ -45,7 +23,7 @@ RMASTER="${REMOTE_NAME}/master"
4523
_error-if-empty() {
4624
# _error-if-empty <a value to check> <error message>
4725
if [[ -z "$1" ]]; then
48-
__rm "$2"
26+
error-text "$2"
4927
exit 45
5028
fi
5129
}
@@ -61,7 +39,7 @@ __loop_ask() {
6139
local m="$1"; shift
6240
[ -z "$1" ] && return 0
6341
for i in $@; do
64-
__mmn "$m [$i] "
42+
info-text "$m [$i] "
6543
read answer
6644
if [ -z "$answer" ]; then
6745
eval "$c $i"
@@ -81,7 +59,7 @@ __batch() {
8159
local MM="$1"; shift
8260
local AM="$1"; shift
8361
local CM="$1"; shift
84-
__mmn "$MM "
62+
info-text "$MM "
8563
read answer
8664
if [ -z "$answer" ]; then
8765
__loop "$CM" $@

libexec/git-elegant-accept-work

+7-7
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,18 @@ MESSAGE
4040
default() {
4141
local WORK_BRANCH="__eg"
4242
git elegant obtain-work "${1}" "${WORK_BRANCH}"
43-
boxtee git status
44-
boxtee git rebase ${RMASTER}
43+
git-verbose status
44+
git-verbose rebase ${RMASTER}
4545
# @todo #137 Alternative flows of `accept-work`
4646
# If this command is executed with `${WORK_BRANCH}` and
4747
# 1. `git rebase` is in progress, it has to run `git rebase --continue` prior working with
4848
# `${MASTER}` branch.
4949
# 2. `git rebase` is completed (history of `${WORK_BRANCH}` and `${RMASTER}` are different),
5050
# just work with `${MASTER}` branch.
5151
local ACTUAL_REMOTE=$(git for-each-ref --format='%(upstream:short)' refs/heads/${WORK_BRANCH})
52-
boxtee git checkout ${MASTER}
53-
boxtee git merge --ff-only ${WORK_BRANCH}
54-
boxtee git push ${REMOTE_NAME} ${MASTER}:${MASTER}
55-
boxtee git branch --delete --force ${WORK_BRANCH}
56-
boxtee git push ${REMOTE_NAME} --delete $(branch-from-remote-reference ${ACTUAL_REMOTE})
52+
git-verbose checkout ${MASTER}
53+
git-verbose merge --ff-only ${WORK_BRANCH}
54+
git-verbose push ${REMOTE_NAME} ${MASTER}:${MASTER}
55+
git-verbose branch --delete --force ${WORK_BRANCH}
56+
git-verbose push ${REMOTE_NAME} --delete $(branch-from-remote-reference ${ACTUAL_REMOTE})
5757
}

libexec/git-elegant-acquire-repository

+8-8
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,14 @@ __ask_question() {
107107
}
108108

109109
__interactive-configuration() {
110-
box "Interactive configuration. Please hit enter if you wish {default value}."
110+
info-box "Interactive configuration. Please hit enter if you wish {default value}."
111111
FUNCTIONS=$@
112112
for f in ${FUNCTIONS[@]}; do
113113
unset ANSWER
114114
while [ -z "${ANSWER}" ]; do
115115
__ask_question ${f}
116116
if [[ -n "${ANSWER}" ]]; then
117-
boxtee git config --local $(eval "echo -n \$${f}_key") "${ANSWER}"
117+
git-verbose config --local $(eval "echo -n \$${f}_key") "${ANSWER}"
118118
fi
119119
done
120120
done
@@ -133,29 +133,29 @@ __mandatory-configuration() {
133133
[[ "${config}" =~ "darwin" || "${config}" =~ "linux" ]] && continue
134134
fi
135135
local data=($(eval "echo -n \${${config}[@]}"))
136-
boxtee git config --local ${data[0]} ${data[1]}
136+
git-verbose config --local ${data[0]} ${data[1]}
137137
done
138138
}
139139
__remove-old-aliases() {
140140
old_aliases=($(git config --get-regexp ^alias\. | grep "elegant " | cut -f 1 -d " "))
141141
if [[ ${#old_aliases[@]} -ne 0 ]]; then
142142
local counter=0
143143
for old in ${old_aliases[@]}; do
144-
boxtee git config --local --unset ${old} &&
144+
git-verbose config --local --unset ${old} &&
145145
counter=$((counter+1)) ||
146-
box "Non-local alias! Remove it if needed using 'git config --global --unset ${old}'"
146+
info-box "Non-local alias! Remove it if needed using 'git config --global --unset ${old}'"
147147
done
148-
box "${counter} git aliases were removed that contained 'elegant git' reference."
148+
info-box "${counter} git aliases were removed that contained 'elegant git' reference."
149149
else
150-
box "There are no git aliases which contain 'elegant git' reference."
150+
info-box "There are no git aliases which contain 'elegant git' reference."
151151
fi
152152
}
153153

154154
__aliases-configuration() {
155155
for command in ${@}; do
156156
local alias=${command}
157157
local origin="elegant ${command}"
158-
boxtee git config --local "alias.${alias}" "${origin}"
158+
git-verbose config --local "alias.${alias}" "${origin}"
159159
done
160160
}
161161

libexec/git-elegant-amend-work

+5-5
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ MESSAGE
3131
default(){
3232
local BRANCH=$(__branches 'git branch | grep \*')
3333
if [[ "$BRANCH" == "master" ]]; then
34-
box "No commits to 'master' branch. Please read more on ${__site}"
35-
box "Try 'git elegant start-work' prior to retrying this command."
34+
error-box "No commits to 'master' branch. Please read more on ${__site}"
35+
error-box "Try 'git elegant start-work' prior to retrying this command."
3636
exit 42
3737
fi
38-
boxtee git add --interactive
39-
boxtee git diff --cached --check
40-
boxtee git commit --amend
38+
git-verbose add --interactive
39+
git-verbose diff --cached --check
40+
git-verbose commit --amend
4141
}

libexec/git-elegant-clear-local

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ default() {
3535
local cmd="git branch -lvv | grep gone | awk {'print \$1'}"
3636
__loop "git branch -d" $(eval "$cmd") || \
3737
(
38-
__gm "There are unmerged branches:" && \
39-
__loop "__gm -" $(eval "$cmd") && \
38+
info-text "There are unmerged branches:" && \
39+
__loop "info-text -" $(eval "$cmd") && \
4040
__batch "Do you want to delete all unmerged branches?" "Delete this?" "git branch -D" $(eval "$cmd")
4141
)
4242
}

libexec/git-elegant-clone-repository

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ MESSAGE
2929

3030
default() {
3131
_error-if-empty "$1" "Cloneable URL is not set."
32-
boxtee git clone "$1"
32+
git-verbose clone "$1"
3333
cd $(basename -s .git $1)
3434
git elegant acquire-repository
3535
}

libexec/git-elegant-deliver-work

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ MESSAGE
3232
default() {
3333
local BRANCH=$(__branches 'git branch | grep \*')
3434
if [[ "$BRANCH" == "master" ]]; then
35-
box "No pushes to 'master' branch. Please read more on ${__site}"
35+
error-box "No pushes to 'master' branch. Please read more on ${__site}"
3636
exit 42
3737
fi
38-
boxtee git fetch
39-
boxtee git rebase ${RMASTER}
40-
boxtee git push --set-upstream --force origin ${BRANCH}:${BRANCH}
38+
git-verbose fetch
39+
git-verbose rebase ${RMASTER}
40+
git-verbose push --set-upstream --force origin ${BRANCH}:${BRANCH}
4141
}

libexec/git-elegant-init-repository

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ MESSAGE
2828
}
2929

3030
default() {
31-
boxtee git init
31+
git-verbose init
3232
git elegant acquire-repository
3333
}

libexec/git-elegant-obtain-work

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ MESSAGE
3232
default() {
3333
local PATTERN=${1}
3434
_error-if-empty "${PATTERN}" "Please provide a branch name or its part."
35-
boxtee git fetch --all
35+
git-verbose fetch --all
3636
local REMOTE_BRANCHES=(
3737
$(git for-each-ref --format='%(refname:strip=2)' refs/remotes/** | grep "${PATTERN}")
3838
)
@@ -41,7 +41,7 @@ default() {
4141
for branch in ${REMOTE_BRANCHES[@]}; do
4242
echo " - ${branch}"
4343
done
44-
box "Please re-run the command with concrete branch name from the list above!"
44+
error-box "Please re-run the command with concrete branch name from the list above!"
4545
exit 43
4646
fi
4747
if [[ ${#REMOTE_BRANCHES[@]} = 1 ]]; then
@@ -50,9 +50,9 @@ default() {
5050
if [[ -z ${LOCAL} ]]; then
5151
LOCAL=$(branch-from-remote-reference ${REMOTE})
5252
fi
53-
boxtee git checkout -B ${LOCAL} ${REMOTE}
53+
git-verbose checkout -B ${LOCAL} ${REMOTE}
5454
else
55-
box "There is no branch that matches the '${PATTERN}' pattern."
55+
error-box "There is no branch that matches the '${PATTERN}' pattern."
5656
exit 43
5757
fi
5858
}

libexec/git-elegant-save-work

+5-5
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ MESSAGE
3232
default(){
3333
local BRANCH=$(__branches 'git branch | grep \*')
3434
if [[ "$BRANCH" == "master" ]]; then
35-
box "No commits to 'master' branch. Please read more on ${__site}"
36-
box "Try 'git elegant start-work' prior to retrying this command."
35+
error-box "No commits to 'master' branch. Please read more on ${__site}"
36+
error-box "Try 'git elegant start-work' prior to retrying this command."
3737
exit 42
3838
fi
39-
boxtee git add --interactive
40-
boxtee git diff --cached --check
41-
boxtee git commit
39+
git-verbose add --interactive
40+
git-verbose diff --cached --check
41+
git-verbose commit
4242
}

libexec/git-elegant-start-work

+6-6
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ MESSAGE
3434

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

4242
if [[ "$status" =~ "Saved working directory" ]]; then
43-
boxtee git stash apply stash^{/elegant-git}
44-
boxtee git stash drop stash@{0}
43+
git-verbose stash apply stash^{/elegant-git}
44+
git-verbose stash drop stash@{0}
4545
fi
4646
}

libexec/plugins/text

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env bash
2+
3+
# 8 ANSI colors
4+
TEXT_COLOR_BLACK=30
5+
TEXT_COLOR_RED=31
6+
TEXT_COLOR_GREEN=32
7+
TEXT_COLOR_BROWN=33
8+
TEXT_COLOR_BLUE=34
9+
TEXT_COLOR_PURPLE=35
10+
TEXT_COLOR_CYAN=36
11+
TEXT_COLOR_GRAY=37
12+
TEXT_COLOR_DEFAULT=0
13+
14+
# 4 ANSI text formats
15+
TEXT_FORMAT_NORMAL=0
16+
TEXT_FORMAT_BOLD=1
17+
TEXT_FORMAT_UNDERLINE=4
18+
TEXT_FORMAT_BLINKING=5
19+
20+
--colored-text() {
21+
# usage: --colored-text <format> <color> <message>...
22+
local COLOR=""
23+
local RESET=""
24+
if [[ -t 1 ]]; then
25+
COLOR="\e[${1};${2}m"
26+
RESET="\e[m"
27+
fi
28+
shift; shift
29+
printf "${COLOR}"
30+
local prefix=""
31+
for part in "$@"; do
32+
printf "%s" "${prefix}${part}"
33+
prefix=" "
34+
done
35+
printf "${RESET}"
36+
printf "\n"
37+
}
38+
39+
command-text() {
40+
# Display a CLI command
41+
local COLOR=""
42+
local RESET=""
43+
if [[ -t 1 ]]; then
44+
COLOR="\e[${TEXT_FORMAT_BOLD};${TEXT_COLOR_GREEN}m"
45+
RESET="\e[m"
46+
fi
47+
printf "${COLOR}==>> ${RESET}"
48+
--colored-text ${TEXT_FORMAT_BOLD} ${TEXT_COLOR_BLUE} "$@"
49+
}
50+
51+
info-text() {
52+
# Display a regular message
53+
--colored-text ${TEXT_FORMAT_NORMAL} ${TEXT_COLOR_GREEN} "$@"
54+
}
55+
56+
--box-text(){
57+
local LAYOUT=${1}
58+
shift
59+
t="$@xxxxxx"
60+
c=${replace:-=}
61+
${LAYOUT} ${t//?/$c}
62+
${LAYOUT} "$c$c $@ $c$c"
63+
${LAYOUT} ${t//?/$c}
64+
}
65+
66+
info-box() {
67+
# Display an important message
68+
--box-text info-text "$@"
69+
}
70+
71+
error-text() {
72+
# Display an error message within a single-line log
73+
--colored-text ${TEXT_FORMAT_BOLD} ${TEXT_COLOR_RED} "$@"
74+
}
75+
76+
error-box() {
77+
# Display an error message within a multi-line log
78+
--box-text error-text "$@"
79+
}

0 commit comments

Comments
 (0)