Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Correction to original post?' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Correction to original post?
Authored by: clh on Dec 31, '01 02:26:54AM

For the solution in the original post to work, I believe you need to set PS1 *inside* the prompt_command function.

The final lines of the function should read

...
if [ $(echo -n $PWD | wc -c | tr -d " ") -gt $pwd_length ]
then newPWD="$(echo -n $PWD | sed -e "s/.*(.{$pwd_length})/1/")"
else newPWD="$(echo -n $PWD)"
fi
PS1="[u@h:$newPWD]$ " ## <-- add this line to recompute PS1
}

Another solution, which doesn't require burying the assignment of PS1 inside a function, is to modify prompt_command as follows:

...
if [ $(echo -n $PWD | wc -c | tr -d " ") -gt $pwd_length ]
then newPWD="$(echo -n $PWD | sed -e "s/.*(.{$pwd_length})/1/")"
else newPWD="$(echo -n $PWD)"
fi
echo $newPWD ## <-- "return" the newly-computed CWD
}

Then set PS1 as follows

PS1="u@h:$(prompt_command)$ " ## <-- invokes prompt_command and inserts CWD

You don't need to use the PROMPT_COMMAND variable with this method.

Well, I'm new to this forum and I haven't learned how achieve WYSIWYGedness, so I'm sure this will be missing scads of backslashes when you read it on the web. But anyway, I *think* the original post was not quite correct.

semicolon hyphen close-parenthesis



[ Reply to This | # ]
Saving PS1, don't change it :)
Authored by: cheako on Sep 15, '03 05:43:33AM

This is what I have...

echo $PROMPT_COMMAND | grep -qe 'How many characters' || {
PROMPT_COMMAND=${PROMPT_COMMAND:+$PROMPT_COMMAND';
'}' {
# How many characters of the $PWD should be kept;
if [ "$lastPWD" != "$PWD" ];
then lastPWD="$PWD";
tmpPWD="$(echo -n "$PWD" |
sed -e '\''s:^'\''"$HOME"'\'':~:'\'')";
pwd_length=41;
pwd_prefixlength=11;
if [ $(echo -n "$tmpPWD" | wc -c) -gt $pwd_length ];
then newPWD="$(echo -n "$tmpPWD" |
sed -e '\''s:\(.\{'\''$pwd_prefixlength'\''\}\).*\(.\{'\''$((
$pwd_length - $pwd_prefixlength - 2 ))'\''\}\):\1..\2:'\'')";
else newPWD="$tmpPWD";
fi;
unset pwd_length pwd_prefixlength;
fi; # tmpPWD is not newPWD;
}'; # Out of singel quoted string hell.
# Export is not needed, but PS1 is and this will be used there.
export newPWD="$(echo -n "$PWD" | sed -e 's:^'"$HOME"':~:')"
# Force newPWD update.
lastPWD=..Yo..
}; # grep for 'How many characters' in PROMPT_COMMAND;

# All of this should allready be set to a good default, make them better.
[ "$PRO_UID" ] || PRO_UID=$(id -u)
if [ "$PRO_UID" -eq 0 ]
then PS1='\[\017\]\h:\[$newPWD\]\$ '
else PS1='\[\017\]\u@\h:\[$newPWD\]\$ '
fi
unset PRO_UID

# The \017 fixes the \016 bug :) echo -e '\016' for deatails :P



[ Reply to This | # ]