Last active
March 14, 2018 09:16
-
-
Save Xerkus/0a6d144926e01e85dcbf344a15cc3d03 to your computer and use it in GitHub Desktop.
find . -type f | ../test.php
This file contains 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 | |
$input = stream_get_contents(fopen("php://stdin", "r")); | |
$files = explode("\n", $input); | |
$treeFiles = []; | |
foreach ($files as $file) { | |
if (empty($file)) { | |
continue; | |
} | |
$parts = explode('/', $file); | |
$filename = array_pop($parts); | |
$parts = array_reverse($parts); | |
$tree = [$filename => $file]; | |
foreach ($parts as $part) { | |
if ($part === '.') { | |
continue; | |
} | |
$tree = [$part => $tree]; | |
} | |
$treeFiles = array_merge_recursive($treeFiles, $tree); | |
} | |
$format = function($tree, $format, $nesting = 0) { | |
ksort($tree, SORT_STRING); | |
$folders = []; | |
$return = ''; | |
foreach ($tree as $name => $value) { | |
if (!is_string($value)) { | |
$folders[$name] = $value; | |
continue; | |
} | |
$return .= sprintf( | |
"%s [%s]%s\n", | |
str_repeat('*', $nesting + 1), | |
$value, | |
pathinfo($value, PATHINFO_FILENAME) | |
); | |
} | |
foreach ($folders as $name => $value) { | |
$return .= sprintf( | |
"%s %s\n", | |
str_repeat('*', $nesting + 1), | |
ucfirst($name) | |
); | |
$return .= $format($tree[$name], $format, $nesting + 1); | |
} | |
return $return; | |
}; | |
echo $format($treeFiles, $format); |
This file contains 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 | |
$input = stream_get_contents(fopen("php://stdin", "r")); | |
$files = explode("\n", $input); | |
$treeFiles = []; | |
foreach ($files as $file) { | |
if (empty($file)) { | |
continue; | |
} | |
$parts = explode('/', $file); | |
$filename = array_pop($parts); | |
$parts = array_reverse($parts); | |
$tree = [$filename => $file]; | |
foreach ($parts as $part) { | |
if ($part === '.') { | |
continue; | |
} | |
$tree = [$part => $tree]; | |
} | |
$treeFiles = array_merge_recursive($treeFiles, $tree); | |
} | |
$format = function($tree, $format, $nesting = 0) { | |
ksort($tree, SORT_STRING); | |
$files = []; | |
$return = ''; | |
foreach ($tree as $name => $value) { | |
if (is_string($value)) { | |
$files[$name] = $value; | |
continue; | |
} | |
$return .= sprintf( | |
"%s %s\n", | |
str_repeat('*', $nesting + 1), | |
ucfirst($name) | |
); | |
$return .= $format($tree[$name], $format, $nesting + 1); | |
} | |
foreach ($files as $name => $file) { | |
$return .= sprintf( | |
"%s [%s]%s\n", | |
str_repeat('*', $nesting + 1), | |
$file, | |
pathinfo($file, PATHINFO_FILENAME) | |
); | |
} | |
return $return; | |
}; | |
echo $format($treeFiles, $format); |
This file contains 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 | |
$input = stream_get_contents(fopen("php://stdin", "r")); | |
$files = explode("\n", $input); | |
$treeFiles = []; | |
foreach ($files as $file) { | |
if (empty($file)) { | |
continue; | |
} | |
$parts = explode('/', $file); | |
$filename = array_pop($parts); | |
$parts = array_reverse($parts); | |
$tree = [$filename => $filename]; | |
foreach ($parts as $part) { | |
if ($part === '.') { | |
continue; | |
} | |
$tree = [$part => $tree]; | |
} | |
$treeFiles = array_merge_recursive($treeFiles, $tree); | |
} | |
$format = function($tree, $format, $nesting = 0) { | |
ksort($tree, SORT_STRING); | |
$files = []; | |
$return = ''; | |
foreach ($tree as $name => $value) { | |
if ($name === $value) { | |
$files[] = $value; | |
continue; | |
} | |
$return .= str_repeat(' ', $nesting) . $name . "\n"; | |
$return .= $format($tree[$name], $format, $nesting + 1); | |
} | |
foreach ($files as $file) { | |
$return .= str_repeat(' ', $nesting) . $file . "\n"; | |
} | |
return $return; | |
}; | |
echo $format($treeFiles, $format); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks @Xerkus