コード例 #1
0
        /// <summary>
        /// Add custom task
        /// </summary>
        /// <param name="menu"></param>
        /// <param name="key"></param>
        /// <param name="title"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public static ConsoleMenu Add(this ConsoleMenu menu, ConsoleKey key, string title, Func <Task> action, bool waitForCompletion = true)
        {
            var item = new ConsoleMenuItem(key, title, action, waitForCompletion);

            menu.Add(item);
            return(menu);
        }
コード例 #2
0
        public static void AddSubMenu(this ConsoleMenu menu, string title = "Sub Menu", Action <ConsoleMenu> addMenuItems = null)
        {
            var subMenu = new ConsoleMenu(
                menu.WaitForTask,
                menu.CancellationToken,
                title);

            addMenuItems?.Invoke(subMenu);

            menu.Add(new ConsoleTask(title, () => menu.RunSubMenu(subMenu)));
        }
コード例 #3
0
        /// <summary>
        /// Add menu item from class which implements IConsoleTask
        /// </summary>
        /// <param name="menu"></param>
        /// <param name="task"></param>
        /// <param name="waitForCompletion"></param>
        /// <returns></returns>
        public static ConsoleMenu Add(this ConsoleMenu menu, IConsoleTask task, bool waitForCompletion = true, string overrideTitle = null, ConsoleKey?overrideKey = null)
        {
            var key   = overrideKey ?? ConsoleKey.A + menu.ItemCount;
            var title = overrideTitle ?? task.Title;

            if (string.IsNullOrWhiteSpace(title))
            {
                title = task.GetType().Name.FromCamelCaseToSentence();
            }

            var item = new ConsoleMenuItem(
                key,
                title,
                () => menu.RunTask(task.Execute()),
                waitForCompletion);

            menu.Add(item);
            return(menu);
        }
コード例 #4
0
        public static async Task RunMainMenuAndWaitForCompletion(string title = "Lakerfield Main Console Menu", Action <ConsoleMenu> addMenuItems = null, bool onlyExitWhenPressingQuit = true)
        {
            var cancellationTokenSource = new CancellationTokenSource();
            var cancellationToken       = cancellationTokenSource.Token;
            var tasks = new List <Task>();

            var menu = new ConsoleMenu(
                (task) => tasks.Add(task),
                cancellationToken,
                title);

            addMenuItems?.Invoke(menu);

            if (onlyExitWhenPressingQuit)
            {
                menu.Add(ConsoleKey.Q, "Quit", () =>
                {
                    cancellationTokenSource.Cancel();
                    return(Task.CompletedTask);
                });

                while (!cancellationToken.IsCancellationRequested)
                {
                    await menu.Run();

                    if (cancellationToken.IsCancellationRequested)
                    {
                        break;
                    }
                    Console.WriteLine();
                    Console.WriteLine($@"Exit prevented, to quit Q must be pressed");
                }
            }
            else
            {
                await menu.Run();
            }

            tasks = tasks.Where(t => !t.IsCompleted).ToList();
            Console.WriteLine($"Waiting for completion of {tasks.Count} tasks...");
            await Task.WhenAll(tasks);
        }
コード例 #5
0
 /// <summary>
 /// Add menu item from class which implements IConsoleTask
 /// </summary>
 /// <param name="menu"></param>
 /// <param name="task"></param>
 /// <param name="waitForCompletion"></param>
 /// <returns></returns>
 public static ConsoleMenu Add <T>(this ConsoleMenu menu, bool waitForCompletion = true, string overrideTitle = null, ConsoleKey?overrideKey = null) where T : IConsoleTask
 {
     return(menu.Add(Activator.CreateInstance <T>(), waitForCompletion, overrideTitle, overrideKey));
 }
コード例 #6
0
 public Task RunSubMenu(ConsoleMenu subMenu)
 {
     return(RunTask(subMenu.Run()));
 }