From d9321031384d5202023772a03225bd7df01f3490 Mon Sep 17 00:00:00 2001 From: Alexander Barton Date: Sat, 21 Mar 2020 14:14:19 +0100 Subject: [PATCH] Fix axzsh_is_widechar_terminal() logic axzsh_is_widechar_terminal() returns 0 when the terminal CAN'T display wiede characters in multiple columns, and non-null, when the terminal CAN display wide characters. Fix themes accordingly. --- core/11_terminal/11_terminal.zshrc | 24 +++++++++++++++++------- themes/ax.axzshtheme | 2 +- themes/axemoji.axzshtheme | 4 ++-- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/core/11_terminal/11_terminal.zshrc b/core/11_terminal/11_terminal.zshrc index 3990129..a15c1d4 100644 --- a/core/11_terminal/11_terminal.zshrc +++ b/core/11_terminal/11_terminal.zshrc @@ -21,7 +21,21 @@ function axzsh_is_utf_terminal { } alias isutfenv=axzsh_is_utf_terminal -# Check if terminal supports "wide" characters. +# Get the length of a string when shown on the terminal. The return code of the +# function is the length in "cells". Note: Echo'ing the length to the terminal, +# which looks cleaner at first, doesn't work: this command can't be called with +# its stdin and/or stdout redirected, as it it must be able to interact with the +# terminal (write to and read from it). +function axzsh_get_displayed_length { + echo -ne "$*\033[6n" + read -s -d\[ garbage + read -s -d R pos + echo -ne "\033[1K\r" + return $((${pos#*;} - 1)) +} + +# Check if terminal correctly handles "wide" characters, which means, displays +# them with the correct width (>1). # typeset -g _axzsh_is_widechar_terminal_cache function axzsh_is_widechar_terminal { @@ -36,12 +50,8 @@ function _axzsh_is_widechar_terminal { [[ -t 1 ]] || return 1 [[ -z "$AXZSH_PLUGIN_CHECK" ]] || return 1 axzsh_is_utf_terminal || return 1 - echo -ne "🍀\033[6n" - read -s -d\[ garbage - read -s -d R pos - echo -ne "\033[1K\r" - [[ "${pos#*;}" -eq 2 ]] || return 1 - return 0 + axzsh_get_displayed_length "🍀" + [[ $? -eq 2 ]] && return 0 || return 1 } # Test for "modern" terminal diff --git a/themes/ax.axzshtheme b/themes/ax.axzshtheme index 93eb88a..0a4eda4 100644 --- a/themes/ax.axzshtheme +++ b/themes/ax.axzshtheme @@ -16,7 +16,7 @@ ax_hostname_prompt_functions=($ax_hostname_prompt_functions ax_hostname_prompt_r PS1="$(ax_logname_prompt_yn)$(ax_hostname_prompt_yn)%B%2~%b"'$(ax_vcs_prompt)'"$(ax_prompt)" # Primary prompt on the right-hand side. -axzsh_is_widechar_terminal \ +axzsh_is_utf_terminal \ && RPS1="%(?..%{$fg_no_bold[red]%}%? ↵%{$reset_color%})" \ || RPS1="%(?..%{$fg_no_bold[red]%}<%?>%{$reset_color%})" diff --git a/themes/axemoji.axzshtheme b/themes/axemoji.axzshtheme index febacdc..331a322 100644 --- a/themes/axemoji.axzshtheme +++ b/themes/axemoji.axzshtheme @@ -4,7 +4,7 @@ ZSH_THEME_PROMPT_PREFIX_SPACING=" " ZSH_THEME_PROMPT_SUFFIX_SPACING=" " -if axzsh_is_widechar_terminal; then +if axzsh_is_utf_terminal; then # UTF8 capable terminal: if [[ -r "$HOME/.system_emoji-$SHORT_HOST" ]]; then system_emoji=$(<"$HOME/.system_emoji-$SHORT_HOST") @@ -13,7 +13,7 @@ if axzsh_is_widechar_terminal; then else system_emoji="🍀" fi - [[ -n "$VTE_VERSION" ]] \ + axzsh_is_widechar_terminal \ && emoji_spacing="" \ || emoji_spacing=" " -- 2.39.2