Fix PS1 length calculation

Based on <http://unix.stackexchange.com/a/105932/3645> and older code.