Exemplo n.º 1
0
        internal InputQuestion(string name, FunctionOrColorString message)
            : base(name, message)
        {
            _askerFn = (q, ans) =>
            {
                bool Validator(string str)
                {
                    var question = (InputQuestion <TValue>)q;

                    if (string.IsNullOrEmpty(str))
                    {
                        str = question.DefaultValue.Resolve(ans);
                    }
                    bool valid = (question.RawValueValidator is null) || question.RawValueValidator(str, ans).Valid;
                    var  teq   = (TextEntryQuestion <TValue>)q;

                    if (valid && teq.IsRequired)
                    {
                        return(teq.AllowWhitespaceOnly
                            ? !string.IsNullOrEmpty(str)
                            : !string.IsNullOrWhiteSpace(str));
                    }

                    return(valid);
                }

                return(ConsoleEx.Prompt(new ColorString(q.Message.Resolve(ans),
                                                        Prompter.Style.Question.ForeColor, Prompter.Style.Question.BackColor).ToString(),
                                        Validator));
            };
        }
Exemplo n.º 2
0
 public MultiLineQuestion(string name, FunctionOrColorString message)
     : base(name, message)
 {
     _askerFn = (q, ans) =>
     {
         ConsoleEx.PrintLine(new ColorString(q.Message.Resolve(ans),
                                             Prompter.Style.Question.ForeColor, Prompter.Style.Question.BackColor));
         return(string.Empty);
     };
 }
Exemplo n.º 3
0
 internal PasswordQuestion(string name, FunctionOrColorString message)
     : base(name, message)
 {
     _askerFn = (q, ans) =>
     {
         var pq = (PasswordQuestion <TValue>)q;
         return(ConsoleEx.ReadSecret(new ColorString(q.Message.Resolve(ans),
                                                     Prompter.Style.Question.ForeColor, Prompter.Style.Question.BackColor),
                                     hideCursor: pq._hideCursor, hideMask: pq._hideMask));
     };
 }
Exemplo n.º 4
0
 internal StaticText(FunctionOrColorString message)
     : base(message)
 {
     _askerFn = (q, ans) =>
     {
         ColorString staticText = q.Message.Resolve(ans);
         if (staticText != null)
         {
             ConsoleEx.PrintLine(staticText);
         }
         return(null);
     };
 }
Exemplo n.º 5
0
        public CheckboxQuestion(string name, FunctionOrColorString message, IEnumerable <string> choices)
            : base(name, message)
        {
            if (choices is null)
            {
                throw new ArgumentNullException(nameof(choices));
            }
            _choices = choices.ToList();

            _askerFn = (q, ans) =>
            {
                var cq = (CheckboxQuestion)q;
                ConsoleEx.PrintLine(new ColorString(q.Message.Resolve(ans),
                                                    Prompter.Style.Question.ForeColor, Prompter.Style.Question.BackColor));
                return(ConsoleEx.SelectMultiple(cq._choices));
            };
        }
Exemplo n.º 6
0
        internal ConfirmQuestion(string name, FunctionOrColorString message, bool @default)
            : base(name, message)
        {
            _askerFn = (q, ans) =>
            {
                ConsoleEx.Print(new ColorString(q.Message.Resolve(ans),
                                                Prompter.Style.Question.ForeColor, Prompter.Style.Question.BackColor));

                var cq = (ConfirmQuestion)q;
                ConsoleEx.Print(new ColorString($" ({(cq._default ? 'Y' : 'y')}/{(cq._default ? 'n' : 'N')}) ",
                                                Prompter.Style.Question.ForeColor, Prompter.Style.Question.BackColor));

                ConsoleKey keyPressed = ConsoleEx.WaitForKeys(ConsoleKey.Y, ConsoleKey.N, ConsoleKey.Enter);
                bool       result     = keyPressed == ConsoleKey.Enter ? cq._default : (keyPressed == ConsoleKey.Y);

                ConsoleEx.PrintLine(result ? "Y" : "N");

                return(result);
            };
            _default = @default;
        }