Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command aliases #211

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bindings/clean_plugins → commands/tpm-clean
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

# Tmux key-binding script.
# Tmux command script.
# Scripts intended to be used via the command line are in `bin/` directory.

CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
Expand Down
2 changes: 1 addition & 1 deletion bindings/install_plugins → commands/tpm-install
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

# Tmux key-binding script.
# Tmux command script.
# Scripts intended to be used via the command line are in `bin/` directory.

CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
Expand Down
2 changes: 1 addition & 1 deletion bindings/update_plugins → commands/tpm-update
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

# Tmux key-binding script.
# Tmux command script.
# Scripts intended to be used via the command line are in `bin/` directory.

# This script:
Expand Down
4 changes: 4 additions & 0 deletions scripts/variables.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ default_update_key="U"
clean_key_option="@tpm-clean"
default_clean_key="M-u"

# TODO
# Since command-alias was not previosly used in tpm we need to make sure
# that the version here reflects the requirements for the command-alias to
# be present the required version for command-alias to work
SUPPORTED_TMUX_VERSION="1.9"

DEFAULT_TPM_ENV_VAR_NAME="TMUX_PLUGIN_MANAGER_PATH"
Expand Down
18 changes: 14 additions & 4 deletions tpm
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash

CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
BINDINGS_DIR="$CURRENT_DIR/bindings"
COMMANDS_DIR="$CURRENT_DIR/commands"
SCRIPTS_DIR="$CURRENT_DIR/scripts"

source "$SCRIPTS_DIR/variables.sh"
Expand Down Expand Up @@ -53,18 +53,27 @@ source_plugins() {
"$SCRIPTS_DIR/source_plugins.sh" >/dev/null 2>&1
}

# tpm-install - downloads TPM plugins and reloads TMUX environment
# tpm-update - updates a plugin (or all of them) and reloads TMUX environment
# tpm-clean - remove unused TPM plugins and reloads TMUX environment
set_tpm_commands() {
tmux set-option -ag command-alias "tpm-install=run-shell $COMMANDS_DIR/tpm-install"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This statement isn't idempotent because of the -a flag, so be careful. If someone reloads their config, this file could get sourced again and append duplicate aliases. tmux seems to use the first registered alias for a given key, so subsequent calls to set_tpm_commands would effectively be ignored until a new session is started.

I also noticed a bug where using -a causes aliases that contain commas to be split across multiple entries. This shouldn't be an issue for this PR, but it makes me feel like using -a with array options like command-alias isn't fully tested.

tmux set-option -ag command-alias "tpm-update=run-shell $COMMANDS_DIR/tpm-update"
tmux set-option -ag command-alias "tpm-clean=run-shell $COMMANDS_DIR/tpm-clean"
}

# prefix + I - downloads TPM plugins and reloads TMUX environment
# prefix + U - updates a plugin (or all of them) and reloads TMUX environment
# prefix + alt + u - remove unused TPM plugins and reloads TMUX environment
set_tpm_key_bindings() {
local install_key="$(get_tmux_option "$install_key_option" "$default_install_key")"
tmux bind-key "$install_key" run-shell "$BINDINGS_DIR/install_plugins"
tmux bind-key "$install_key" tpm-install

local update_key="$(get_tmux_option "$update_key_option" "$default_update_key")"
tmux bind-key "$update_key" run-shell "$BINDINGS_DIR/update_plugins"
tmux bind-key "$update_key" tpm-update

local clean_key="$(get_tmux_option "$clean_key_option" "$default_clean_key")"
tmux bind-key "$clean_key" run-shell "$BINDINGS_DIR/clean_plugins"
tmux bind-key "$clean_key" tpm-clean
}

supported_tmux_version_ok() {
Expand All @@ -74,6 +83,7 @@ supported_tmux_version_ok() {
main() {
if supported_tmux_version_ok; then
set_tpm_path
set_tpm_commands
set_tpm_key_bindings
source_plugins
fi
Expand Down