Check for TMUX only once, check whether $TMUX is writeable

Fixes #801
This commit is contained in:
ZyX 2014-02-15 20:21:09 +04:00
parent 4e6cd08078
commit 6c1571139f
4 changed files with 69 additions and 48 deletions

View File

@ -9,26 +9,37 @@ if test -z "${POWERLINE_COMMAND}" ; then
fi
fi
_powerline_tmux_setenv() {
if [[ -n "$TMUX" ]]; then
tmux setenv -g TMUX_"$1"_`tmux display -p "#D" | tr -d %` "$2"
tmux refresh -S
_powerline_init_tmux_support() {
# Note: `test -w ""` returns false, so first condition may be removed
if test -n "$TMUX" && test -w "$TMUX" ; then
# TMUX variable may be unset to create new tmux session inside this one
_POWERLINE_TMUX="$TMUX"
_powerline_tmux_setenv() {
TMUX="$_POWERLINE_TMUX" tmux setenv -g TMUX_"$1"_`tmux display -p "#D" | tr -d %` "$2"
TMUX="$_POWERLINE_TMUX" tmux refresh -S
}
_powerline_tmux_set_pwd() {
if test "x$_POWERLINE_SAVED_PWD" != "x$PWD" ; then
_POWERLINE_SAVED_PWD="$PWD"
_powerline_tmux_setenv PWD "$PWD"
fi
}
_powerline_tmux_set_columns() {
_powerline_tmux_setenv COLUMNS "$COLUMNS"
}
trap "_powerline_tmux_set_columns" SIGWINCH
_powerline_tmux_set_columns
else
_powerline_tmux_set_pwd() {
return 0
}
fi
}
_POWERLINE_SAVED_PWD=
_powerline_tmux_set_pwd() {
if test "x$_POWERLINE_SAVED_PWD" != "x$PWD" ; then
_POWERLINE_SAVED_PWD="$PWD"
_powerline_tmux_setenv PWD "$PWD"
fi
}
_powerline_tmux_set_columns() {
_powerline_tmux_setenv COLUMNS "$COLUMNS"
}
_powerline_prompt() {
local last_exit_code=$?
PS1="$($POWERLINE_COMMAND shell left -r bash_prompt --last_exit_code=$last_exit_code --jobnum="$(jobs -p|wc -l)")"
@ -36,8 +47,7 @@ _powerline_prompt() {
return $last_exit_code
}
trap "_powerline_tmux_set_columns" SIGWINCH
_powerline_tmux_set_columns
[[ "$PROMPT_COMMAND" != "${PROMPT_COMMAND/_powerline_prompt/}" ]] ||
export PROMPT_COMMAND="${PROMPT_COMMAND}"$'\n'"_powerline_prompt"
_powerline_init_tmux_support

View File

@ -20,18 +20,18 @@ function powerline-setup
"
end
_powerline_update
function _powerline_tmux_setenv
if test -n "$TMUX"
if test -w "$TMUX"
function _powerline_tmux_setenv
tmux setenv -g TMUX_$argv[1]_(tmux display -p "#D" | tr -d "%") "$argv[2]"
tmux refresh -S
end
function --on-variable PWD _powerline_tmux_set_pwd
_powerline_tmux_setenv PWD "$PWD"
end
function --on-signal WINCH _powerline_tmux_set_columns
_powerline_tmux_setenv COLUMNS "$COLUMNS"
end
_powerline_tmux_set_columns
_powerline_tmux_set_pwd
end
function --on-variable PWD _powerline_tmux_set_pwd
_powerline_tmux_setenv PWD "$PWD"
end
function --on-signal WINCH _powerline_tmux_set_columns
_powerline_tmux_setenv COLUMNS "$COLUMNS"
end
_powerline_tmux_set_columns
_powerline_tmux_set_pwd
end

View File

@ -14,7 +14,7 @@ if ! $?POWERLINE_COMMAND then
setenv POWERLINE_COMMAND $POWERLINE_SOURCED:h:h:h:h:q/scripts/powerline
endif
endif
alias _powerline_tmux_set_pwd 'if $?TMUX tmux setenv -g TMUX_PWD_`tmux display -p "#D" | tr -d %` $PWD:q ; if $?TMUX tmux refresh -S'
alias _powerline_tmux_set_pwd 'if ( $?TMUX && { test -w $TMUX:q } ) tmux setenv -g TMUX_PWD_`tmux display -p "#D" | tr -d %` $PWD:q ; if ( $?TMUX && { test -w $TMUX:q } ) tmux refresh -S'
alias _powerline_set_prompt 'set prompt="`$POWERLINE_COMMAND shell left -r tcsh_prompt --last_exit_code=$?`"'
alias _powerline_set_rprompt 'set rprompt="`$POWERLINE_COMMAND shell right -r tcsh_prompt --last_exit_code=$?` "'
alias cwdcmd "`alias cwdcmd` ; _powerline_tmux_set_pwd"

View File

@ -8,22 +8,37 @@ if test -z "${POWERLINE_COMMAND}" ; then
fi
fi
_powerline_tmux_setenv() {
integer _POWERLINE_JOBNUM
_powerline_init_tmux_support() {
emulate -L zsh
if [[ -n "$TMUX" ]]; then
tmux setenv -g TMUX_"$1"_$(tmux display -p "#D" | tr -d %) "$2"
tmux refresh -S
# Note: `test -w ""` returns false, so first condition may be removed
if test -n "$TMUX" && test -w "$TMUX" ; then
# TMUX variable may be unset to create new tmux session inside this one
typeset -g _POWERLINE_TMUX="$TMUX"
function -g _powerline_tmux_setenv() {
emulate -L zsh
local -x TMUX="$_POWERLINE_TMUX"
tmux setenv -g TMUX_"$1"_$(tmux display -p "#D" | tr -d %) "$2"
tmux refresh -S
}
function -g _powerline_tmux_set_pwd() {
_powerline_tmux_setenv PWD "$PWD"
}
function -g _powerline_tmux_set_columns() {
_powerline_tmux_setenv COLUMNS "$COLUMNS"
}
chpwd_functions+=( _powerline_tmux_set_pwd )
trap "_powerline_tmux_set_columns" SIGWINCH
_powerline_tmux_set_columns
_powerline_tmux_set_pwd
fi
}
_powerline_tmux_set_pwd() {
_powerline_tmux_setenv PWD "$PWD"
}
_powerline_tmux_set_columns() {
_powerline_tmux_setenv COLUMNS "$COLUMNS"
}
_powerline_precmd() {
# If you are wondering why I am not using the same code as I use for bash
# ($(jobs|wc -l)): consider the following test:
@ -47,7 +62,6 @@ _powerline_setup_prompt() {
fi
done
precmd_functions+=( _powerline_precmd )
chpwd_functions+=( _powerline_tmux_set_pwd )
_powerline_set_true_keymap_name "${${(Q)${${(z)${"$(bindkey -lL main)"}}[3]}}:-.safe}"
if zmodload zsh/zpython &>/dev/null ; then
zpython 'from powerline.bindings.zsh import setup as _powerline_setup'
@ -106,10 +120,7 @@ if [[ "$_POWERLINE_MODE" != vi* ]] ; then
export _POWERLINE_DEFAULT_MODE="$_POWERLINE_MODE"
fi
trap "_powerline_tmux_set_columns" SIGWINCH
_powerline_tmux_set_columns
_powerline_tmux_set_pwd
setopt promptpercent
setopt promptsubst
_powerline_setup_prompt
_powerline_init_tmux_support