winure/qdls/Program.cs

66 lines
1.6 KiB
C#

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