From 6388b82d4db59d2c47c25aab388b85e028f04456 Mon Sep 17 00:00:00 2001 From: ImNicolasTheDev Date: Sun, 7 May 2023 16:28:49 +0200 Subject: [PATCH] Added namespace System to ConsoleHelper --- Sources/Console/ConsoleHelper.cs | 194 ++++++++++++++++--------------- 1 file changed, 98 insertions(+), 96 deletions(-) diff --git a/Sources/Console/ConsoleHelper.cs b/Sources/Console/ConsoleHelper.cs index 3d53e35..c93460e 100644 --- a/Sources/Console/ConsoleHelper.cs +++ b/Sources/Console/ConsoleHelper.cs @@ -7,122 +7,124 @@ using System.Threading.Tasks; using static System.Net.Mime.MediaTypeNames; -public class ConsoleHelper -{ - public static void displayTitle(string title, bool clear) +namespace System { + public class ConsoleHelper { - if (clear) + public static void displayTitle(string title, bool clear) { - Console.Clear(); + if (clear) + { + System.Console.Clear(); + } + Console.ForegroundColor = ConsoleColor.Green; //Ecris le titre suivant en vert + Console.WriteLine(); + Console.WriteLine($"-==============- {title} -==============-"); + Console.WriteLine(); + Console.ResetColor(); //Reset la couleur du texte par défaut (à blanc) } - Console.ForegroundColor = ConsoleColor.Green; //Ecris le titre suivant en vert - Console.WriteLine(); - Console.WriteLine($"-==============- {title} -==============-"); - Console.WriteLine(); - Console.ResetColor(); //Reset la couleur du texte par défaut (à blanc) - } - public static string ReadPassword() - { - string psswrd = ""; - ConsoleKeyInfo info = Console.ReadKey(true); - while (info.Key != ConsoleKey.Enter) + public static string ReadPassword() { - if (info.Key != ConsoleKey.Backspace) + string psswrd = ""; + ConsoleKeyInfo info = System.Console.ReadKey(true); + while (info.Key != ConsoleKey.Enter) { - Console.Write("*"); - psswrd += info.KeyChar; - } - else if (info.Key == ConsoleKey.Backspace) - { - if (!string.IsNullOrEmpty(psswrd)) + if (info.Key != ConsoleKey.Backspace) { - // Supprime un élément de la liste de char de psswrd - psswrd = psswrd.Substring(0, psswrd.Length - 1); - // Récupère la position du curseur - int pos = Console.CursorLeft; - // Déplace le curseur d'un à gauche - Console.SetCursorPosition(pos - 1, Console.CursorTop); - // Remplace par un espace dans la console - Console.Write(" "); - // Déplace le curseur d'une position à gauche encore - Console.SetCursorPosition(pos - 1, Console.CursorTop); + System.Console.Write("*"); + psswrd += info.KeyChar; } + else if (info.Key == ConsoleKey.Backspace) + { + if (!string.IsNullOrEmpty(psswrd)) + { + // Supprime un élément de la liste de char de psswrd + psswrd = psswrd.Substring(0, psswrd.Length - 1); + // Récupère la position du curseur + int pos = System.Console.CursorLeft; + // Déplace le curseur d'un à gauche + System.Console.SetCursorPosition(pos - 1, System.Console.CursorTop); + // Remplace par un espace dans la console + System.Console.Write(" "); + // Déplace le curseur d'une position à gauche encore + System.Console.SetCursorPosition(pos - 1, System.Console.CursorTop); + } + } + info = System.Console.ReadKey(true); } - info = Console.ReadKey(true); + // Ajoute un alinéa parce que l'utlisateur a validé + System.Console.WriteLine(); + return psswrd; } - // Ajoute un alinéa parce que l'utlisateur a validé - Console.WriteLine(); - return psswrd; - } - public static int MultipleChoice(string title, bool canCancel, params string[] options) - { - displayTitle(title, true); - // Uint = unsigned int -> pour pas que le décalage soit négatif et entraine une exception - const uint startX = 11; // Décalage à partir de la gauche - const uint startY = 4; // Décalage à partir du haut - const int optionsPerLine = 1; - const int spacingPerLine = 50; - int currentSelection = 0; + public static int MultipleChoice(string title, bool canCancel, params string[] options) + { + displayTitle(title, true); + // Uint = unsigned int -> pour pas que le décalage soit négatif et entraine une exception + const uint startX = 11; // Décalage à partir de la gauche + const uint startY = 4; // Décalage à partir du haut + const int optionsPerLine = 1; + const int spacingPerLine = 50; + int currentSelection = 0; - ConsoleKey key; + ConsoleKey key; - Console.CursorVisible = false; + System.Console.CursorVisible = false; - do - { - - for (int i = 0; i < options.Length; i++) + do { - Console.SetCursorPosition((int)(startX + (i % optionsPerLine) * spacingPerLine), (int)(startY + i / optionsPerLine)); + + for (int i = 0; i < options.Length; i++) + { + System.Console.SetCursorPosition((int)(startX + (i % optionsPerLine) * spacingPerLine), (int)(startY + i / optionsPerLine)); - if (i == currentSelection) - Console.ForegroundColor = ConsoleColor.Blue; + if (i == currentSelection) + System.Console.ForegroundColor = ConsoleColor.Blue; - Console.Write(options[i]); + System.Console.Write(options[i]); - Console.ResetColor(); - } + System.Console.ResetColor(); + } - key = Console.ReadKey(true).Key; + key = System.Console.ReadKey(true).Key; - switch (key) - { - case ConsoleKey.LeftArrow: - { - if (currentSelection % optionsPerLine > 0) - currentSelection--; - break; - } - case ConsoleKey.RightArrow: - { - if (currentSelection % optionsPerLine < optionsPerLine - 1) - currentSelection++; - break; - } - case ConsoleKey.UpArrow: - { - if (currentSelection >= optionsPerLine) - currentSelection -= optionsPerLine; - break; - } - case ConsoleKey.DownArrow: - { - if (currentSelection + optionsPerLine < options.Length) - currentSelection += optionsPerLine; - break; - } - case ConsoleKey.Escape: - { - if (canCancel) - return -1; - break; - } - } - } while (key != ConsoleKey.Enter); + switch (key) + { + case ConsoleKey.LeftArrow: + { + if (currentSelection % optionsPerLine > 0) + currentSelection--; + break; + } + case ConsoleKey.RightArrow: + { + if (currentSelection % optionsPerLine < optionsPerLine - 1) + currentSelection++; + break; + } + case ConsoleKey.UpArrow: + { + if (currentSelection >= optionsPerLine) + currentSelection -= optionsPerLine; + break; + } + case ConsoleKey.DownArrow: + { + if (currentSelection + optionsPerLine < options.Length) + currentSelection += optionsPerLine; + break; + } + case ConsoleKey.Escape: + { + if (canCancel) + return -1; + break; + } + } + } while (key != ConsoleKey.Enter); - Console.CursorVisible = true; + System.Console.CursorVisible = true; - return currentSelection; + return currentSelection; + } } } \ No newline at end of file