1
0
Fork 0
mirror of synced 2024-11-15 22:18:58 -05:00
tpm/scripts/helpers/plugin_functions.sh

87 lines
2.2 KiB
Bash
Raw Normal View History

# using @tpm_plugins is now deprecated in favor of using @plugin syntax
2014-05-18 18:35:55 -04:00
tpm_plugins_variable_name="@tpm_plugins"
# manually expanding tilde char or `$HOME` variable.
_manual_expansion() {
local path="$1"
local expanded_tilde="${path/#\~/$HOME}"
echo "${expanded_tilde/#\$HOME/$HOME}"
}
_tpm_path() {
local string_path="$(tmux start-server\; show-environment -g TMUX_PLUGIN_MANAGER_PATH | cut -f2 -d=)/"
_manual_expansion "$string_path"
}
_CACHED_TPM_PATH="$(_tpm_path)"
_tmux_conf_contents() {
cat /etc/tmux.conf ~/.tmux.conf 2>/dev/null
if [ "$1" == "full" ]; then # also output content from sourced files
local file
for file in $(_sourced_files); do
cat $(_manual_expansion "$file") 2>/dev/null
done
fi
}
# return files sourced from tmux config files
_sourced_files() {
_tmux_conf_contents |
2016-02-01 01:41:03 -05:00
awk '/^[ \t]*source(-file)? +/ { gsub(/'\''/,""); gsub(/'\"'/,""); print $2 }'
}
# Want to be able to abort in certain cases
trap "exit 1" TERM
export TOP_PID=$$
_fatal_error_abort() {
echo >&2 "Aborting."
kill -s TERM $TOP_PID
}
# PUBLIC FUNCTIONS BELOW
tpm_path() {
if [ "$_CACHED_TPM_PATH" == "/" ]; then
echo >&2 "FATAL: Tmux Plugin Manager not configured in tmux.conf"
_fatal_error_abort
fi
echo "$_CACHED_TPM_PATH"
}
2015-08-03 11:40:50 -04:00
tpm_plugins_list_helper() {
2015-12-13 19:21:47 -05:00
# lists plugins from @tpm_plugins option
echo "$(tmux start-server\; show-option -gqv "$tpm_plugins_variable_name")"
# read set -g @plugin "tmux-plugins/tmux-example-plugin" entries
_tmux_conf_contents "full" |
2016-02-01 01:41:03 -05:00
awk '/^[ \t]*set(-option)? +-g +@plugin/ { gsub(/'\''/,""); gsub(/'\"'/,""); print $4 }'
2014-05-18 18:35:55 -04:00
}
# Allowed plugin name formats:
# 1. "git://github.com/user/plugin_name.git"
# 2. "user/plugin_name"
2015-08-03 11:40:50 -04:00
plugin_name_helper() {
2014-08-05 13:02:42 -04:00
local plugin="$1"
2014-05-24 17:15:13 -04:00
# get only the part after the last slash, e.g. "plugin_name.git"
2014-08-05 13:02:42 -04:00
local plugin_basename="$(basename "$plugin")"
2014-05-24 17:15:13 -04:00
# remove ".git" extension (if it exists) to get only "plugin_name"
2014-08-05 13:02:42 -04:00
local plugin_name="${plugin_basename%.git}"
echo "$plugin_name"
2014-05-18 18:35:55 -04:00
}
2015-08-03 11:40:50 -04:00
plugin_path_helper() {
2015-08-02 19:22:06 -04:00
local plugin="$1"
2015-08-03 11:40:50 -04:00
local plugin_name="$(plugin_name_helper "$plugin")"
echo "$(tpm_path)${plugin_name}/"
2014-05-18 18:35:55 -04:00
}
plugin_already_installed() {
2014-08-05 13:02:42 -04:00
local plugin="$1"
2015-08-03 11:40:50 -04:00
local plugin_path="$(plugin_path_helper "$plugin")"
[ -d "$plugin_path" ] &&
cd "$plugin_path" &&
git remote >/dev/null 2>&1
}