Skip to content

Instantly share code, notes, and snippets.

@RaycatWhoDat
Created March 23, 2019 17:08
Show Gist options
  • Save RaycatWhoDat/a44a92f175a68bfabbb2a63d9e5e8b9a to your computer and use it in GitHub Desktop.
Save RaycatWhoDat/a44a92f175a68bfabbb2a63d9e5e8b9a to your computer and use it in GitHub Desktop.
`get-files` implementations.
#!/usr/bin/env rdmd
module getfiles;
import std.array: replace;
import std.file: dirEntries, getcwd, SpanMode;
import std.string: indexOf;
import std.stdio: writeln;
import std.getopt;
enum IndentationLevel {
TWO_SPACES = 2,
FOUR_SPACES = 4
}
string[] ignoredPaths = ["/.git", "/node_modules", "/target"];
string generateIndent(int traversalLevel = 0) pure {
string indentation = "";
foreach (index; 0 .. IndentationLevel.TWO_SPACES * traversalLevel) indentation ~= " ";
return indentation;
}
void printFiles(string directoryPath, int traversalLevel = 0) {
iterateOverFiles:
foreach (entry; dirEntries(directoryPath, SpanMode.shallow)) {
foreach (pathToIgnore; ignoredPaths) {
if (indexOf(entry.name, pathToIgnore) >= 0) break iterateOverFiles;
}
writeln(generateIndent(traversalLevel), entry.name.replace(directoryPath, ""), entry.isDir ? "/" : "");
if (entry.isDir) printFiles(entry.name ~ "/", traversalLevel + 1);
}
}
void main(string[] args) {
string directoryPath = getcwd();
getopt(args, "directory", &directoryPath);
printFiles(directoryPath);
}
// Local Variables:
// compile-command: "./get-files.d --directory .."
// End:
(let ((ignored-paths '("./" "../" ".git/" "node_modules/")))
(labels ((print-file (directory-pathname)
(if (map nil (lambda (ignored-path)
(let ((current-directory-name (car (last (pathname-directory directory-pathname)))))
(print ignored-path)
(print current-directory-name)
(unless (eq current-directory-name :UP)
(or (search ignored-path current-directory-name))
(eql ignored-path current-directory-name)))) ignored-paths) (return))
(format t "~A~%" directory-pathname)
(let ((files (uiop:directory-files directory-pathname)))
(dolist (file files)
(format t "~A~A~:[~;~:*.~A~]~%"
directory-pathname
(pathname-name file)
(pathname-type file))))))
(uiop:collect-sub*directories "../" t t #'print-file)))
my $file_level = 0;
my $max_indent_level = 4;
my @ignored_paths = ".", "..", ".git", "node_modules";
sub generate_indent($file_level) {
my $indentation = "";
for (0..($max_indent_level * ($file_level - 1))) {
$indentation = $indentation ~ ' ';
}
return $indentation;
}
sub list_files($directory = './') {
my @files = dir $directory;
$file_level += 1;
for ([email protected]) {
my $current_filename = @files[$_].basename;
my $should_ignore = False;
for @ignored_paths {
$should_ignore = $current_filename eq $_;
last if $should_ignore;
}
next if $should_ignore;
say generate_indent($file_level) ~ @files[$_].basename;
list_files(@files[$_]) if @files[$_].d;
}
$file_level -= 1;
}
list_files();
# Local Variables:
# compile-command: "perl6 ./get-files.p6"
# End:
#!/bin/sh
# the next line restarts using tclsh \
exec tclsh "$0" ${1+"$@"}
set indentation_level 2
proc generate_indent { traversal_level } {
global indentation_level
set indentation ""
for {set index 0} {$index < [expr $traversal_level * $indentation_level]} {incr index} {
append indentation " "
}
return $indentation
}
proc list_files { directory traversal_level } {
set excluded_paths { ".git" "node_modules" "target" }
if { $directory == "" } { set directory "." }
foreach file [lsort -increasing [glob -nocomplain -directory $directory *]] {
set should_be_excluded 0
foreach path_to_exclude $excluded_paths {
if { [regexp "$path_to_exclude" "$file"] } {
set should_be_excluded 1
break
}
}
if { $should_be_excluded } { break }
puts "[generate_indent $traversal_level][regsub $directory $file {}]"
if { [file isdirectory "$file"] } { list_files "$file" [expr $traversal_level + 1] }
}
}
list_files $argv 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment