From 173a0b093cbf779702bae81a23f25788b8c40357 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Thu, 7 Nov 2019 14:49:05 +0100 Subject: [PATCH] [ADD] Use my own script to display terminal title The provided 'vte.sh' script doesn't quite work on Bash because $HOSTNAME is empty for some reason. --- bash/.bashrc | 10 ++-------- scripts/term-title | 45 +++++++++++++++++++++++++++++++++++++++++++++ shell/.profile | 3 --- zsh/.zshrc | 11 ++--------- 4 files changed, 49 insertions(+), 20 deletions(-) create mode 100644 scripts/term-title diff --git a/bash/.bashrc b/bash/.bashrc index a3d8993..dd55ec8 100644 --- a/bash/.bashrc +++ b/bash/.bashrc @@ -6,14 +6,8 @@ [[ $- != *i* ]] && return # Export our directory to Termite for opening new terminals -if { [[ "$TERM" == xterm-termite ]] || [[ "$TERM" == xterm ]]; } && - [ -r /etc/profile.d/vte.sh ]; then - if [[ "$TERM" == xterm ]]; then # FIXME: hack to make Xterm work - VTE_VERSION="${VTE_VERSION:-3405}" - fi - - . /etc/profile.d/vte.sh - __vte_prompt_command +if { [[ "$TERM" == xterm-termite ]] || [[ "$TERM" == xterm ]]; }; then + source ~/.scripts/term-title fi # Make colorcoding available for everyone diff --git a/scripts/term-title b/scripts/term-title new file mode 100644 index 0000000..3e47aa8 --- /dev/null +++ b/scripts/term-title @@ -0,0 +1,45 @@ +# A script to display $USER@$HOST:$PWD with PWD shortened in terminal title +# Taken mostly verbatim from 'vte.sh' provided with VTE-based shells + +__term_title_urlencode() ( +# This is important to make sure string manipulation is handled +# byte-by-byte. +LC_ALL=C +str="$1" +while [ -n "$str" ]; do + safe="${str%%[!a-zA-Z0-9/:_\.\-\!\'\(\)~]*}" + printf "%s" "$safe" + str="${str#"$safe"}" + if [ -n "$str" ]; then + printf "%%%02X" "'$str" + str="${str#?}" + fi +done +) + +__term_title () { + printf "\033]7;file://%s%s\033\\" "${HOST:-}" "$(__term_title_urlencode "${PWD}")" +} + +__term_title_prompt_command() { + local pwd='~' + [ "$PWD" != "$HOME" ] && pwd=${PWD/#$HOME\//\~\/} + pwd="${pwd//[[:cntrl:]]}" + if [ -n "$BASH_VERSION" ]; then + if command -v hostname &>/dev/null; then + export HOSTNAME="$(hostname)" + export HOST="$HOSTNAME" + fi + fi + printf "\033]0;%s@%s:%s\033\\%s" "${USER}" "${HOST%%.*}" "${pwd}" "$(__term_title)" +} + +case "$TERM" in + xterm*|term_title*) + [ -n "$BASH_VERSION" ] && PROMPT_COMMAND="__term_title_prompt_command;$PROMPT_COMMAND" + [ -n "$ZSH_VERSION" ] && precmd_functions+=(__term_title_prompt_command) + ;; +esac + +# Launch it once to make sure we display the wanted title +__term_title diff --git a/shell/.profile b/shell/.profile index 7de1ce7..0ac5a7b 100644 --- a/shell/.profile +++ b/shell/.profile @@ -45,9 +45,6 @@ if { [ -n "$BASH_VERSION" ] && shopt -q login_shell; } || # Use my preferred pager settings for bat export BAT_PAGER="$PAGER $LESS" - - # Fix terminal title which uses HOSTNAME instead of HOST - export HOSTNAME="$HOST" fi # Use keychain to handle ssh-agent, in interactive shell too diff --git a/zsh/.zshrc b/zsh/.zshrc index 28fe171..e5c6bcb 100755 --- a/zsh/.zshrc +++ b/zsh/.zshrc @@ -1,13 +1,6 @@ # Export our directory to Termite for opening new terminals -if { [[ "$TERM" == xterm-termite ]] || [[ "$TERM" == xterm ]]; } && - [ -r /etc/profile.d/vte.sh ]; then - if [[ "$TERM" == xterm ]]; then # FIXME: hack to make Xterm work - VTE_VERSION="${VTE_VERSION:-3405}" - fi - - . /etc/profile.d/vte.sh - __vte_osc7 - precmd_functions+=(__vte_prompt_command) # FIXME: why isn't it set above ? +if { [[ "$TERM" == xterm-termite ]] || [[ "$TERM" == xterm ]]; }; then + source ~/.scripts/term-title fi # History configuration