aboutsummaryrefslogtreecommitdiffstats
path: root/examples/UserView/UserViewCli/Program.cs
blob: fb9791248af9e8931ba886fdecd971bd91e969a0 (plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/***************************************************************************************************
 Copyright (C) 2025 The Qt Company Ltd.
 SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
***************************************************************************************************/

using UserViewLib;

namespace UserViewCli
{
    internal class Program
    {
        public static IUserList Users { get; private set; } = new UserList();

        static void Main(string[] args)
        {
            Console.WriteLine("Fetching random users...");
            Users.AddRange(RandomUserService.Fetch(20)
                .OrderBy(x => x, UserComparer.ByLastName)
                .ToList());
            ConsoleExt.ClearScreen(false);
            PrintUsers();

            var rand = new Random();
            bool escPressed = false;
            while (!escPressed) {
                if (Console.KeyAvailable) {
                    switch (Console.ReadKey(true).Key) {
                        case ConsoleKey.Escape:
                            escPressed = true;
                            continue;
                        case ConsoleKey.Enter:
                            AddUser();
                            break;
                        case ConsoleKey.Backspace:
                            RemoveUser(rand.Next(Users.Count));
                            break;
                    }
                    while (Console.KeyAvailable)
                        Console.ReadKey(true);
                }
                Thread.Sleep(100);
                var w = rand.Next(100);
                if (w < 10)
                    RemoveUser(rand.Next(Users.Count));
                else if (w < 20)
                    AddUser();
            }
            ConsoleExt.ClearScreen(false);
        }

        private static void AddUser()
        {
            var newUser = RandomUserService.Fetch();
            var index = Users.BinarySearch(newUser, UserComparer.ByLastName);
            if (index < 0) {
                Users.Add(newUser, ~index);
                PrintUsers(~index, ConsoleColor.Green);
                PrintUsers();
            }
        }

        private static void RemoveUser(int removeIndex)
        {
            PrintUsers(removeIndex, ConsoleColor.Red);
            Users.RemoveAt(removeIndex);
            PrintUsers();
        }

        private static void PrintUsers(int hlIndex = -1, ConsoleColor hlColor = ConsoleColor.Black)
        {
            ConsoleExt.ClearScreen();
            var users = Users
                .Select(x => $@"{x.Name.Full} ({x.Age}) ({x.Email})")
                .ToList();
            for (int i = 0; i < users.Count; i++) {
                if (i == hlIndex)
                    Console.ForegroundColor = hlColor;
                ConsoleExt.WriteLine(users[i]);
                if (i == hlIndex)
                    Console.ResetColor();
            }
            Console.Title = $"Users: {users.Count}";
            Thread.Sleep(500);
        }
    }
}