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