From a10a669e2282b7ea1e51ff1c4265cd39b5d0d0a1 Mon Sep 17 00:00:00 2001 From: Heli-o Date: Tue, 19 Apr 2022 00:21:06 +0200 Subject: [PATCH] settings are in working order --- cce/App.cs | 147 ++++++++++++++++++++++++++++++++++++++++------ cce/Program.cs | 34 ++++++----- cce/Transcoder.cs | 8 +-- cce/Utils.cs | 5 ++ 4 files changed, 157 insertions(+), 37 deletions(-) diff --git a/cce/App.cs b/cce/App.cs index e041c48..f16e203 100644 --- a/cce/App.cs +++ b/cce/App.cs @@ -12,28 +12,141 @@ using static System.Threading.Thread; namespace cce { internal class App { - static internal void Settings() { + static internal bool Settings() { // Finish Settings screen, make it interactable - - + bool saved = false; + byte reset = 0; + while (true) { + Clear(); + AppSettings.Screen(saved, reset); + ConsoleKeyInfo cki; + do { + Write(">>> "); + cki = Console.ReadKey(); WriteLine(); + if ((new char[] { '0', '1', 's' }).Contains(char.ToLower(cki.KeyChar))) break; + if (cki.Key == ConsoleKey.Backspace) { AppSettings.Save(); return true; } + WriteLine("Not a valid option"); + } while (true); + + switch (char.ToLower(cki.KeyChar)) { + case '0': + AppSettings.Reset(out reset); + break; + case '1': + AppSettings.Lgwrite = AppSettings.Lgwrite == 1 ? (byte)0 : (byte)1; + break; + case 's': + AppSettings.Save(); + saved = true; + break; + default: + break; + } + } } } - internal class Path { + internal class AppSettings { + static internal byte Lgwrite = 1; + static internal string Common = "common.json"; + static internal string Extended = "extended.json"; + static internal string Settings = "settings.json"; + static internal (int, int) top_bottom = (0, 0); + static internal void Check() { + if (!File.Exists(Settings)) File.WriteAllText(Settings, JsonConvert.SerializeObject(AppSettings.ToDict())); + } + static internal Dictionary ToDict () { + //Add properties and finish this method + return new Dictionary() { + { "LagWrite", Lgwrite.ToString()}, + { "Common", Common}, + { "Extended", Extended}, + { "Settings", Settings}, + }; + } + static internal void Update() { + Check(); + var settings = JsonConvert.DeserializeObject>(File.ReadAllText(Settings)); + Lgwrite = byte.Parse(settings["LagWrite"]); + Common = settings["Common"]; + Extended = settings["Extended"]; + Settings = settings["Settings"]; + } + static internal void Save() { + File.WriteAllText(Settings, JsonConvert.SerializeObject(AppSettings.ToDict())); + } + + static internal void Screen(bool saved = false, byte reset = 0) { + // │ ─ ┌ ┐ └ ┘ + if (saved) { + BackgroundColor = ConsoleColor.Green; + Write("Saved!"); + ResetColor(); + } + if (reset == 2) { + if (saved) Write(" │ "); + BackgroundColor = ConsoleColor.Blue; + Write("SETTINGS RESET!"); + ResetColor(); + } else if (reset == 1) { + BackgroundColor = ConsoleColor.Red; + Write("RESET ABORTED!"); + ResetColor(); + } + WriteLine(); + top_bottom.Item1 = CursorTop; + WriteLine(@"┌──────────────────────────────────┐ +│ │ +│ ┌──┐ │ +│ [1] Laggy Console Write: │ │ │ +│ └──┘ │ +│ │ +│ [0] Use Default Settings │ +│ │ +│ [S] Save Settings │ +│ │ +└──────────────────────────────────┘ + +[S] Settings will save automatically upon return to menu + +Press [Backspace] to return to main menu"); + top_bottom.Item2 = CursorTop; + if (Lgwrite == 1) BackgroundColor = ConsoleColor.Green; + else BackgroundColor = ConsoleColor.Red; + SetCursorPosition(29, top_bottom.Item1 + 3); + Write(" "); + ResetColor(); + SetCursorPosition(0, top_bottom.Item2); + } + static internal void Reset(out byte reset) { + reset = AreYouSure(); + if (reset == 1) return; + Lgwrite = DefaultSettings.Lgwrite; + Common = DefaultSettings.Common; + Extended = DefaultSettings.Extended; + Settings = DefaultSettings.Settings; + } + static private byte AreYouSure() { + SetCursorPosition(0, top_bottom.Item1); + WriteLine(@" +┌──────────────────────────────────┐ +│ │ +│ Are You Sure │ +│ │ +│ Press Y to confirm │ +│ │ +└──────────────────────────────────┘"); + SetCursorPosition(0, top_bottom.Item2); + var cki = ReadKey(); + WriteLine(); + if (cki.Key == ConsoleKey.Y) return 2; + else return 1; + + } + } + internal class DefaultSettings { + static internal byte Lgwrite = 1; static internal string Common = "common.json"; static internal string Extended = "extended.json"; static internal string Settings = "settings.json"; } - internal class AppSettings { - static internal byte Check() { - if (!File.Exists(Path.Settings)) { File.WriteAllText(Path.Settings, JsonConvert.SerializeObject(AppSettings.ToDict())); return 1; } - else { return 0; } - } - static internal Dictionary ToDict () { - //Add properties and finish this method - return new Dictionary (); - } - static internal void Update() { - var settings = JsonConvert.DeserializeObject>(File.ReadAllText(Path.Settings)); - } - } } diff --git a/cce/Program.cs b/cce/Program.cs index 980d301..ac56bab 100644 --- a/cce/Program.cs +++ b/cce/Program.cs @@ -15,6 +15,7 @@ namespace cce { [STAThread] static void Main(string[] args) { byte choice = 255; + AppSettings.Update(); while (true) { SetBufferSize(240, 60); SetWindowSize(120, 30); @@ -23,7 +24,7 @@ namespace cce { Utils.LagWrite("Welcome to the " + title, true); //WriteLine("Welcome to the Corpus cypher executable."); WriteCrypto(); - if (choice == 255) choice = Chooser(); + choice = Chooser(); CursorVisible = false; var entry = ""; if ((new int[] {1,2}).Contains(choice)) { @@ -33,21 +34,21 @@ namespace cce { entry = ReadLine(); CursorVisible = false; } - + bool skip = false; if (choice == 1) { Transcoder.Encode(entry); Utils.Clip(Transcoder.result); } else if (choice == 2) Transcoder.Decode(entry); else if (choice == 5) Transcoder.Teacher(); + else if (choice == 8) skip = App.Settings(); else if (choice == 9) Transcoder.head_transcribe(); - else if (choice == 255) Help(); - else if (choice == 0) { Beep(4000, 200); return; } + else if (choice == 254) Help(); + else if (choice == 255) { Beep(4000, 200); return; } SetCursorPosition(0, WindowHeight - 2); - Utils.LagWrite("\nPress enter to continue, press any other key to exit"); CursorVisible = false; - var cki = ReadKey(); - choice = 255; - var options = new char[] { '0','1','2','3','4','9'}; - if (options.Contains(cki.KeyChar)) choice = byte.Parse(cki.KeyChar.ToString()); - else if (cki.Key != ConsoleKey.Enter) break; + if (!skip) { + Utils.LagWrite("\nPress enter to continue, press any other key to exit"); CursorVisible = false; + var cki = ReadKey(); + if (cki.Key != ConsoleKey.Enter) break; + } } } @@ -61,7 +62,7 @@ namespace cce { +"\n[8] Settings" +"\n[9] DEV: Update JSON files using sorted files from python" +"\n[?] Help" -+"\n[0] Exit" ++"\n[Backspace] Exit" +"\n>>> "); /*Write(@"Options? [1] Encode @@ -73,9 +74,10 @@ namespace cce { [0] Exit >> ");*/ CursorVisible = true; - var choice = ReadKey().KeyChar; + var choice = ReadKey(); WriteLine(); - switch (choice) { + if (choice.Key == ConsoleKey.Backspace) return 255; + switch (choice.KeyChar) { case '1': case '2': case '5': @@ -86,8 +88,8 @@ namespace cce { case '/': case '-': case ',': - if ((new char[] { '?','/','-',','}).Contains(choice)) return 255; - return byte.Parse(choice.ToString()); + if ((new char[] { '?','/','-',','}).Contains(choice.KeyChar)) return 254; + return byte.Parse(choice.KeyChar.ToString()); default: WriteLine("Not a valid option"); break; @@ -122,7 +124,7 @@ namespace cce { CursorLeft = WindowWidth - crypto[i].Length - 2; foreach (char c in crypto[i]) { Write(c); - if (Utils.rng.NextDouble()>0.8 && c != ' ') Sleep(1); + if (AppSettings.Lgwrite == 1 && Utils.rng.NextDouble()>0.8 && c != ' ') Sleep(1); } } SetCursorPosition(pos.CursorLeft, pos.CursorTop); diff --git a/cce/Transcoder.cs b/cce/Transcoder.cs index 47a9606..c5e6b62 100644 --- a/cce/Transcoder.cs +++ b/cce/Transcoder.cs @@ -209,8 +209,8 @@ namespace cce { } static private void update() { uniquire(); - File.WriteAllText(App.path["extended"], JsonConvert.SerializeObject(extended_dict)); - File.WriteAllText(App.path["common"], JsonConvert.SerializeObject(common_dict)); + File.WriteAllText(AppSettings.Extended, JsonConvert.SerializeObject(extended_dict)); + File.WriteAllText(AppSettings.Common, JsonConvert.SerializeObject(common_dict)); } static private string raw_encode(string entry) { var sentence = entry.ToLower(); @@ -227,9 +227,9 @@ namespace cce { static private void init() { if (common_dict != null && extended_dict != null) return; WriteLine("[Loading Dictionary]"); - if (File.Exists(App.path["extended"])) extended_dict = JsonConvert.DeserializeObject>(File.ReadAllText(App.path["extended"])); + if (File.Exists(AppSettings.Extended)) extended_dict = JsonConvert.DeserializeObject>(File.ReadAllText(AppSettings.Extended)); else WriteLine("File missing"); - if (File.Exists(App.path["common"])) common_dict = JsonConvert.DeserializeObject>(File.ReadAllText(App.path["common"])); + if (File.Exists(AppSettings.Common)) common_dict = JsonConvert.DeserializeObject>(File.ReadAllText(AppSettings.Common)); else WriteLine("File missing"); } static private void uniquire() { diff --git a/cce/Utils.cs b/cce/Utils.cs index 06869e4..f5985d4 100644 --- a/cce/Utils.cs +++ b/cce/Utils.cs @@ -28,6 +28,11 @@ namespace cce { if (Char.ToLower(cki) == 'y') { Clipboard.SetText(clip); WriteLine("Writing to Clipboard: Done!"); } else WriteLine("Writing to Clipboard: Canceled by User"); } internal static void LagWrite(string input, bool newline = false) { + if (AppSettings.Lgwrite == 0) { + if (newline) WriteLine(input); + else Write(input); + return; + } var phrases = new List>(); var lines = input.Split(new char[] { '\n' }); for (int i = 0; i < lines.Length; i++) {