Skip to content

Instantly share code, notes, and snippets.

@Strelok78
Last active May 17, 2023 06:42
Show Gist options
  • Save Strelok78/9e630c4780e1a0d6c8f8cf6ad950cc9a to your computer and use it in GitHub Desktop.
Save Strelok78/9e630c4780e1a0d6c8f8cf6ad950cc9a to your computer and use it in GitHub Desktop.
Draws a certain bar in a certain position. It also accepts a certain shaded percentage.
internal class Program
{
static void Main(string[] args)
{
int maxValue = 10;
int value = 5;
int colorCode = 1;
char openSymbol = '[';
char closeSymbol = ']';
char parametrSymbol = ' ';
bool isOpen = true;
while (isOpen)
{
Enter(ref maxValue, ref value, ref colorCode, ref openSymbol, ref closeSymbol, ref parametrSymbol);
ParametersBar(maxValue, value, colorCode, openSymbol, closeSymbol, parametrSymbol);
Console.WriteLine("Clcik ESC button to close app or any other to restart");
if (Console.ReadKey().Key == ConsoleKey.Escape)
{
isOpen = false;
}
else
{
Console.ReadKey();
Console.Clear();
}
}
}
static void Enter(ref int maxValue, ref int value, ref int colorCode, ref char openSymbol, ref char closeSymbol, ref char parametrSymbol)
{
string messageText = "Enter in 1 row with spaces between: max parametr value; parametr value; parametr color where 1 is green, 2 is red and 3 is blue;\n" +
"Also you are able to enter (not necessary): open symbol; close symbol; parametr symbol;\n\n";
Console.SetCursorPosition(0, 10);
Console.Write(messageText);
Console.Write("Enter Max Parameter Value: ");
maxValue = int.Parse(Console.ReadLine());
Console.Write("Enter Parameter Value: ");
value = int.Parse(Console.ReadLine());
Console.Write("Enter Parameter Color Code: ");
colorCode = int.Parse(Console.ReadLine());
Console.Write("Enter Open Symbol: ");
openSymbol = Console.ReadKey().KeyChar;
Console.Write("\nEnter Close Symbol: ");
closeSymbol = Console.ReadKey().KeyChar;
Console.Write("\nEnter Parameter Symbol: ");
parametrSymbol = Console.ReadKey().KeyChar;
}
static void ParametersBar(int maxValue, int value, int colorCode, char openSymbol = '[', char closeSymbol = ']', char parameterSymbol = ' ')
{
ConsoleColor color = ConsoleColor.White;
switch (colorCode)
{
case 0:
color = ConsoleColor.Green;
break;
case 1:
color = ConsoleColor.Red;
break;
case 2:
color = ConsoleColor.Blue;
break;
}
Console.SetCursorPosition(0, 0);
ConsoleColor defaultColor = Console.BackgroundColor;
Console.Write(openSymbol);
for (int i = 0; i < maxValue; i++)
{
if (i < value)
{
Console.BackgroundColor = color;
Console.Write(parameterSymbol);
}
else
{
Console.BackgroundColor = defaultColor;
Console.Write(" ");
}
}
Console.Write(closeSymbol);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment