Code challenge: Bash prompt path shortener

前端 未结 5 1394
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-30 09:27

I implemented a prompt path shortener for bash to be included in the PS1 environment variable, which shortens the working directory into something more compact but still descrip

5条回答
  •  遇见更好的自我
    2021-01-30 10:13

    This one is 20 or so characters shorter than my other answer:

    _dir_chomp () {
        local p=${1/#$HOME/\~} b s
        s=${#p}
        while [[ $p != "${p//\/}" ]]&&(($s>$2))
        do
            p=${p#/}
            [[ $p =~ \.?. ]]
            b=$b/${BASH_REMATCH[0]}
            p=${p#*/}
            ((s=${#b}+${#p}))
        done
        echo ${b/\/~/\~}${b+/}$p
    }
    

提交回复
热议问题