Skip to content

Commit 7baa936

Browse files
author
Vasiliy Polyakov
committed
save
1 parent 97dc65c commit 7baa936

File tree

7 files changed

+88
-113
lines changed

7 files changed

+88
-113
lines changed

lib.bash

Lines changed: 49 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
#### Source guard #######################################################@{1
1010
[ x"$BASH_VERSION" = x ] && return 1 # Current shell is not Bash
11-
[[ -n $BASH_LIB ]] && return 0 # Sourced already
11+
[[ -n $BASH_LIB ]] && return 0 # Library was already sourced
1212
(( BASH_VERSINFO[0] < 4 )) && return 2 # Bash version is not supported
13-
type -f realpath dirname &>/dev/null || return 3 # Coreutils are not installed
13+
type -p dirname realpath &>/dev/null || return 3 # Coreutils are not installed
1414

1515
## @c BASH_LIB contains the path to the Bash library directory.
16-
declare -gr BASH_LIB="$(dirname "$(realpath -qe "${BASH_SOURCE[0]}")")/lib"
16+
declare -gr BASH_LIB="$(realpath -qe "$(dirname "${BASH_SOURCE[0]}")/lib")"
1717

18-
#### Private functions ##################################################@{1
18+
#### Library initialization #############################################@{1
1919
_lib::init() {
2020
# Set options
2121
shopt -qs extglob # enable extended pattern matching
@@ -25,65 +25,35 @@ _lib::init() {
2525
}
2626

2727
#### Exported functions #################################################@{1
28-
## @name Function stubs.
29-
## @{
30-
_() { echo "$@"; }
31-
dbg() { :; }
32-
title() { false; }
33-
## @}
34-
35-
## @name Source guards.
36-
## @{
37-
38-
## @brief Is this script sourced?
39-
## @param $1 Function call stack frame number (>= 1).
40-
## @return Boolean value.
41-
## @retval 0 Sourced.
42-
## @retval 1 Not sourced.
43-
is_sourced() {
44-
local -i frame="${1:-1}"
45-
[[ ${FUNCNAME[frame]} == 'source' ]]
46-
}
4728

48-
## @brief Is this script imported?
49-
## @param $1 Function call stack frame number (>= 1).
50-
## @return Boolean value.
51-
## @retval 0 Imported.
52-
## @retval 1 Not imported.
53-
is_imported() {
54-
local -i frame="${1:-1}"
55-
[[ ${FUNCNAME[frame]} == 'source' && ${FUNCNAME[frame+1]} == 'import' ]]
56-
}
57-
58-
## @brief Import a library file only once.
59-
## @detail Use `import_once || return $(($?-1))`
29+
## @brief Bash library header.
30+
## @detail Use `bash_lib || return $(($?-1))`
6031
## at the beginning of the Bash library file (@c lib/*.sh).
6132
## @return Import error.
6233
## @retval 0 OK.
63-
## @retval 1 Library has been imported already.
64-
## @retval 2 Bash lib was not initialized correctly.
65-
## @retval 3 This library was not imported correctly.
66-
## @retval 127 import_once: command not found (retured by Bash).
67-
import_once() {
68-
[ x"$BASH_LIB" = x ] && return 2
69-
70-
local name="$(relpath "${BASH_SOURCE[1]}" "$BASH_LIB")"
71-
name="${name%.*}"; name="_BASH_${name^^}_"
34+
## @retval 1 Library was already imported.
35+
## @retval 125 This library was not imported correctly.
36+
## @retval 126 Bash lib was not initialized correctly.
37+
## @retval 127 'library: command not found' (returned by Bash itself).
38+
bash_lib() {
39+
[ x"$BASH_LIB" = x ] && return 126
40+
41+
local name="$1"
42+
local -i version="${2:-1}"
43+
44+
if [[ -z $name ]]; then
45+
name="$(relpath "${BASH_SOURCE[1]}" "$BASH_LIB")"
46+
name="${name%.*}"
47+
fi
48+
name="__BASH_${name^^}__"
7249

7350
eval "(($name))" && return 1
74-
is_imported 2 || return 3
51+
is_imported 2 || return 125
7552

76-
local -i version="${1:-1}"
7753
eval "declare -gri $name='$version'"
78-
79-
return 0
8054
}
81-
## @}
82-
83-
## @name Source libraries.
84-
## @{
8555

86-
## Import Bash library.
56+
## @brief Import Bash library.
8757
## @param $1 Library name.
8858
## @param $@ Additional parameters for the library.
8959
## @return Error code.
@@ -103,8 +73,34 @@ import() {
10373

10474
source "$lib" "$@"
10575
}
76+
77+
## @name Function stubs.
78+
## @{
79+
_() { echo "$@"; }
80+
dbg() { :; }
81+
title() { false; }
10682
## @}
10783

84+
## @brief Is this script sourced?
85+
## @param $1 Function call stack frame number (>= 1).
86+
## @return Boolean value.
87+
## @retval 0 Sourced.
88+
## @retval 1 Not sourced.
89+
is_sourced() {
90+
local -i frame="${1:-1}"
91+
[[ ${FUNCNAME[frame]} == 'source' ]]
92+
}
93+
94+
## @brief Is this script imported?
95+
## @param $1 Function call stack frame number (>= 1).
96+
## @return Boolean value.
97+
## @retval 0 Imported.
98+
## @retval 1 Not imported.
99+
is_imported() {
100+
local -i frame="${1:-1}"
101+
[[ ${FUNCNAME[frame]} == 'source' && ${FUNCNAME[frame+1]} == 'import' ]]
102+
}
103+
108104
## Convert array to a regular expression.
109105
## @param $@ Array values.
110106
## @return Nothing.

lib/colors.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
## @pre lib.bash (Bash scripting library).
66
## @pre ncurses (tput).
77

8-
import_once || return $(($?-1))
8+
bash_lib || return $(($?-1))
99

10-
if ! type -p tput >/dev/null || [[ $1 == '0' ]]; then
10+
if ! type -p tput &>/dev/null || [[ $1 == '0' ]]; then
1111
declare -gir COLORS=0
1212
declare -gr SGR0=''
1313
SGR() { :; }
@@ -87,4 +87,3 @@ SGR() {
8787
echo -n $E "$P0\e[${A[*]}m$P1"
8888
fi
8989
}
90-

lib/exceptions.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
## @pre lib.bash (Bash scripting library).
66
## @pre sysexits.sh (exit/return code constants).
77

8-
import_once || return $(($?-1))
8+
bash_lib || return $(($?-1))
9+
910
import sysexits
1011

1112
#### Private functions ##################################################@{1

lib/git.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
## @pre lib.bash (Bash scripting library).
66
## @pre Git (Git VCS).
77

8-
import_once || return $(($?-1))
8+
bash_lib || return $(($?-1))
99

1010
git::in_work_tree() {
1111
local inside="$(git rev-parse --is-inside-work-tree 2>/dev/null)"

lib/prompt.sh

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
## @see https://www.askapache.com/linux/bash-power-prompt/
99
## @see https://gitweb.gentoo.org/repo/gentoo.git/tree/app-shells/bash/files/bashrc
1010

11-
import_once || return $(($?-1))
11+
bash_lib || return $(($?-1))
12+
1213
import colors
13-
import title
1414
import git
1515

1616
declare -gA PROMPT_COLOR PROMPT_CHAR
@@ -52,6 +52,7 @@ prompt::init() {
5252

5353
declare -r PROMPT_COLOR PROMPT_CHAR
5454
declare -g PROMPT_COMMAND='prompt::cmd;'
55+
#declare -g PS0='\[\e]2;bash> ${BASH_COMMAND}\e\\\]'
5556
export VIRTUAL_ENV_DISABLE_PROMPT=1
5657
}
5758

@@ -79,7 +80,7 @@ prompt::cmd() {
7980
return
8081
fi
8182

82-
local R="\[$SGR0\]" x u='user' user at host dir venv char chroot
83+
local R="\[$SGR0\]" x u='user' user at host dir venv char title chroot
8384
(( EUID )) || u='root' # Superuser
8485

8586
# Set prompt fragments
@@ -90,6 +91,9 @@ prompt::cmd() {
9091
char="${PROMPT_COLOR[$u]}${PROMPT_CHAR[$u]}$R"
9192
chroot="${debian_chroot:+($debian_chroot) }"
9293

94+
if (( PROMPT_TITLE )); then
95+
title='\[\e]2;bash> \u@\h:\w\e\\\]'
96+
fi
9397
if [[ -n $SSH_TTY ]]; then
9498
# Connected over SSH
9599
at="${PROMPT_COLOR[ssh]}${PROMPT_CHAR[at]}$R"
@@ -100,8 +104,7 @@ prompt::cmd() {
100104
fi
101105

102106
# Set prompt
103-
PS1="$chroot$user$at$host:$dir$(prompt::vcs)$venv\n╰$char "
107+
PS1="$title$chroot$user$at$host:$dir$(prompt::vcs)$venv\n╰$char "
104108

105109
echo -en '\e[6n' && read -sdR x && (( ${x##*;} > 1 )) && echo
106110
}
107-

lib/title.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## @file $XDG_DATA_HOME/bash/lib/title.sh
2+
## @brief Set terminal and window titles.
3+
## @author Vasiliy Polyakov
4+
## @date 2019
5+
## @pre lib.bash (Bash scripting library).
6+
## @pre ncurses (tput).
7+
8+
bash_lib || return $(($?-1))
9+
type -p tput &>/dev/null || return 1
10+
11+
title() {
12+
local tsl=$'\e]2;' fsl=$'\e\\' title
13+
14+
title="$*"
15+
title="${title@Q}"
16+
title="${title//\'\\\'\'/\'}"
17+
title="${title//\\\'/\'}"
18+
if (( ${#title} > 1 )); then
19+
title="${title#?(\$)\'}"
20+
title="${title%\'}"
21+
fi
22+
23+
printf "$tsl%s$fsl" "$title"
24+
}

test/test.sh

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,6 @@
22
# vim: set ft=sh et sw=2 ts=2:
33

44
. "$(realpath -qe "$(dirname "${BASH_SOURCE[0]}")/../lib.bash")"
5-
import colors
6-
7-
declare -a C=(0 1 2 3) C8=(8 1 2 3 4 5 6 7 8) C16=(16) C256=(256 1)
8-
declare -n c="C0"
9-
echo ${c[0]} ${#c[@]}
10-
11-
exit 0
12-
13-
declare -g _ret_func=''
14-
15-
fail() {
16-
false 1"2 3" 34
17-
success
18-
return 5
19-
}
20-
21-
success() {
22-
:
23-
return 0
24-
}
25-
26-
_err_trap() {
27-
local -i code="$1" line="$5"
28-
local cmd="$2" func="$3" file="$4"
29-
local msg="$(_ 'command %s exited with %d')"
30-
local -a args=("${cmd@Q}" "$code")
31-
32-
if [[ $cmd == return* ]]; then
33-
msg="$(_ 'function %s() returned %d')"
34-
args=("$_ret_func" "$code")
35-
fi
36-
37-
printf "error: $msg\n" "${args[@]}"
38-
}
39-
40-
catch() {
41-
local a="$1"; shift
42-
local b="${*:-:}"
43-
44-
echo "$b"
45-
}
46-
47-
shopt -qso errtrace functrace
48-
trap '_ret_func="${FUNCNAME[0]}"' RETURN
49-
trap '_err_trap "$?" "$BASH_COMMAND" "${FUNCNAME[0]}" "${BASH_SOURCE[0]}" "$LINENO"' ERR
50-
#declare -ft fail
51-
52-
success
53-
fail
54-
catch 1
5+
import prompt
556

7+
prompt::init

0 commit comments

Comments
 (0)