diff --git a/qdls/Program.cs b/qdls/Program.cs index 71cbabc..ef7d0c7 100644 --- a/qdls/Program.cs +++ b/qdls/Program.cs @@ -1,42 +1,66 @@ 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); - var line = ""; + // 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); - if( - name.StartsWith('.') || - ( File.GetAttributes(child) & FileAttributes.Hidden ) == FileAttributes.Hidden - ) - continue; + // skip or format hidden files + if(Util.IsHidden(child)) + if(showHidden) + buffer += Format.Hidden; + else + continue; - if(++position >= columns) { + // 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(line); - line = ""; + Console.WriteLine(buffer); + buffer = ""; } - line += name + " "; + if(multiline) + buffer += $"{name}{Format.Reset}".PadRight(longest) + " "; + else + buffer += $"{name}{Format.Reset} "; } - Console.WriteLine(line); + // print last line + Console.WriteLine(buffer); } diff --git a/qdls/Util.cs b/qdls/Util.cs new file mode 100644 index 0000000..6b3200a --- /dev/null +++ b/qdls/Util.cs @@ -0,0 +1,24 @@ +namespace Qdls; + +using System.IO; + +public static class Util { + + public static bool IsHidden(string path) { + var name = Path.GetFileName(path); + return + name.StartsWith('.') || + ( File.GetAttributes(path) & FileAttributes.Hidden ) == FileAttributes.Hidden; + } + +} + +public static class Format { + + public const string Reset = "\x1b[0m"; // reset + public const string Hidden = "\x1b[2;3m"; // faint + italic + public const string Executable = "\x1b[1;32m"; // bold + green + public const string Directory = "\x1b[1;34m"; // bold + blue + +} +