diff --git a/Adv-FolderSize/Adv-FolderSize.csproj b/Adv-FolderSize/Adv-FolderSize.csproj index 69aa649..2842d31 100644 --- a/Adv-FolderSize/Adv-FolderSize.csproj +++ b/Adv-FolderSize/Adv-FolderSize.csproj @@ -4,6 +4,7 @@ Exe net5.0 Adv_FolderSize + 1.0.1 diff --git a/Adv-FolderSize/Args.cs b/Adv-FolderSize/Args.cs index ffb1fb4..d1fb456 100644 --- a/Adv-FolderSize/Args.cs +++ b/Adv-FolderSize/Args.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; +using System.Collections.Generic; namespace Adv_FolderSize { diff --git a/Adv-FolderSize/CUI.cs b/Adv-FolderSize/CUI.cs new file mode 100644 index 0000000..85a38f7 --- /dev/null +++ b/Adv-FolderSize/CUI.cs @@ -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."); + } + } +} diff --git a/Adv-FolderSize/ColorConsole.cs b/Adv-FolderSize/ColorConsole.cs index 73c5bb3..2a91208 100644 --- a/Adv-FolderSize/ColorConsole.cs +++ b/Adv-FolderSize/ColorConsole.cs @@ -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; } } diff --git a/Adv-FolderSize/FolderAnalysis.cs b/Adv-FolderSize/FolderAnalysis.cs index 5d46ed5..8b3a01b 100644 --- a/Adv-FolderSize/FolderAnalysis.cs +++ b/Adv-FolderSize/FolderAnalysis.cs @@ -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); @@ -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; @@ -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; @@ -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++; @@ -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++; diff --git a/Adv-FolderSize/Program.cs b/Adv-FolderSize/Program.cs index 6303fe6..d6b3a5d 100644 --- a/Adv-FolderSize/Program.cs +++ b/Adv-FolderSize/Program.cs @@ -1,176 +1,21 @@ using System; -using System.IO; -using System.Diagnostics; -using TriggerLib; -using FolderAnalysisFSLib; namespace Adv_FolderSize { class Program { - 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 3 - 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"; - static void Main(string[] args) { - var fa = new FolderAnalysis(""); - var lastdisplayed = 0b0; // 0b0 -> tree 0b1 -> list + var cui = new CUI(); + Console.WriteLine(); - Console.WriteLine("Message: Enter help(h) to get help message"); - ColorConsole.Write("Suggest command: scan(s) [path]", ConsoleColor.DarkGreen); while (true) { - try - { - Console.Write("> "); - var line = Console.ReadLine(); - var result = LineInterpreter(line, out var opt); - - switch (result) - { - case "HELP" or "H": - Console.WriteLine(_helpMsg); - break; - case "SCAN" or "S": - if (!Directory.Exists(opt[0])) - throw new DirectoryNotFoundException(); - - var trigger = new TriggerSource(300, () => - Console.WriteLine("... This scaning operation will take several seconds")); - - fa = new FolderAnalysis(opt[0]); - fa.StartAnalysisAsync().Wait(); - - trigger.Cancel(); - Console.WriteLine("Scaning finished."); - - ColorConsole.Write( - "Suggest command: tree(t) [/m /dp /de /fe] | dirlist(d) [/m /n] | filelist(f) [/m /n]", - ConsoleColor.DarkGreen); - break; - case "TREE" or "T": - fa.PrintDirTree(opt[0], int.Parse(opt[1]), int.Parse(opt[2]), int.Parse(opt[3])); - - lastdisplayed = 0b0; - ColorConsole.Write( - "Suggest command: dirlist(d) [/m /n] | filelist(f) [/m /n] | redirect(r) [index]", - ConsoleColor.DarkGreen); - break; - case "DIRLIST" or "D": - fa.PrintDirList(opt[0], int.Parse(opt[1])); - - lastdisplayed = 0b1; - ColorConsole.Write( - "Suggest command: tree(t) [/m /dp /de /fe] | filelist(f) [/m /n]", ConsoleColor.DarkGreen); - break; - case "FILELIST" or "F": - fa.PrintFileList(opt[0], int.Parse(opt[1])); - - lastdisplayed = 0b1; - ColorConsole.Write("Suggest command: tree(t) [/m /dp /de /fe] | dirlist(d) [/m /n]", - ConsoleColor.DarkGreen); - break; - case "REDIRECT" or "R": - if (!fa.RedirectTo(int.Parse(opt[0]))) - throw new Exception("The specified index does not exist."); - - ColorConsole.Write("Suggest command: tree(t) [/m /dp /de /fe] ", ConsoleColor.DarkGreen); - break; - case "BACK" or "B": - if (!fa.Back()) - throw new Exception("Can not go back anymore."); - - ColorConsole.Write("Suggest command: tree(t) [/m /dp /de /fe] ", ConsoleColor.DarkGreen); - break; - case "OPEN" or "O": - var path = ""; - - if (lastdisplayed == 0b0) - path = fa.GetTreeDirPath(int.Parse(opt[0])); - else - path = fa.GetListElemPath(int.Parse(opt[0])); - - if (path == null) - throw new Exception("The specified index does not exist."); - - OpenExplorer(path); - break; - case "EXIT" or "E": - Environment.Exit(0); - break; - default: - throw new Exception("Invalid command."); - } - } - catch (NotAnalyzedYetException ex) - { - Console.WriteLine($"Error: {ex.Message}"); - ColorConsole.Write("Suggest command: scan(s) [path]", ConsoleColor.DarkGreen); - } - catch (Exception ex) - { - Console.WriteLine($"Error: {ex.Message}"); - } - + Console.Write("> "); + cui.Input(Console.ReadLine()); Console.WriteLine(); } } - - 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 = "3" }, - 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; - } - - 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."); - } } } diff --git a/Adv-FolderSize/Properties/PublishProfiles/FolderProfile.pubxml b/Adv-FolderSize/Properties/PublishProfiles/FolderProfile.pubxml index 548a293..2056962 100644 --- a/Adv-FolderSize/Properties/PublishProfiles/FolderProfile.pubxml +++ b/Adv-FolderSize/Properties/PublishProfiles/FolderProfile.pubxml @@ -10,7 +10,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121. FileSystem net5.0 false - win-x86 + win-x64 True False diff --git a/Adv-FolderSize/StringExtension.cs b/Adv-FolderSize/StringExtension.cs index ac4deba..0dadf87 100644 --- a/Adv-FolderSize/StringExtension.cs +++ b/Adv-FolderSize/StringExtension.cs @@ -1,5 +1,4 @@ -using System; -using System.Linq; +using System.Linq; namespace Adv_FolderSize { diff --git a/FolderAnalysisFSLib/FolderAnalysisBase.fs b/FolderAnalysisFSLib/FolderAnalysisBase.fs index 9eef944..73aaa8d 100644 --- a/FolderAnalysisFSLib/FolderAnalysisBase.fs +++ b/FolderAnalysisFSLib/FolderAnalysisBase.fs @@ -54,4 +54,4 @@ type FolderAnalysisBase(path) = this.GetList(dirList, top, num) member this.GetFileList(top, num) = - this.GetList(fileList, top, num) + this.GetList(fileList, top, num) \ No newline at end of file