winure/qdls/Program.cs

67 lines
1.8 KiB
C#
Raw Permalink Normal View History

2024-06-10 13:05:12 -04:00
using Qdls;
2024-06-10 09:53:43 -04:00
// collect args and consume flags
List<string> arguments = args.ToList();
bool showHidden = Util.CheckFlag(arguments, "-a", "--all", "-A");
2024-06-10 09:53:43 -04:00
// default to "." if no target args
if(arguments.Count == 0)
arguments = ["."];
// run on targets
2024-06-05 10:19:21 -04:00
foreach(var arg in arguments) {
// don't try to list nonexistent entries or files
if(!Path.Exists(arg)) {
Console.WriteLine($"'{arg}': does not exist");
continue;
}
if(File.Exists(arg)) {
Console.WriteLine($"'{arg}': is file");
continue;
}
2024-06-05 10:19:21 -04:00
// fetch children
2024-06-05 10:19:21 -04:00
var children = Directory.GetFileSystemEntries(arg);
2024-06-10 09:53:43 -04:00
// exclude hidden files where applicable
2024-06-07 15:17:18 -04:00
if(!showHidden) { children = children.Where(f => !Util.IsHidden(f)).ToArray(); }
2024-06-05 10:19:21 -04:00
// state vars
var buffer = "";
2024-06-10 13:05:12 -04:00
var longest = Path.GetFileName(children.OrderByDescending(s => s.Length).First()).Length;
2024-06-05 10:19:21 -04:00
var columns = Console.WindowWidth / (longest + 2);
2024-06-10 13:05:12 -04:00
var multiline = columns < children.Count();
var position = 0;
2024-06-05 10:19:21 -04:00
// print names if more than one target
2024-06-05 10:19:21 -04:00
if(args.Length > 1)
Console.WriteLine($"{arg}:");
// iterate over children; build buffer and output when full
foreach(var child in children.OrderBy(f => (int)(Path.GetFileName(f)[0]))) {
2024-06-05 10:19:21 -04:00
var name = Path.GetFileName(child);
2024-06-10 09:53:43 -04:00
// format hidden files
if(Util.IsHidden(child))
2024-06-07 15:17:18 -04:00
buffer += Format.Hidden;
// format directories
if(Directory.Exists(child))
buffer += Format.Directory;
2024-06-05 10:19:21 -04:00
// if we pass max width, write buffer and clear
2024-06-10 13:05:12 -04:00
if( (++position > columns) && multiline ) {
position = 1;
Console.WriteLine(buffer.TrimEnd());
buffer = "";
2024-06-05 10:19:21 -04:00
}
if(multiline)
2024-06-10 13:34:59 -04:00
buffer += $"{name}{Format.Reset}".PadRight(longest + 6); // magic number 6 is +2 ignoring ansi reset sequence
else
buffer += $"{name}{Format.Reset} ";
2024-06-05 10:19:21 -04:00
}
// print last line
Console.WriteLine(buffer);
2024-06-05 10:19:21 -04:00
}