Skip to content

Commit

Permalink
Optimize CUI
Browse files Browse the repository at this point in the history
  • Loading branch information
BearOffice committed Feb 23, 2021
1 parent 75d80d2 commit bcba9bc
Show file tree
Hide file tree
Showing 9 changed files with 240 additions and 186 deletions.
1 change: 1 addition & 0 deletions Adv-FolderSize/Adv-FolderSize.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<RootNamespace>Adv_FolderSize</RootNamespace>
<Version>1.0.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
4 changes: 1 addition & 3 deletions Adv-FolderSize/Args.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.Generic;

namespace Adv_FolderSize
{
Expand Down
203 changes: 203 additions & 0 deletions Adv-FolderSize/CUI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
using System;
using System.Diagnostics;
using System.IO;
using FolderAnalysisFSLib;
using TriggerLib;

namespace Adv_FolderSize
{
public class CUI
{
private FolderAnalysis _fA = new FolderAnalysis("");
private byte _lastDisplayed = 0b0; // 0b0 -> tree 0b1 -> list

private const string _helpMsg =
@"Enter help(h) to get help message
Enter scan(s) [path] to scan all directories and files below the path specified
Enter tree(t) to print the scan\'s result
Option: Measure /m[auto|b|kb|mb|gb] Default is auto
Dir depth limit /dp[number] Default is 2
Dir expand limit /de[number] Default is 3
File expand limit /fe[number] Default is 3
Enter dirlist(d) to print the directories\' paths descending by size
Option: Measure /m[auto|b|kb|mb|gb] Default is auto
Number to display /n[number] Default is 10
Enter filelist(f) to print the files\' paths descending by size
Option: Measure /m[auto|b|kb|mb|gb] Default is auto
Number to display /n[number] Default is 10
Enter redirect(r) [index] to move into the specified tree
Enter back(b) to back to the previous tree
Enter open(o) [index] to open the folder or the file specified
Enter exit(e) to exit";

private const string _cmdHelp = "help(h)";
private const string _cmdScan = "scan(s) [path]";
private const string _cmdTree = "tree(t) [/m /dp /de /fe]";
private const string _cmdDirL = "dirlist(d)[/ m / n]";
private const string _cmdFileL = "filelist(f) [/m /n]";
private const string _cmdRed = "redirect(r) [index]";
private const string _cmdBack = "back(b)";
private const string _cmdOpen = "open(o)";

public CUI()
{
Console.WriteLine("Message: Enter help(h) to get help message");
WriteSuggest("scan(s) [path]");
}

public void Input(string line)
{
var result = LineInterpreter(line, out var opt);

try
{
CmdSelect(result, opt);
}
catch (NotAnalyzedYetException ex)
{
Console.WriteLine($"Error: {ex.Message}");
WriteSuggest(_cmdScan);
}
catch (IndexOutOfRangeException ex)
{
Console.WriteLine($"Error: {ex.Message}");
WriteSuggest(_cmdTree, _cmdDirL, _cmdFileL);
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
WriteSuggest(_cmdHelp);
}
}

private void CmdSelect(string cmd, string[] opt)
{
switch (cmd)
{
case "HELP" or "H":
Console.WriteLine(_helpMsg);
break;
case "SCAN" or "S":
if (!Directory.Exists(opt[0]))
throw new DirectoryNotFoundException();

var sw = new Stopwatch();
sw.Start();
var trigger = new TriggerSource(300, () =>
Console.WriteLine("... This scaning operation will take several seconds."));

_fA = new FolderAnalysis(opt[0]);
_fA.StartAnalysisAsync().Wait();

sw.Stop();
trigger.Cancel();
Console.WriteLine($"Scaning finished. Time cost: {Math.Round(sw.Elapsed.TotalSeconds, 2)}s.");

WriteSuggest(_cmdTree, _cmdDirL, _cmdFileL);
break;
case "TREE" or "T":
_fA.PrintDirTree(opt[0], int.Parse(opt[1]), int.Parse(opt[2]), int.Parse(opt[3]));

_lastDisplayed = 0b0;
WriteSuggest(_cmdDirL, _cmdFileL, _cmdRed);
break;
case "DIRLIST" or "D":
_fA.PrintDirList(opt[0], int.Parse(opt[1]));

_lastDisplayed = 0b1;
WriteSuggest(_cmdTree, _cmdFileL, _cmdOpen);
break;
case "FILELIST" or "F":
_fA.PrintFileList(opt[0], int.Parse(opt[1]));

_lastDisplayed = 0b1;
WriteSuggest(_cmdTree, _cmdDirL, _cmdOpen);
break;
case "REDIRECT" or "R":
if (!_fA.RedirectTo(int.Parse(opt[0])))
throw new IndexOutOfRangeException("The specified index does not exist.");

Console.WriteLine($"Redirected to [{opt[0]}].");
WriteSuggest(_cmdTree, _cmdBack);
break;
case "BACK" or "B":
if (!_fA.Back())
throw new IndexOutOfRangeException("Can not go back anymore.");

Console.WriteLine($"Backed to the previous directory.");
WriteSuggest(_cmdTree, _cmdRed);
break;
case "OPEN" or "O":
string path;
if (_lastDisplayed == 0b0)
path = _fA.GetTreeDirPath(int.Parse(opt[0]));
else
path = _fA.GetListElemPath(int.Parse(opt[0]));

if (path == null)
throw new IndexOutOfRangeException("The specified index does not exist.");

OpenExplorer(path);
break;
case "EXIT" or "E":
Environment.Exit(0);
break;
default:
throw new Exception("Invalid command.");
}
}

private static void WriteSuggest(string sug, params string[] sugs)
{
var middlesym = " | ";

string suggests;
if (sugs.Length == 0)
suggests = sug;
else
suggests = sug + middlesym + string.Join(middlesym, sugs);

ColorConsole.WriteLine($"Suggest command: {suggests}", ConsoleColor.DarkGreen);
}

private static string LineInterpreter(string line, out string[] options)
{
var splited = line.Trim().Split(' ');
var cmd = splited[0].ToUpper();
var args = string.Join(' ', splited[1..]);

options = cmd switch
{
"HELP" or "H" or "BACK" or "B" or "EXIT" or "E" => Args.Interprete(new[] { new ArgSet() }, args),
"SCAN" or "S" => Args.Interprete(new[] { new ArgSet { Default = null } }, args, remainder: true),
"TREE" or "T" => Args.Interprete(new[] {
new ArgSet { Key = "/M" ,Default = "AUTO" },
new ArgSet { Key="/DP", Default = "2" },
new ArgSet { Key ="/DE", Default = "3" },
new ArgSet { Key="/FE", Default = "3" } }, args),
"DIRLIST" or "D" => Args.Interprete(new[] {
new ArgSet { Key = "/M", Default = "AUTO" },
new ArgSet { Key = "/N", Default = "10" } }, args),
"FILELIST" or "F" => Args.Interprete(new[] {
new ArgSet { Key = "/M", Default = "AUTO" },
new ArgSet { Key = "/N", Default = "10" } }, args),
"REDIRECT" or "R" => Args.Interprete(new[] { new ArgSet { Default = null } }, args),
"OPEN" or "O" => Args.Interprete(new[] { new ArgSet { Default = null } }, args),
_ => null,
};
return options != null ? cmd : null;
}

private static void OpenExplorer(string path)
{
var platform = Environment.OSVersion.Platform;

if (platform == PlatformID.Win32NT)
Process.Start("explorer.exe", "/select," + path);
else if (platform == PlatformID.Unix)
Process.Start("open", $"-R \"{path}\"");
else
throw new Exception("Unknown operating system.");
}
}
}
15 changes: 10 additions & 5 deletions Adv-FolderSize/ColorConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ namespace Adv_FolderSize
{
public static class ColorConsole
{
public static void Write(string contents, ConsoleColor color, bool asline = true)
public static void Write(string contents, ConsoleColor color)
{
var curcolor = Console.ForegroundColor;
Console.ForegroundColor = color;
if (asline)
Console.WriteLine(contents);
else
Console.Write(contents);
Console.Write(contents);
Console.ForegroundColor = curcolor;
}

public static void WriteLine(string contents, ConsoleColor color)
{
var curcolor = Console.ForegroundColor;
Console.ForegroundColor = color;
Console.WriteLine(contents);
Console.ForegroundColor = curcolor;
}
}
Expand Down
33 changes: 18 additions & 15 deletions Adv-FolderSize/FolderAnalysis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private static string SizeBar(long stand, long size, int length)
else
return "";
}


private static string DirAbbrevName(string path)
=> Path.DirectorySeparatorChar + Path.GetFileName(path);
Expand Down Expand Up @@ -90,10 +90,10 @@ public void PrintDirTree(string measure, int depthLimt, int dirExpLimt, int file
case "D":
ColorConsole.Write(
$"{SizeBar(dirsizestand, size, 16),16} {ByteMeasure.byteToString(measure, size),10} "
, ConsoleColor.Cyan, asline: false);
, ConsoleColor.Cyan);
Console.Write($"{diridx.Repeat(depth)}{sym}");

var print = "";
string print;
if (idxcount == -1)
{
print = name;
Expand All @@ -102,26 +102,27 @@ public void PrintDirTree(string measure, int depthLimt, int dirExpLimt, int file
else if (depth == 1)
{
_selectableTree.Add(name);
print = $"[{idxcount}] {DirAbbrevName(name)}";
ColorConsole.Write($"{idxcount} ", ConsoleColor.DarkYellow);
print = DirAbbrevName(name);
idxcount++;
}
else
{
print = DirAbbrevName(name);
}
ColorConsole.Write(print, ConsoleColor.Cyan);
ColorConsole.WriteLine(print, ConsoleColor.Cyan);
break;
case "FH":
Console.Write($"{"",16} {"",-10} {tabidx.Repeat(depth)}{sym}");
ColorConsole.Write($"... {name} files are hided", ConsoleColor.Yellow);
ColorConsole.WriteLine($"... {name} files are hided", ConsoleColor.Yellow);
break;
case "DH":
Console.Write($"{"",16} {"",-10} {tabidx.Repeat(depth + 1)}{sym}");
ColorConsole.Write($"... {name} directories are hided", ConsoleColor.Yellow);
ColorConsole.WriteLine($"... {name} directories are hided", ConsoleColor.Yellow);
break;
case "DF":
Console.Write($"{"",16} {"",-10} {tabidx.Repeat(depth + 1)}{sym}");
ColorConsole.Write($"... {name} directories above this directory are folded", ConsoleColor.Yellow);
ColorConsole.WriteLine($"... {name} directories above this directory are folded", ConsoleColor.Yellow);
break;
default:
break;
Expand All @@ -141,10 +142,11 @@ public void PrintDirList(string measure = "AUTO", int num = 10)
{
var name = item.Item1;
var size = item.Item2;
ColorConsole.Write(
$"{SizeBar(dirsizestand, size, 16),16} {ByteMeasure.byteToString(measure, size),10} " +
$"[{idxcount}] {name}"
, ConsoleColor.Cyan);

ColorConsole.Write($"{SizeBar(dirsizestand, size, 16),16} {ByteMeasure.byteToString(measure, size),10} ",
ConsoleColor.Cyan);
ColorConsole.Write($"[{idxcount}] ", ConsoleColor.DarkYellow);
ColorConsole.WriteLine(name, ConsoleColor.Cyan);

_selectableList.Add(name);
idxcount++;
Expand All @@ -163,9 +165,10 @@ public void PrintFileList(string measure = "AUTO", int num = 10)
{
var name = item.Item1;
var size = item.Item2;
Console.WriteLine(
$"{SizeBar(filesizestand, size, 16),16} {ByteMeasure.byteToString(measure, size),10} " +
$"[{idxcount}] {name}");

Console.Write($"{SizeBar(filesizestand, size, 16),16} {ByteMeasure.byteToString(measure, size),10} ");
ColorConsole.Write($"[{idxcount}] ", ConsoleColor.DarkYellow);
Console.WriteLine(name);

_selectableList.Add(name);
idxcount++;
Expand Down
Loading

0 comments on commit bcba9bc

Please sign in to comment.