mirror of
https://github.com/ohmyzsh/ohmyzsh.git
synced 2026-08-07 06:08:39 +02:00
feat(tmux): add ZSH_TMUX_AUTOSTART_EXCLUDE option
This commit is contained in:
parent
70ad5e3df8
commit
4faff2d710
2 changed files with 41 additions and 0 deletions
|
|
@ -35,6 +35,7 @@ The plugin also supports the following:
|
||||||
| `ZSH_TMUX_AUTOREFRESH` | Automatically refresh global environments (default: `false`) |
|
| `ZSH_TMUX_AUTOREFRESH` | Automatically refresh global environments (default: `false`) |
|
||||||
| `ZSH_TMUX_AUTOSTART` | Automatically starts tmux (default: `false`) |
|
| `ZSH_TMUX_AUTOSTART` | Automatically starts tmux (default: `false`) |
|
||||||
| `ZSH_TMUX_AUTOSTART_ONCE` | Autostart only if tmux hasn't been started previously (default: `true`) |
|
| `ZSH_TMUX_AUTOSTART_ONCE` | Autostart only if tmux hasn't been started previously (default: `true`) |
|
||||||
|
| `ZSH_TMUX_AUTOSTART_EXCLUDE` | Space-separated list of POSIX ERE regex patterns for sessions to skip when autostarting (default: `""`) |
|
||||||
| `ZSH_TMUX_AUTOCONNECT` | Automatically connect to a previous session if it exits (default: `true`) |
|
| `ZSH_TMUX_AUTOCONNECT` | Automatically connect to a previous session if it exits (default: `true`) |
|
||||||
| `ZSH_TMUX_AUTOQUIT` | Automatically closes terminal once tmux exits (default: `ZSH_TMUX_AUTOSTART`) |
|
| `ZSH_TMUX_AUTOQUIT` | Automatically closes terminal once tmux exits (default: `ZSH_TMUX_AUTOSTART`) |
|
||||||
| `ZSH_TMUX_CONFIG` | Set the configuration path (default: `$HOME/.tmux.conf`, `$XDG_CONFIG_HOME/tmux/tmux.conf`) |
|
| `ZSH_TMUX_CONFIG` | Set the configuration path (default: `$HOME/.tmux.conf`, `$XDG_CONFIG_HOME/tmux/tmux.conf`) |
|
||||||
|
|
|
||||||
|
|
@ -49,6 +49,10 @@ else
|
||||||
fi
|
fi
|
||||||
# Set -u option to support unicode
|
# Set -u option to support unicode
|
||||||
: ${ZSH_TMUX_UNICODE:=false}
|
: ${ZSH_TMUX_UNICODE:=false}
|
||||||
|
# Space-separated list of POSIX ERE regex patterns to exclude sessions when
|
||||||
|
# autostarting. If all running sessions match, a new one is created.
|
||||||
|
# Example: ZSH_TMUX_AUTOSTART_EXCLUDE="^dev-.* staging$"
|
||||||
|
: ${ZSH_TMUX_AUTOSTART_EXCLUDE:=""}
|
||||||
|
|
||||||
# ALIASES
|
# ALIASES
|
||||||
function _build_tmux_alias {
|
function _build_tmux_alias {
|
||||||
|
|
@ -107,6 +111,31 @@ else
|
||||||
export _ZSH_TMUX_FIXED_CONFIG="${0:h:a}/tmux.only.conf"
|
export _ZSH_TMUX_FIXED_CONFIG="${0:h:a}/tmux.only.conf"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Finds the first tmux session not matching any of the given patterns.
|
||||||
|
# $1 = space-separated list of POSIX ERE regex patterns.
|
||||||
|
# Prints session name and returns 0, or returns 1 if all match or none exist.
|
||||||
|
function _tmux_find_first_non_excluded_session() {
|
||||||
|
local _patterns="${1}"
|
||||||
|
# If empty or only whitespace, no patterns to match
|
||||||
|
[[ -z "${_patterns// }" ]] && return 1
|
||||||
|
# Strip all leading and trailing spaces
|
||||||
|
_patterns="${_patterns#"${_patterns%%[! ]*}"}"
|
||||||
|
_patterns="${_patterns%"${_patterns##*[! ]}"}"
|
||||||
|
|
||||||
|
local _combined="${(j:|:)${(s: :)_patterns}}"
|
||||||
|
local session_name
|
||||||
|
|
||||||
|
while IFS= read -r session_name; do
|
||||||
|
[[ -z "$session_name" ]] && continue
|
||||||
|
if [[ ! "$session_name" =~ $_combined ]]; then
|
||||||
|
print -- "$session_name"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
done < <(command tmux list-sessions -F '#S' 2>/dev/null)
|
||||||
|
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
# Wrapper function for tmux.
|
# Wrapper function for tmux.
|
||||||
function _zsh_tmux_plugin_run() {
|
function _zsh_tmux_plugin_run() {
|
||||||
if [[ -n "$@" ]]; then
|
if [[ -n "$@" ]]; then
|
||||||
|
|
@ -134,6 +163,17 @@ function _zsh_tmux_plugin_run() {
|
||||||
session_name="$ZSH_TMUX_DEFAULT_SESSION_NAME"
|
session_name="$ZSH_TMUX_DEFAULT_SESSION_NAME"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Check for non-excluded sessions
|
||||||
|
if [[ -n "$ZSH_TMUX_AUTOSTART_EXCLUDE" ]]; then
|
||||||
|
local _exclude_session
|
||||||
|
_exclude_session="$(_tmux_find_first_non_excluded_session "$ZSH_TMUX_AUTOSTART_EXCLUDE")"
|
||||||
|
if [[ $? -eq 0 ]]; then
|
||||||
|
$tmux_cmd attach $_detached -t "$_exclude_session"
|
||||||
|
[[ "$ZSH_TMUX_AUTOQUIT" == "true" ]] && exit
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
# Try to connect to an existing session.
|
# Try to connect to an existing session.
|
||||||
if [[ -n "$session_name" ]]; then
|
if [[ -n "$session_name" ]]; then
|
||||||
[[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach $_detached -t "$session_name"
|
[[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach $_detached -t "$session_name"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue