winure/qdls/Program.cs

64 lines
1.7 KiB
C#
Raw Normal View History

2024-06-07 15:17:18 -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
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");
// 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-05 10:19:21 -04:00
var longest = children.OrderByDescending(s => Path.GetFileName(s).Length).First().Length;
var columns = Console.WindowWidth / (longest + 2);
var multiline = columns < args.Length;
2024-06-05 10:19:21 -04:00
var position = -1;
// 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
2024-06-05 10:19:21 -04:00
foreach(var child in children) {
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
if( multiline && (++position > columns) ) {
2024-06-05 10:19:21 -04:00
position = -1;
Console.WriteLine(buffer);
buffer = "";
2024-06-05 10:19:21 -04:00
}
if(multiline)
buffer += $"{name}{Format.Reset}".PadRight(longest) + " ";
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
}