Created
June 25, 2015 17:22
-
-
Save jimbocoder/890ada56490990597561 to your computer and use it in GitHub Desktop.
php shortpath PS1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
$safeHome = preg_quote(getenv('HOME'), '/'); | |
$path = preg_replace("/^$safeHome(.*)$/", '~$1', getcwd()); | |
$delim = "\xe2\x8b\xaf"; // ⋯ | |
$maxlen=5; | |
$abbreviator = function($component) use ($delim, $maxlen) { | |
if ( strlen($component) > $maxlen ) { | |
return substr($component,0,$maxlen) . $delim; | |
} else { | |
return $component; | |
} | |
}; | |
$pathComponents = explode('/', $path); | |
$lastComponent = array_pop($pathComponents); // I don't want to abbreviate the last path component, even when it's long. | |
print implode ('/', array_merge(array_map( $abbreviator, $pathComponents), (array)$lastComponent)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment