forked from ohmybash/oh-my-bash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oh-my-bash.sh
165 lines (141 loc) · 5.65 KB
/
oh-my-bash.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env bash
# Bail out early if non-interactive
#
# Note: We cannot produce any error messages here because, in some systems,
# /etc/gdm3/Xsession sources ~/.profile and checks stderr. If there is any
# stderr ourputs, it refuses to start the session.
case $- in
*i*) ;;
*) return;;
esac
if [ ! -n "${BASH_VERSION-}" ]; then
printf '%s\n' 'oh-my-bash: This is not a Bash. Use OMB with Bash 3.2 or higher.' >&2
return 1
fi
_omb_bash_version=$((BASH_VERSINFO[0] * 10000 + BASH_VERSINFO[1] * 100 + BASH_VERSINFO[2]))
if ((_omb_bash_version < 30200)); then
printf '%s\n' "oh-my-bash: OMB does not support this version of Bash ($BASH_VERSION)" >&2
printf '%s\n' "oh-my-bash: Use OMB with Bash 3.2 or higher" >&2
return 1
fi
OMB_VERSINFO=(1 0 0 0 master noarch)
OMB_VERSION="${OMB_VERSINFO[0]}.${OMB_VERSINFO[1]}.${OMB_VERSINFO[2]}(${OMB_VERSINFO[3]})-${OMB_VERSINFO[4]} (${OMB_VERSINFO[5]})"
_omb_version=$((OMB_VERSINFO[0] * 10000 + OMB_VERSINFO[1] * 100 + OMB_VERSINFO[2]))
# Check for updates on initial load...
if [[ $DISABLE_AUTO_UPDATE != true ]]; then
source "$OSH"/tools/check_for_upgrade.sh
fi
# Initializes Oh My Bash
# Set OSH_CUSTOM to the path where your custom config files
# and plugins exists, or else we will use the default custom/
if [[ ! ${OSH_CUSTOM-} ]]; then
OSH_CUSTOM=$OSH/custom
[[ -d $OSH_CUSTOM && -O $OSH_CUSTOM ]] ||
OSH_CUSTOM=${XDG_DATA_HOME:-$HOME/.local/share}/oh-my-bash/custom
fi
# Set OSH_CACHE_DIR to the path where cache files should be created
# or else we will use the default cache/
if [[ ! ${OSH_CACHE_DIR-} ]]; then
OSH_CACHE_DIR=$OSH/cache
[[ -d $OSH_CACHE_DIR && -O $OSH_CACHE_DIR ]] ||
OSH_CACHE_DIR=${XDG_STATE_HOME:-$HOME/.local/state}/oh-my-bash/cache
fi
_omb_module_loaded=
_omb_module_require() {
local status=0
local -a files=()
while (($#)); do
local type=lib name=$1; shift
[[ $name == *:* ]] && type=${name%%:*} name=${name#*:}
name=${name%.bash}
name=${name%.sh}
[[ ' '$_omb_module_loaded' ' == *" $type:$name "* ]] && continue
_omb_module_loaded="$_omb_module_loaded $type:$name"
local -a locations=()
case $type in
lib) locations=({"$OSH_CUSTOM","$OSH"}/lib/"$name".{bash,sh}) ;;
plugin) locations=({"$OSH_CUSTOM","$OSH"}/plugins/"$name"/"$name".plugin.{bash,sh}) ;;
alias) locations=({"$OSH_CUSTOM","$OSH"}/aliases/"$name".aliases.{bash,sh}) ;;
completion) locations=({"$OSH_CUSTOM","$OSH"}/completions/"$name".completion.{bash,sh}) ;;
theme) locations=({"$OSH_CUSTOM"{,/themes},"$OSH"/themes}/"$name"/"$name".theme.{bash,sh}) ;;
*)
echo "oh-my-bash (module_require): unknown module type '$type'." >&2
status=2
continue ;;
esac
local path
for path in "${locations[@]}"; do
if [[ -f $path ]]; then
files+=("$path")
continue 2
fi
done
echo "oh-my-bash (module_require): module '$type:$name' not found." >&2
status=127
done
if ((status==0)); then
local path
for path in "${files[@]}"; do
source "$path" || status=$?
done
fi
return "$status"
}
_omb_module_require_lib() { _omb_module_require "${@/#/lib:}"; }
_omb_module_require_plugin() { _omb_module_require "${@/#/plugin:}"; }
_omb_module_require_alias() { _omb_module_require "${@/#/alias:}"; }
_omb_module_require_completion() { _omb_module_require "${@/#/completion:}"; }
_omb_module_require_theme() { _omb_module_require "${@/#/theme:}"; }
# Load all of the config files in ~/.oh-my-bash/lib that end in .sh
# TIP: Add files you don't want in git to .gitignore
_omb_module_require_lib utils
_omb_util_glob_expand _omb_init_files '{"$OSH","$OSH_CUSTOM"}/lib/*.{bash,sh}'
_omb_init_files=("${_omb_init_files[@]##*/}")
_omb_init_files=("${_omb_init_files[@]%.bash}")
_omb_init_files=("${_omb_init_files[@]%.sh}")
_omb_module_require_lib "${_omb_init_files[@]}"
unset -v _omb_init_files
# Figure out the SHORT hostname
if [[ "$OSTYPE" = darwin* ]]; then
# macOS's $HOST changes with dhcp, etc. Use ComputerName if possible.
SHORT_HOST=$(scutil --get ComputerName 2>/dev/null) || SHORT_HOST=${HOST/.*/}
else
SHORT_HOST=${HOST/.*/}
fi
# Load all of the plugins that were defined in ~/.bashrc
_omb_module_require_plugin "${plugins[@]}"
# Load all of the aliases that were defined in ~/.bashrc
_omb_module_require_alias "${aliases[@]}"
# Load all of the completions that were defined in ~/.bashrc
_omb_module_require_completion "${completions[@]}"
# Load all of your custom configurations from custom/
_omb_util_glob_expand _omb_init_files '"$OSH_CUSTOM"/*.{sh,bash}'
for _omb_init_file in "${_omb_init_files[@]}"; do
[[ -f $_omb_init_file ]] &&
source "$_omb_init_file"
done
unset -v _omb_init_files _omb_init_file
# Load the theme
if [[ $OSH_THEME == random ]]; then
_omb_util_glob_expand _omb_init_files '"$OSH"/themes/*/*.theme.sh'
if ((${#_omb_init_files[@]})); then
_omb_init_file=${_omb_init_files[RANDOM%${#_omb_init_files[@]}]}
source "$_omb_init_file"
OMB_THEME_RANDOM_SELECTED=${_omb_init_file##*/}
OMB_THEME_RANDOM_SELECTED=${OMB_THEME_RANDOM_SELECTED%.theme.bash}
OMB_THEME_RANDOM_SELECTED=${OMB_THEME_RANDOM_SELECTED%.theme.sh}
echo "[oh-my-bash] Random theme '$OMB_THEME_RANDOM_SELECTED' ($_omb_init_file) loaded..."
fi
unset -v _omb_init_files _omb_init_file
elif [[ $OSH_THEME ]]; then
_omb_module_require_theme "$OSH_THEME"
fi
if [[ $PROMPT ]]; then
export PS1="\["$PROMPT"\]"
fi
if ! _omb_util_command_exists '__git_ps1' ; then
source "$OSH/tools/git-prompt.sh"
fi
# Adding Support for other OSes
[ -s /usr/bin/gloobus-preview ] && PREVIEW="gloobus-preview" ||
[ -s /Applications/Preview.app ] && PREVIEW="/Applications/Preview.app" || PREVIEW="less"