/// <summary>
 /// Override the default key for quitting the Prompt.
 /// </summary>
 /// <param name="promptBuilder"></param>
 /// <param name="consoleKey"></param>
 /// <returns></returns>
 public static PromptBuilder SetQuitKeyInfo(this PromptBuilder promptBuilder, ConsoleKey consoleKey)
 {
     promptBuilder.QuitKey = consoleKey;
     return(promptBuilder);
 }
 /// <summary>
 /// Override the default line for quitting the Prompt.
 /// </summary>
 /// <param name="promptBuilder"></param>
 /// <param name="consoleKey"></param>
 /// <returns></returns>
 public static PromptBuilder SetQuitLine(this PromptBuilder promptBuilder, string quitLine)
 {
     promptBuilder.QuitLine = quitLine;
     return(promptBuilder);
 }
 /// <summary>
 /// Define a function that returns the string that populates the prompt.
 /// </summary>
 /// <param name="promptBuilder"></param>
 /// <param name="action"></param>
 /// <returns></returns>
 public static PromptBuilder SetPopulatePromptAction(this PromptBuilder promptBuilder, Func <Task <string> > action)
 {
     promptBuilder.PopulatePrompt = action;
     return(promptBuilder);
 }
 /// <summary>
 /// Define an action to run after you initialize the Prompt, but before the user gets the prompt.
 /// </summary>
 /// <param name="promptBuilder"></param>
 /// <param name="action"></param>
 /// <returns></returns>
 public static PromptBuilder SetOnStartupAction(this PromptBuilder promptBuilder, Func <Task> action)
 {
     promptBuilder.UseOnStartupAction = true;
     promptBuilder.OnStartupAction    = action;
     return(promptBuilder);
 }
 /// <summary>
 /// Override the default console writer to do things like use colors.
 /// </summary>
 /// <param name="promptBuilder"></param>
 /// <param name="console"></param>
 /// <returns></returns>
 public static PromptBuilder SetConsoleWriter(this PromptBuilder promptBuilder, IConsole console)
 {
     promptBuilder.ConsoleWriter = console;
     return(promptBuilder);
 }
 /// <summary>
 /// Define a timepsan to automatically update the prompt at an interval. Useful for including things like date/time in your prompts. Note, if the prompt is "locked" when the auto update occurs, it will just skip it instead of waiting since it will be updated on unlock.
 /// </summary>
 /// <param name="promptBuilder"></param>
 /// <param name="timeSpan"></param>
 /// <returns></returns>
 public static PromptBuilder SetAutoPromptUpdateIfUnlockedTimeSpan(this PromptBuilder promptBuilder, TimeSpan timeSpan)
 {
     promptBuilder.AutomaticUpdatePromptTimeSpan = timeSpan;
     return(promptBuilder);
 }
示例#7
0
        /// <summary>
        /// Run the prompt. Remember that this will call <see cref="Console.ReadKey"/> or <see cref="Console.ReadLine"/> which will block the main thread.
        /// </summary>
        /// <param name="promptBuilder"></param>
        /// <returns></returns>
        public static async Task Run(this PromptBuilder promptBuilder)
        {
            _lastPromptLength = 0;
            _promptBuilder    = promptBuilder;
            await UpdatePrompt();

            if (_promptBuilder.UseOnStartupAction)
            {
                await _promptBuilder.OnStartupAction.Invoke();
            }

            if (_promptBuilder.AutomaticUpdatePromptTimeSpan != TimeSpan.Zero)
            {
                _promptUpdateTimer = new Timer((s) =>
                {
                    if (Lock.CurrentCount > 0)
                    {
                        UpdatePrompt().Wait();
                    }
                }, null, _promptBuilder.AutomaticUpdatePromptTimeSpan, _promptBuilder.AutomaticUpdatePromptTimeSpan);
            }

            if (_promptBuilder.UseKeyHandler)
            {
                while (true)
                {
                    var key = System.Console.ReadKey(true);
                    if (_promptBuilder.QuitKey == key.Key)
                    {
                        return;
                    }
                    else
                    {
                        try
                        {
                            await _promptBuilder.KeyHandler.Invoke(key);
                        }
                        catch (Exception e)
                        {
                            WriteLine("Unhandled Exception: {0} - {1}", e.Source, e.Message);
                        }
                    }
                    await UpdatePrompt();
                }
            }
            else if (_promptBuilder.UseLineHandler)
            {
                while (true)
                {
                    var line = System.Console.ReadLine();
                    if (_promptBuilder.QuitLine == line)
                    {
                        return;
                    }
                    else
                    {
                        try
                        {
                            await _promptBuilder.LineHandler.Invoke(line);
                        }
                        catch (Exception e)
                        {
                            WriteLine("Unhandled Exception: {0} - {1}", e.Source, e.Message);
                        }
                    }
                    await UpdatePrompt();
                }
            }
            else
            {
                throw new ArgumentException("No line handler or key handler supplied.");
            }
        }