aboutsummaryrefslogtreecommitdiffstats
path: root/examples/UserView/UserViewCli/Program.cs
blob: 7a38dd3c47447bc656becd077a987c339baf81ff (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
/***************************************************************************************************
 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...");
            RandomUserService.Fetch(20)
                .OrderBy(x => x, UserComparer.ByLastName)
                .ToList()
                .ForEach(x => Users.Add(x));
            ConsoleExt.ClearScreen(false);
            PrintUsers();

            var rand = new Random();
            while (!ConsoleExt.EscapePressed) {
                Thread.Sleep(100);
                var w = rand.Next(100);
                if (w < 10) {
                    var removeIndex = rand.Next(Users.Count);
                    PrintUsers(removeIndex, ConsoleColor.Red);
                    Users.RemoveAt(removeIndex);
                    PrintUsers();
                }
                else if (w < 20) {
                    var newUser = RandomUserService.Fetch();
                    var index = Users.BinarySearch(newUser, UserComparer.ByLastName);
                    if (index < 0) {
                        Users.Add(newUser, ~index);
                        PrintUsers(~index, ConsoleColor.Green);
                        PrintUsers();
                    }
                }
            }
            ConsoleExt.ClearScreen(false);
        }

        private static void PrintUsers(int hlIndex = -1, ConsoleColor hlColor = ConsoleColor.Black)
        {
            ConsoleExt.ClearScreen();
            var users = Users
                .Select(x => $@"{x.Name.Full} ({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();
            }
            Thread.Sleep(500);
        }
    }
}