2024-06-05 10:19:21 -04:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
2024-06-07 14:11:14 -04:00
|
|
|
|
using Qdls;
|
|
|
|
|
|
|
|
|
|
// collect args; no args becomes working directory
|
2024-06-05 10:19:21 -04:00
|
|
|
|
string[] arguments;
|
|
|
|
|
if(args.Length == 0)
|
|
|
|
|
arguments = ["."];
|
|
|
|
|
else
|
|
|
|
|
arguments = args;
|
2024-06-07 14:11:14 -04:00
|
|
|
|
|
|
|
|
|
// flags
|
|
|
|
|
bool showHidden = true;
|
|
|
|
|
|
|
|
|
|
// run on targets
|
2024-06-05 10:19:21 -04:00
|
|
|
|
foreach(var arg in arguments) {
|
2024-06-07 14:11:14 -04:00
|
|
|
|
// don't try to list nonexistent entries or files
|
2024-06-05 10:19:21 -04:00
|
|
|
|
if(!Path.Exists(arg))
|
|
|
|
|
Console.WriteLine($"'{arg}: does not exist");
|
|
|
|
|
if(File.Exists(arg))
|
|
|
|
|
Console.WriteLine($"'{arg}: is file");
|
|
|
|
|
|
2024-06-07 14:11:14 -04:00
|
|
|
|
// fetch children
|
2024-06-05 10:19:21 -04:00
|
|
|
|
var children = Directory.GetFileSystemEntries(arg);
|
|
|
|
|
|
2024-06-07 14:11:14 -04:00
|
|
|
|
// state vars
|
|
|
|
|
var buffer = "";
|
2024-06-05 10:19:21 -04:00
|
|
|
|
var longest = children.OrderByDescending(s => Path.GetFileName(s).Length).First().Length;
|
|
|
|
|
var columns = Console.WindowWidth / (longest + 2);
|
2024-06-07 14:11:14 -04:00
|
|
|
|
var multiline = columns < args.Length;
|
2024-06-05 10:19:21 -04:00
|
|
|
|
var position = -1;
|
|
|
|
|
|
2024-06-07 14:11:14 -04:00
|
|
|
|
// print names if more than one target
|
2024-06-05 10:19:21 -04:00
|
|
|
|
if(args.Length > 1)
|
|
|
|
|
Console.WriteLine($"{arg}:");
|
2024-06-07 14:11:14 -04:00
|
|
|
|
// iterate over children; build buffer and output when full
|
2024-06-05 10:19:21 -04:00
|
|
|
|
foreach(var child in children) {
|
|
|
|
|
var name = Path.GetFileName(child);
|
|
|
|
|
|
2024-06-07 14:11:14 -04:00
|
|
|
|
// skip or format hidden files
|
|
|
|
|
if(Util.IsHidden(child))
|
|
|
|
|
if(showHidden)
|
|
|
|
|
buffer += Format.Hidden;
|
|
|
|
|
else
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
// format directories
|
|
|
|
|
if(Directory.Exists(child))
|
|
|
|
|
buffer += Format.Directory;
|
2024-06-05 10:19:21 -04:00
|
|
|
|
|
2024-06-07 14:11:14 -04:00
|
|
|
|
// if we pass max width, write buffer and clear
|
|
|
|
|
if( multiline && (++position > columns) ) {
|
2024-06-05 10:19:21 -04:00
|
|
|
|
position = -1;
|
2024-06-07 14:11:14 -04:00
|
|
|
|
Console.WriteLine(buffer);
|
|
|
|
|
buffer = "";
|
2024-06-05 10:19:21 -04:00
|
|
|
|
}
|
2024-06-07 14:11:14 -04:00
|
|
|
|
if(multiline)
|
|
|
|
|
buffer += $"{name}{Format.Reset}".PadRight(longest) + " ";
|
|
|
|
|
else
|
|
|
|
|
buffer += $"{name}{Format.Reset} ";
|
2024-06-05 10:19:21 -04:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-07 14:11:14 -04:00
|
|
|
|
// print last line
|
|
|
|
|
Console.WriteLine(buffer);
|
2024-06-05 10:19:21 -04:00
|
|
|
|
}
|