settings are in working order

This commit is contained in:
Heli-o 2022-04-19 00:21:06 +02:00
parent 3a4d617933
commit a10a669e22
4 changed files with 157 additions and 37 deletions

View file

@ -12,28 +12,141 @@ using static System.Threading.Thread;
namespace cce { namespace cce {
internal class App { internal class App {
static internal void Settings() { static internal bool Settings() {
// Finish Settings screen, make it interactable // 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 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<string, string> ToDict () {
//Add properties and finish this method
return new Dictionary<string, string>() {
{ "LagWrite", Lgwrite.ToString()},
{ "Common", Common},
{ "Extended", Extended},
{ "Settings", Settings},
};
}
static internal void Update() {
Check();
var settings = JsonConvert.DeserializeObject<Dictionary<string, string>>(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 Path { internal class DefaultSettings {
static internal byte Lgwrite = 1;
static internal string Common = "common.json"; static internal string Common = "common.json";
static internal string Extended = "extended.json"; static internal string Extended = "extended.json";
static internal string Settings = "settings.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<string, byte> ToDict () {
//Add properties and finish this method
return new Dictionary<string, byte> ();
}
static internal void Update() {
var settings = JsonConvert.DeserializeObject<Dictionary<string, byte>>(File.ReadAllText(Path.Settings));
}
}
} }

View file

@ -15,6 +15,7 @@ namespace cce {
[STAThread] [STAThread]
static void Main(string[] args) { static void Main(string[] args) {
byte choice = 255; byte choice = 255;
AppSettings.Update();
while (true) { while (true) {
SetBufferSize(240, 60); SetBufferSize(240, 60);
SetWindowSize(120, 30); SetWindowSize(120, 30);
@ -23,7 +24,7 @@ namespace cce {
Utils.LagWrite("Welcome to the " + title, true); Utils.LagWrite("Welcome to the " + title, true);
//WriteLine("Welcome to the Corpus cypher executable."); //WriteLine("Welcome to the Corpus cypher executable.");
WriteCrypto(); WriteCrypto();
if (choice == 255) choice = Chooser(); choice = Chooser();
CursorVisible = false; CursorVisible = false;
var entry = ""; var entry = "";
if ((new int[] {1,2}).Contains(choice)) { if ((new int[] {1,2}).Contains(choice)) {
@ -33,21 +34,21 @@ namespace cce {
entry = ReadLine(); entry = ReadLine();
CursorVisible = false; CursorVisible = false;
} }
bool skip = false;
if (choice == 1) { Transcoder.Encode(entry); Utils.Clip(Transcoder.result); } if (choice == 1) { Transcoder.Encode(entry); Utils.Clip(Transcoder.result); }
else if (choice == 2) Transcoder.Decode(entry); else if (choice == 2) Transcoder.Decode(entry);
else if (choice == 5) Transcoder.Teacher(); else if (choice == 5) Transcoder.Teacher();
else if (choice == 8) skip = App.Settings();
else if (choice == 9) Transcoder.head_transcribe(); else if (choice == 9) Transcoder.head_transcribe();
else if (choice == 255) Help(); else if (choice == 254) Help();
else if (choice == 0) { Beep(4000, 200); return; } else if (choice == 255) { Beep(4000, 200); return; }
SetCursorPosition(0, WindowHeight - 2); SetCursorPosition(0, WindowHeight - 2);
Utils.LagWrite("\nPress enter to continue, press any other key to exit"); CursorVisible = false; if (!skip) {
var cki = ReadKey(); Utils.LagWrite("\nPress enter to continue, press any other key to exit"); CursorVisible = false;
choice = 255; var cki = ReadKey();
var options = new char[] { '0','1','2','3','4','9'}; if (cki.Key != ConsoleKey.Enter) break;
if (options.Contains(cki.KeyChar)) choice = byte.Parse(cki.KeyChar.ToString()); }
else if (cki.Key != ConsoleKey.Enter) break;
} }
} }
@ -61,7 +62,7 @@ namespace cce {
+"\n[8] Settings" +"\n[8] Settings"
+"\n[9] DEV: Update JSON files using sorted files from python" +"\n[9] DEV: Update JSON files using sorted files from python"
+"\n[?] Help" +"\n[?] Help"
+"\n[0] Exit" +"\n[Backspace] Exit"
+"\n>>> "); +"\n>>> ");
/*Write(@"Options? /*Write(@"Options?
[1] Encode [1] Encode
@ -73,9 +74,10 @@ namespace cce {
[0] Exit [0] Exit
>> ");*/ >> ");*/
CursorVisible = true; CursorVisible = true;
var choice = ReadKey().KeyChar; var choice = ReadKey();
WriteLine(); WriteLine();
switch (choice) { if (choice.Key == ConsoleKey.Backspace) return 255;
switch (choice.KeyChar) {
case '1': case '1':
case '2': case '2':
case '5': case '5':
@ -86,8 +88,8 @@ namespace cce {
case '/': case '/':
case '-': case '-':
case ',': case ',':
if ((new char[] { '?','/','-',','}).Contains(choice)) return 255; if ((new char[] { '?','/','-',','}).Contains(choice.KeyChar)) return 254;
return byte.Parse(choice.ToString()); return byte.Parse(choice.KeyChar.ToString());
default: default:
WriteLine("Not a valid option"); WriteLine("Not a valid option");
break; break;
@ -122,7 +124,7 @@ namespace cce {
CursorLeft = WindowWidth - crypto[i].Length - 2; CursorLeft = WindowWidth - crypto[i].Length - 2;
foreach (char c in crypto[i]) { foreach (char c in crypto[i]) {
Write(c); 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); SetCursorPosition(pos.CursorLeft, pos.CursorTop);

View file

@ -209,8 +209,8 @@ namespace cce {
} }
static private void update() { static private void update() {
uniquire(); uniquire();
File.WriteAllText(App.path["extended"], JsonConvert.SerializeObject(extended_dict)); File.WriteAllText(AppSettings.Extended, JsonConvert.SerializeObject(extended_dict));
File.WriteAllText(App.path["common"], JsonConvert.SerializeObject(common_dict)); File.WriteAllText(AppSettings.Common, JsonConvert.SerializeObject(common_dict));
} }
static private string raw_encode(string entry) { static private string raw_encode(string entry) {
var sentence = entry.ToLower(); var sentence = entry.ToLower();
@ -227,9 +227,9 @@ namespace cce {
static private void init() { static private void init() {
if (common_dict != null && extended_dict != null) return; if (common_dict != null && extended_dict != null) return;
WriteLine("[Loading Dictionary]"); WriteLine("[Loading Dictionary]");
if (File.Exists(App.path["extended"])) extended_dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(File.ReadAllText(App.path["extended"])); if (File.Exists(AppSettings.Extended)) extended_dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(File.ReadAllText(AppSettings.Extended));
else WriteLine("File missing"); else WriteLine("File missing");
if (File.Exists(App.path["common"])) common_dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(File.ReadAllText(App.path["common"])); if (File.Exists(AppSettings.Common)) common_dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(File.ReadAllText(AppSettings.Common));
else WriteLine("File missing"); else WriteLine("File missing");
} }
static private void uniquire() { static private void uniquire() {

View file

@ -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"); 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) { 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<List<string>>(); var phrases = new List<List<string>>();
var lines = input.Split(new char[] { '\n' }); var lines = input.Split(new char[] { '\n' });
for (int i = 0; i < lines.Length; i++) { for (int i = 0; i < lines.Length; i++) {