From 936cca97dc46219cca23e91ea9411eec359432cf Mon Sep 17 00:00:00 2001 From: Iain Patterson Date: Tue, 2 Feb 2016 10:59:11 +0000 Subject: [PATCH] Set PROMPT_DIRTRIM dynamically. Unless PROMPT_DIRTRIM is explicitly set to 0, force trimming if the length of the prompt is guesstimated to be more than 2/3 of the available terminal width. --- .profile.d/ps1.bashrc | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/.profile.d/ps1.bashrc b/.profile.d/ps1.bashrc index 0283611..0667d0f 100644 --- a/.profile.d/ps1.bashrc +++ b/.profile.d/ps1.bashrc @@ -201,11 +201,24 @@ function __ps1_colon() { function __ps1_short() { local dirtrim=${PROMPT_DIRTRIM//[^0-9]/} - dirtrim=${dirtrim##0} - if [ -z "$dirtrim" ]; then + + if [ "${dirtrim:0:1}" = "0" ]; then echo "$PWD" return $1 fi + + dirtrim=${dirtrim##0} + if [ -z "$dirtrim" ]; then + local prompt="$USER$HOSTNAME$PWD" + local width=$(((COLUMNS*2)/3)) + if [ ${#prompt} -le ${width:-53} ]; then + echo "$PWD" + return $1 + else + dirtrim=1 + fi + fi + local dirname=${PWD##*/} local basename=${PWD%/$dirname} local reversed= @@ -223,6 +236,7 @@ function __ps1_short() { fi n=$((n+1)) done + echo "$short/$dirname" return $1 } -- 2.20.1