Skip to content

Instantly share code, notes, and snippets.

@Xerkus
Last active March 14, 2018 09:16
Show Gist options
  • Save Xerkus/0a6d144926e01e85dcbf344a15cc3d03 to your computer and use it in GitHub Desktop.
Save Xerkus/0a6d144926e01e85dcbf344a15cc3d03 to your computer and use it in GitHub Desktop.
find . -type f | ../test.php
#!/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);
#!/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);
#!/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);
@settermjd
Copy link

Thanks @Xerkus

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment