about summary refs log tree commit diff
path: root/BugMine.CLI/Classes/BaseTUIMenu.cs
blob: 1cf4876a4c2b3363f6030ae58380e75b8c883e07 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
namespace BugMine.CLI.Interfaces;

public abstract class BaseTUIMenu(ILogger logger) {
    public virtual Dictionary<string, Func<Task>> MenuItems { get; set; }

    public virtual async Task Execute() { }

    public void HandleMenu() {
        if (MenuItems == null) {
            logger.LogCritical("Menu {type} had no MenuItems intialised!", this.GetType().FullName);
            Environment.Exit(1);
        }
        
        int currentIndex = 0;
        bool running = true;
        // int startHeight = Console.CursorTop;
        // Console.WriteLine("sh - " + startHeight);
        while (running) {
            Console.CursorLeft = 0;
            
            // Console.CursorTop = startHeight;
            Console.Beep();
            int i = 0;
            foreach (var (key, value) in MenuItems) {
                Console.Beep();
                Thread.Sleep(25);
                Console.WriteLine($"{(i++ == currentIndex ? ">" : " ")} {key}                        " + Console.CursorTop);
            }

            Console.CursorTop -= MenuItems.Count - currentIndex;
            var oldIndex = currentIndex;
            var ckey = Console.ReadKey(true);
            Console.Write(ckey.Key);
            switch (ckey.Key) {
                case ConsoleKey.DownArrow:
                    currentIndex++;
                    if (currentIndex >= MenuItems.Count()) currentIndex = MenuItems.Count() - 1;
                    Console.Write($"  NEXT ENTRY: {currentIndex}");
                    break;
                case ConsoleKey.UpArrow:
                    currentIndex--;
                    if (currentIndex < 0) currentIndex = 0;
                    Console.Write($"  NEXT ENTRY: {currentIndex}");
                    break;
            }

            // Console.CursorTop -= MenuItems.Count() - oldIndex;
            Thread.Sleep(250);
            // Console.CursorTop -= currentIndex - 1;
        }
    }
}