Use `stty size` or zero as a fallback for $COLUMNS

Fixes #924
This commit is contained in:
ZyX 2014-07-19 19:56:31 +04:00
parent 12a93c57ad
commit f92e6eb54c
3 changed files with 50 additions and 7 deletions

View File

@ -1,3 +1,15 @@
_powerline_columns_fallback() {
if which stty &>/dev/null ; then
local cols="$(stty size 2>/dev/null)"
if ! test -z "$cols" ; then
echo "${cols#* }"
return 0
fi
fi
echo 0
return 0
}
_powerline_init_tmux_support() {
if test -n "$TMUX" && tmux refresh -S &>/dev/null ; then
# TMUX variable may be unset to create new tmux session inside this one
@ -16,7 +28,7 @@ _powerline_init_tmux_support() {
}
_powerline_tmux_set_columns() {
_powerline_tmux_setenv COLUMNS "$COLUMNS"
_powerline_tmux_setenv COLUMNS "${COLUMNS:-$(_powerline_columns_fallback)}"
}
trap "_powerline_tmux_set_columns" SIGWINCH
@ -32,7 +44,7 @@ _powerline_init_tmux_support() {
_run_powerline() {
# Arguments: side, last_exit_code, jobnum
$POWERLINE_COMMAND shell $1 -w $COLUMNS -r bash_prompt --last_exit_code=$2 --jobnum=$3
$POWERLINE_COMMAND shell $1 -w "${COLUMNS:-$(_powerline_columns_fallback)}" -r bash_prompt --last_exit_code=$2 --jobnum=$3
}
_powerline_prompt() {

View File

@ -1,4 +1,23 @@
function powerline-setup
function _powerline_columns_fallback
if which stty ^/dev/null
if stty size ^/dev/null
stty size | cut -d' ' -f2
return 0
end
end
echo 0
return 0
end
function _powerline_columns
if test -z "$COLUMNS"
_powerline_columns_fallback
else
echo "$COLUMNS"
end
end
if test -z "$POWERLINE_NO_FISH_PROMPT$POWERLINE_NO_SHELL_PROMPT"
if test -z "$POWERLINE_COMMAND"
if which powerline-client >/dev/null
@ -20,11 +39,11 @@ function powerline-setup
if test -z "$POWERLINE_NO_FISH_ABOVE$POWERLINE_NO_SHELL_ABOVE"
set promptside aboveleft
set rpromptpast 'echo -n " "'
set columnsexpr '(math $COLUMNS - 1)'
set columnsexpr '(math (_powerline_columns) - 1)'
else
set promptside left
set rpromptpast
set columnsexpr '$COLUMNS'
set columnsexpr '(_powerline_columns)'
end
eval "
function fish_prompt
@ -53,7 +72,7 @@ function powerline-setup
_powerline_tmux_setenv PWD "$PWD"
end
function --on-signal WINCH _powerline_tmux_set_columns
_powerline_tmux_setenv COLUMNS "$COLUMNS"
_powerline_tmux_setenv COLUMNS (_powerline_columns)
end
_powerline_tmux_set_columns
_powerline_tmux_set_pwd

View File

@ -8,6 +8,18 @@ if test -z "${POWERLINE_COMMAND}" ; then
fi
fi
_powerline_columns_fallback() {
if which stty &>/dev/null ; then
local cols="$(stty size 2>/dev/null)"
if ! test -z "$cols" ; then
echo "${cols#* }"
return 0
fi
fi
echo 0
return 0
}
integer _POWERLINE_JOBNUM
_powerline_init_tmux_support() {
@ -28,7 +40,7 @@ _powerline_init_tmux_support() {
}
function -g _powerline_tmux_set_columns() {
_powerline_tmux_setenv COLUMNS "$COLUMNS"
_powerline_tmux_setenv COLUMNS "${COLUMNS:-$(_powerline_columns_fallback)}"
}
chpwd_functions+=( _powerline_tmux_set_pwd )
@ -126,7 +138,7 @@ _powerline_setup_prompt() {
new_args_2+=' -R local_theme=continuation'
local add_args_3=$add_args' -R local_theme=select'
local add_args_2=$add_args$new_args_2
add_args+=' --width=$(( COLUMNS - 1 ))'
add_args+=' --width=$(( ${COLUMNS:-$(_powerline_columns_fallback)} - 1 ))'
local add_args_r2=$add_args$new_args_2
PS1='$($POWERLINE_COMMAND shell aboveleft -r zsh_prompt '$add_args')'
RPS1='$($POWERLINE_COMMAND shell right -r zsh_prompt '$add_args')'