示例#1
0
 public HandleAnswer(CommandContext context, BooleanAnswerHandler handler)
 {
     _type    = AnswerType.Boolean;
     _options = context.HttpContext.RequestServices.GetRequiredService <IOptions <BeavisCliOptions> >().Value;
     _booleanAnswerHandler = handler;
     _context = context;
 }
示例#2
0
        private static Task <CommandResult> AskQuestion(CommandContext context, string[] questions, bool useMask, StringAnswerHandler stringHandler, BooleanAnswerHandler booleanHandler)
        {
            // this will be invoked just before we are sending the response
            context.Response.Sending += (sender, args) =>
            {
                IJobPool pool = context.HttpContext.RequestServices.GetRequiredService <IJobPool>();

                IJob job;

                if (stringHandler != null)
                {
                    job = new HandleAnswer(context, stringHandler);
                }
                else if (booleanHandler != null)
                {
                    job = new HandleAnswer(context, booleanHandler);
                }
                else
                {
                    throw new InvalidOperationException($"{nameof(StringAnswerHandler)} or {nameof(BooleanAnswerHandler)} must be set.");
                }

                string key = pool.Push(job);

                foreach (string question in questions)
                {
                    context.WriteText(question);
                }

                // disable terminal prompt
                context.WriteJs(new SetPrompt(""));

                // disable terminal history -> this will set back to 'enabled' in HandleAnswerJob
                context.WriteJs(new SetHistory(false));

                if (useMask)
                {
                    context.WriteJs(new SetMask(true));
                    context.WriteJs(new QueueJob(key, new SetMask(false)));
                }
                else
                {
                    context.WriteJs(new QueueJob(key));
                }
            };

            return(Task.FromResult(CommandResult.Default));
        }
示例#3
0
        public static Task <CommandResult> AskConfirmation(this CommandContext context, string question, BooleanAnswerHandler handler)
        {
            var questions = new[]
            {
                question,
                "Only 'yes' will be accepted to approve.",
                "Enter a value:"
            };

            return(AskQuestion(context, questions, false, null, handler));
        }