Skip to content

Instantly share code, notes, and snippets.

@jimbocoder
Created June 25, 2015 17:22
Show Gist options
  • Save jimbocoder/890ada56490990597561 to your computer and use it in GitHub Desktop.
Save jimbocoder/890ada56490990597561 to your computer and use it in GitHub Desktop.
php shortpath PS1
#!/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