1

I'm using terminator with zsh in combination with antigen. The theme (a modified version of af-magic) I'm using retrieves the current window width and prints a bar consisting of = signs to separate inputs.

# af-magic.zsh-theme
# Repo: https://github.com/andyfleming/oh-my-zsh
# Direct Link: https://github.com/andyfleming/oh-my-zsh/blob/master/themes/af-magic.zsh-theme

if [ $UID -eq 0 ]; then NCOLOR="red"; else NCOLOR="green"; fi
local return_code="%(?..%{$fg[red]%}%? ↵%{$reset_color%})"

# primary prompt

BAR=$(printf '=%.0s' {1..$(tput cols)})

PROMPT='$FG[237]$BAR%{$reset_color%}
$FG[032]%~\
$(git_prompt_info) \
$FG[105]%(!.#.»)%{$reset_color%} '
PROMPT2='%{$fg[red]%}\ %{$reset_color%}'
RPS1='${return_code}'


# color vars
eval my_gray='$FG[237]'
eval my_orange='$FG[214]'

# right prompt
if type "virtualenv_prompt_info" > /dev/null
then
    RPROMPT='$(virtualenv_prompt_info)$my_gray%n@%m%{$reset_color%}%'
else
    RPROMPT='$my_gray%n@%m%{$reset_color%}%'
fi

# git settings
ZSH_THEME_GIT_PROMPT_PREFIX="$FG[075](branch:"
ZSH_THEME_GIT_PROMPT_CLEAN=""
ZSH_THEME_GIT_PROMPT_DIRTY="$my_orange*%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="$FG[075])%{$reset_color%}"
# af-magic.zsh-theme
# Repo: https://github.com/andyfleming/oh-my-zsh
# Direct Link: https://github.com/andyfleming/oh-my-zsh/blob/master/themes/af-magic.zsh-theme

if [ $UID -eq 0 ]; then NCOLOR="red"; else NCOLOR="green"; fi
local return_code="%(?..%{$fg[red]%}%? ↵%{$reset_color%})"

# primary prompt

BAR=$(printf '=%.0s' {1..$(tput cols)})

PROMPT='$FG[237]$BAR%{$reset_color%}
$FG[032]%~\
$(git_prompt_info) \
$FG[105]%(!.#.»)%{$reset_color%} '
PROMPT2='%{$fg[red]%}\ %{$reset_color%}'
RPS1='${return_code}'


# color vars
eval my_gray='$FG[237]'
eval my_orange='$FG[214]'

# right prompt
if type "virtualenv_prompt_info" > /dev/null
then
    RPROMPT='$(virtualenv_prompt_info)$my_gray%n@%m%{$reset_color%}%'
else
    RPROMPT='$my_gray%n@%m%{$reset_color%}%'
fi

# git settings
ZSH_THEME_GIT_PROMPT_PREFIX="$FG[075](branch:"
ZSH_THEME_GIT_PROMPT_CLEAN=""
ZSH_THEME_GIT_PROMPT_DIRTY="$my_orange*%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="$FG[075])%{$reset_color%}"

This works pretty well, but when resizing the window the BAR is not updated and so it's either too short or too long, taking multiple lines. Example:

|==================================================================|
|~ »                                                               |

becomes

|==================================================| 
|================                                  | <- window border
|~ »                                               |

Is there a way to make either zsh or terminator reload the theme upon resizing the window?

2

When the terminal is resized, the shell receives a SIGWINCH signal. So update BAR in a trap. In zsh you can define the corresponding trap function:

TRAPWINCH () {
  BAR=$(printf '=%.0s' {1..$COLUMNS})
}

You don't need to call tput since zsh tracks the number of columns of the terminal in the COLUMNS variable.

Make sure to turn on the prompt_subst option (setopt prompt_subst) so that $PROMPT is reevaluated each time it's displayed. Alternatively, update PROMPT in TRAPWINCH as well.

Instead of forking to call printf in a strange way, you can use parameter expansion to get a padded string. Parameter expansion normally works from the value of a variable, but ${:-STRING} lets you work from a string instead, in this case an empty string. This way you don't need a trap at all.

setopt prompt_subst
PROMPT='$FG[237]${(l:$COLUMNS::=:):-}%{$reset_color%}
1

To quote this answer:

Make sure that the prompt_subst option is turned on. If necessary, add the following line to your ~/.zshrc:

setopt prompt_subst

This tells zsh to reevaluate the prompt string each time it's displaying a prompt.

  • I tried that, but it didn't work. I'll update my question to include the complete theme – herhuf Apr 22 '17 at 12:44
0

You need to turn $BAR into a function:

function bar {
    echo `printf '=%.0s' {1..$(tput cols)}`
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.