From 4c919a152c1b9af78ac0cd676f3f45b8e44ba607 Mon Sep 17 00:00:00 2001 From: That_One_Nerd Date: Sat, 1 Apr 2023 15:05:58 -0400 Subject: [PATCH 1/4] Fixed issue #28. Literally a one-liner. --- SrcMod/Shell/Modules/BaseModule.cs | 2 +- SrcMod/Shell/Shell.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/SrcMod/Shell/Modules/BaseModule.cs b/SrcMod/Shell/Modules/BaseModule.cs index 9e18192..653dfbb 100644 --- a/SrcMod/Shell/Modules/BaseModule.cs +++ b/SrcMod/Shell/Modules/BaseModule.cs @@ -194,7 +194,7 @@ public static class BaseModule { List lines = new() { " Timestamp Description"}; int longestName = 0; - for (int i = lines.Count - 1; i >= 0; i--) + for (int i = Program.Shell!.History.Count - 1; i >= 0; i--) { HistoryItem hist = Program.Shell!.History[i]; if (hist.name.Length > longestName) longestName = hist.name.Length; diff --git a/SrcMod/Shell/Shell.cs b/SrcMod/Shell/Shell.cs index a2b7cda..3a90b88 100644 --- a/SrcMod/Shell/Shell.cs +++ b/SrcMod/Shell/Shell.cs @@ -6,7 +6,7 @@ public class Shell { public const string Author = "That_One_Nerd"; public const string Name = "SrcMod"; - public const string Version = "Alpha 0.3.1"; + public const string Version = "Alpha 0.3.2"; public readonly string? ShellDirectory; From e8a5cbc846e6a04e43602913946074c174ba8a76 Mon Sep 17 00:00:00 2001 From: That_One_Nerd Date: Sat, 1 Apr 2023 15:22:38 -0400 Subject: [PATCH 2/4] Oh look, another one liner. Fixed #34 --- SrcMod/Shell/Modules/BaseModule.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SrcMod/Shell/Modules/BaseModule.cs b/SrcMod/Shell/Modules/BaseModule.cs index 653dfbb..374f672 100644 --- a/SrcMod/Shell/Modules/BaseModule.cs +++ b/SrcMod/Shell/Modules/BaseModule.cs @@ -159,7 +159,7 @@ public static class BaseModule FileInfo info = new(f); if (f.Length > longestName) longestName = f.Trim().Length; - int size = Mathf.Ceiling(MathF.Log10(info.Length)); + int size = Mathf.Ceiling(info.Length.ToString().Length); if (longestSize > size) longestSize = size; } From daae786beca340eb68ae10eb90dc0d5be22840f9 Mon Sep 17 00:00:00 2001 From: That_One_Nerd Date: Sat, 1 Apr 2023 20:16:37 -0400 Subject: [PATCH 3/4] Added commands to make new directories and new files. --- SrcMod/Shell/Modules/BaseModule.cs | 154 +++++++++++++++++------------ 1 file changed, 91 insertions(+), 63 deletions(-) diff --git a/SrcMod/Shell/Modules/BaseModule.cs b/SrcMod/Shell/Modules/BaseModule.cs index 374f672..de258b6 100644 --- a/SrcMod/Shell/Modules/BaseModule.cs +++ b/SrcMod/Shell/Modules/BaseModule.cs @@ -100,6 +100,75 @@ public static class BaseModule }); } + [Command("cut")] + [Command("move")] + public static void MoveFile(string source, string destination) + { + string absSource = Path.GetFullPath(source), + absDest = Path.GetFullPath(destination); + + if (File.Exists(source)) + { + if (File.Exists(destination)) throw new($"File already exists at \"{destination}\""); + + string message = $"Moving file \"{source}\" to \"{destination}\"..."; + Write(message); + + File.Move(source, destination); + + Console.CursorLeft = 0; + Console.CursorTop -= (message.Length / Console.BufferWidth) + 1; + Write(new string(' ', message.Length), newLine: false); + } + else if (Directory.Exists(source)) + { + if (Directory.Exists(destination)) throw new($"Directory already exists at \"{destination}\""); + + string message = $"Moving directory \"{source}\" to \"{destination}\"..."; + Write(message); + + Directory.Move(source, destination); + + Console.CursorLeft = 0; + Console.CursorTop -= (message.Length / Console.BufferWidth) + 1; + Write(new string(' ', message.Length), newLine: false); + } + else throw new($"No file or directory found at \"{source}\""); + + DateTime stamp = DateTime.Now; + + Program.Shell!.AddHistory(new() + { + action = delegate + { + if (File.Exists(absDest)) + { + FileInfo info = new(absDest); + if ((info.LastWriteTime - stamp).TotalMilliseconds >= 10) + throw new("The copied file has been modified and probably shouldn't be undone."); + + if (File.Exists(absSource)) throw new($"A file already exists at {absSource} and can't " + + "be overriden."); + + File.Move(absDest, absSource); + } + else if (Directory.Exists(absDest)) + { + DirectoryInfo info = new(absDest); + if ((info.LastWriteTime - stamp).TotalMilliseconds >= 10) + throw new("The copied directory has been modified and probably shouldn't be undone."); + + if (Directory.Exists(absSource)) throw new($"A directory already exists at {absSource} and " + + "can't be overriden."); + + Directory.Move(absDest, absSource); + } + else Write("Looks like the job is already completed Boss.", ConsoleColor.DarkYellow); + }, + name = $"Moved a file or folder from \"{absSource}\" to \"{absDest}\"" + }); + } + [Command("del")] public static void Delete(string path) { @@ -204,73 +273,32 @@ public static class BaseModule DisplayWithPages(lines); } - [Command("cut")] - [Command("move")] - public static void MoveFile(string source, string destination) + [Command("makedir")] + [Command("mkdir")] + public static void CreateDirectory(string name) { - string absSource = Path.GetFullPath(source), - absDest = Path.GetFullPath(destination); + if (Directory.Exists(name)) throw new($"Directory already exists at \"{name}\""); + Directory.CreateDirectory(name); + } - if (File.Exists(source)) + [Command("makefile")] + [Command("mkfile")] + public static void CreateFile(string name, string? text = null) + { + string? dir = Path.GetDirectoryName(name); + if (dir is null) throw new($"Cannot parse file path \"{name}\""); + + if (File.Exists(name)) throw new($"File already exists at \"{name}\""); + if (!string.IsNullOrWhiteSpace(dir) && !Directory.Exists(dir)) Directory.CreateDirectory(dir); + + FileStream stream = File.Create(name); + if (text is not null) { - if (File.Exists(destination)) throw new($"File already exists at \"{destination}\""); - - string message = $"Moving file \"{source}\" to \"{destination}\"..."; - Write(message); - - File.Move(source, destination); - - Console.CursorLeft = 0; - Console.CursorTop -= (message.Length / Console.BufferWidth) + 1; - Write(new string(' ', message.Length), newLine: false); + StreamWriter writer = new(stream); + writer.Write(text); + writer.Close(); } - else if (Directory.Exists(source)) - { - if (Directory.Exists(destination)) throw new($"Directory already exists at \"{destination}\""); - - string message = $"Moving directory \"{source}\" to \"{destination}\"..."; - Write(message); - - Directory.Move(source, destination); - - Console.CursorLeft = 0; - Console.CursorTop -= (message.Length / Console.BufferWidth) + 1; - Write(new string(' ', message.Length), newLine: false); - } - else throw new($"No file or directory found at \"{source}\""); - - DateTime stamp = DateTime.Now; - - Program.Shell!.AddHistory(new() - { - action = delegate - { - if (File.Exists(absDest)) - { - FileInfo info = new(absDest); - if ((info.LastWriteTime - stamp).TotalMilliseconds >= 10) - throw new("The copied file has been modified and probably shouldn't be undone."); - - if (File.Exists(absSource)) throw new($"A file already exists at {absSource} and can't " + - "be overriden."); - - File.Move(absDest, absSource); - } - else if (Directory.Exists(absDest)) - { - DirectoryInfo info = new(absDest); - if ((info.LastWriteTime - stamp).TotalMilliseconds >= 10) - throw new("The copied directory has been modified and probably shouldn't be undone."); - - if (Directory.Exists(absSource)) throw new($"A directory already exists at {absSource} and " + - "can't be overriden."); - - Directory.Move(absDest, absSource); - } - else Write("Looks like the job is already completed Boss.", ConsoleColor.DarkYellow); - }, - name = $"Moved a file or folder from \"{absSource}\" to \"{absDest}\"" - }); + stream.Close(); } [Command("permdel")] From a2b9daa10753666cbc3d7971bbb17b5c80c85e45 Mon Sep 17 00:00:00 2001 From: That_One_Nerd Date: Sat, 1 Apr 2023 20:35:30 -0400 Subject: [PATCH 4/4] Made a command that can be used to run other processes. --- SrcMod/Shell/Modules/BaseModule.cs | 31 +++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/SrcMod/Shell/Modules/BaseModule.cs b/SrcMod/Shell/Modules/BaseModule.cs index de258b6..0436cb5 100644 --- a/SrcMod/Shell/Modules/BaseModule.cs +++ b/SrcMod/Shell/Modules/BaseModule.cs @@ -255,6 +255,13 @@ public static class BaseModule [Command("echo")] public static void Echo(string msg) => Write(msg); + [Command("exit")] + [Command("quit")] + public static void QuitShell(int code = 0) + { + Environment.Exit(code); + } + [Command("explorer")] public static void OpenExplorer(string path = ".") => Process.Start("explorer.exe", Path.GetFullPath(path)); @@ -318,6 +325,23 @@ public static class BaseModule Write(reader.ReadToEnd()); } + [Command("run")] + [CanCancel(false)] + public static void RunProcess(string name, string args = "") + { + Process? run = Process.Start(new ProcessStartInfo() + { + Arguments = args, + CreateNoWindow = false, + ErrorDialog = true, + FileName = name + }); + + if (run is null) throw new($"Error starting the process \"{name}\""); + + run.WaitForExit(); + } + [Command("sleep")] public static void WaitTime(int timeMs) => Thread.Sleep(timeMs); @@ -370,13 +394,6 @@ public static class BaseModule }); } - [Command("exit")] - [Command("quit")] - public static void QuitShell(int code = 0) - { - Environment.Exit(code); - } - [Command("undo")] public static void UndoCommand(int amount = 1) {