winure/qdls/Program.cs

67 lines
1.6 KiB
C#
Raw Normal View History

2024-06-05 10:19:21 -04:00
using System.IO;
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;
// flags
bool showHidden = true;
// 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);
// 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);
// 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
// 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
}