improved formatting and code readability
This commit is contained in:
parent
cb59ed771a
commit
9591a4867c
2 changed files with 59 additions and 11 deletions
|
@ -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
|
||||
)
|
||||
// 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);
|
||||
}
|
||||
|
|
24
qdls/Util.cs
Normal file
24
qdls/Util.cs
Normal file
|
@ -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
|
||||
|
||||
}
|
||||
|
Loading…
Reference in a new issue