Created
February 9, 2023 17:31
-
-
Save JerryNixon/6bf47679b77c5815268e6b1f0538bdba to your computer and use it in GitHub Desktop.
Week 04 Coding in Class Snapshot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
internal class Program | |
{ | |
private static async Task Main(string[] args) | |
{ | |
Setup(); | |
var items = new[] { "Clear the directory", "Create files", "Arrange files" }; | |
var current = items[0]; | |
while (true) | |
{ | |
Menu.Draw(0, 0, current, items); | |
var key = Console.ReadKey(true).Key; | |
if (key == ConsoleKey.UpArrow) | |
{ | |
if (current != items.First()) | |
{ | |
var index = Array.IndexOf(items, current); | |
current = items[index - 1]; | |
} | |
} | |
else if (key == ConsoleKey.DownArrow) | |
{ | |
if (current != items.Last()) | |
{ | |
var index = Array.IndexOf(items, current); | |
current = items[index + 1]; | |
} | |
} | |
else if (key == ConsoleKey.Enter) | |
{ | |
var l = items.Max(x => x.Length); | |
var s = new string(' ', l); | |
Screen.WriteAt(s, 0, items.Length, ConsoleColor.White, ConsoleColor.Black); | |
Screen.WriteAt(current, 0, items.Length, ConsoleColor.Yellow, ConsoleColor.Blue); | |
} | |
else | |
{ | |
HandleInvalidKey(); | |
} | |
} | |
void HandleInvalidKey() | |
{ | |
var l = items.Max(x => x.Length); | |
var s = new string(' ', l); | |
Screen.WriteAt(s, 0, items.Length, ConsoleColor.White, ConsoleColor.Black); | |
Screen.WriteAt("Invalid selection", 0, items.Length, ConsoleColor.Yellow, ConsoleColor.Blue); | |
} | |
static void Setup() | |
{ | |
Console.Clear(); | |
Console.CursorVisible = false; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class Screen | |
{ | |
/// <summary> | |
/// Writes the text to the console at the specified (x, y) position, with optional foreground and background colors. | |
/// </summary> | |
/// <param name="text">The text to write to the console.</param> | |
/// <param name="x">The x-coordinate of the position to write the text.</param> | |
/// <param name="y">The y-coordinate of the position to write the text.</param> | |
/// <param name="foreground">The foreground color to use for the text. If not specified, the current foreground color will be used.</param> | |
/// <param name="background">The background color to use for the text. If not specified, the current background color will be used.</param> | |
public static void WriteAt(this string text, int x, int y, ConsoleColor? foreground = null, ConsoleColor? background = null) | |
{ | |
if (foreground is null) | |
{ | |
foreground = ConsoleColor.White; | |
} | |
Console.ForegroundColor = foreground.Value; | |
Console.BackgroundColor = background ?? ConsoleColor.Black; | |
Console.SetCursorPosition(left: x, top: y); | |
Console.WriteLine(text); | |
Console.ResetColor(); | |
} | |
/// <summary> | |
/// Listens for a keystroke from the user and returns the first allowed key that is pressed. | |
/// </summary> | |
/// <param name="allowed">An array of allowed keys to listen for.</param> | |
/// <param name="key">The first allowed key that was pressed by the user.</param> | |
public static void Listen(this ConsoleKey[] allowed, out ConsoleKey key) | |
{ | |
throw new NotImplementedException(); | |
} | |
} |
Here's another interesting approach that accomplishes the same thing using switch expressions.
current = key switch
{
ConsoleKey.UpArrow when current != items.First() => items[Array.IndexOf(items, current) - 1],
ConsoleKey.DownArrow when current != items.Last() => items[Array.IndexOf(items, current) + 1],
_ => current,
};
if (key == ConsoleKey.Enter)
{
var l = items.Max(x => x.Length);
var s = new string(' ', l);
Screen.WriteAt(s, 0, items.Length, ConsoleColor.White, ConsoleColor.Black);
Screen.WriteAt(current, 0, items.Length, ConsoleColor.Yellow, ConsoleColor.Blue);
}
To help you out, here's a first draft at Box.Draw().
public static void Draw(this Rectangle rect, char fill = ' ')
{
// Character reference: ┌ ─ ┐ │ └ ─ ┘
var top = $"┌{new string('─', rect.Width)}┐";
Screen.WriteAt(top, rect.Left, rect.Top);
// TODO: middle
var bottom = $"└{new string('─', rect.Width)}┘";
Screen.WriteAt(top, rect.Left, rect.Top + rect.Height);
}
If you want to rewrite the signature to not use Rectangle, you can do that.
public static void Draw(int x, int y, int height, int width, char fill = ' ')
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What would that
if-then
statement look like as acase
statement?