rice

rei with the taste
git clone https://s.sonu.ch/~rei/rice.git
Log | Files | Refs | README

spwd (1325B)


      1 #!/usr/bin/awk -f
      2 # spwd: pure AWK path shortener for PS1
      3 # Shortens a path by keeping only the first character of each directory, or the
      4 # two firsts if it starts with a dot.
      5 
      6 # Usage:
      7 # export PS1="\u:\$(awk -f /where/is/spwd -- "\${PWD}")> "
      8 
      9 # Runtime example:
     10 # charlene:/u/s/d/mg> echo $PWD
     11 # /usr/share/doc/mg
     12 # charlene:/u/s/d/mg> cd /home/charlene/.vim/colors/
     13 # charlene:~/.v/colors>
     14 
     15 # Should be POSIX-compliant, tested with OpenBSD's awk, mawk and gawk.
     16 # May break if you have funny chars in $HOME
     17 
     18 BEGIN {
     19     # dealing with directories with spaces
     20     for (elem = 1; elem < length(ARGV); elem++) {
     21         pwd = sprintf("%s %s", pwd, ARGV[elem])
     22     }
     23     sub(/^ /, "", pwd)
     24 
     25     home = ENVIRON["HOME"]
     26     # Regex-ify $HOME
     27     gsub(/\//, "\\/", home)
     28     sub("^"home, "~", pwd)
     29     if (pwd == "~") {
     30         printf(pwd)
     31         exit
     32     }
     33 
     34     split(pwd, pelems, /\//)
     35     # we don't need the useless pelems[0], and the shortened "basename $PWD"
     36     # (last one). Also the pelems[1] is empty because pwd starts with the
     37     # seperator, so it will always prints '/'.
     38     for (i = 1; i < length(pelems); i++) {
     39         # two characters for dotfiles/dirs
     40         nchar = substr(pelems[i], 1, 1) == "." ? 2 : 1
     41         printf("%s/", substr(pelems[i], 1, nchar))
     42     }
     43     printf("%s", pelems[length(pelems)])
     44 }