Ejemplo n.º 1
0
        // prints the question
        public virtual IQuestion Ask()
        {
            IUserTerminal terminal = this.Terminal;

            // 1. ask the question, simply buy just printing it
            terminal.ForegroundColor = this.Questionnaire.Settings.QuestionIconColor;
            terminal.Printer.Write($"{this.Questionnaire.Settings.QuestionIcon} ");


            terminal.ForegroundColor = this.Questionnaire.Settings.QuestionColor;
            terminal.Printer.Write(this.Text);

            // prints any hints available
            if (this.Hint.Trim().Length > 0)
            {
                terminal.ForegroundColor = this.Questionnaire.Settings.HintColor;
                terminal.Printer.Write($"( {this.Hint} )");
            }

            // a single space to seperate q/a
            terminal.Printer.Write(" ");

            // 2. wait for the user to give answer to the question
            this.TakeAnswer();

            terminal.ResetColor();

            return(this);
        }
Ejemplo n.º 2
0
        protected virtual void PrintIndividualOption(IOption option, bool isActive, bool highlight = false)
        {
            IUserTerminal terminal = this.Terminal;

            // which option is selected
            if (isActive)
            {
                terminal.ForegroundColor = this.Questionnaire.Settings.AnswerColor;

                if (this.ShowAsRadio)
                {
                    terminal.Printer.Write("    (O) ");
                }
                else
                {
                    terminal.Printer.Write("    > ");
                }
            }
            else
            {
                if (this.ShowAsRadio)
                {
                    terminal.Printer.Write("    ( ) ");
                }
                else
                {
                    terminal.Printer.Write("    > ");
                }
            }
            //	**********************************************************

            terminal.Printer.WriteLine($"{option.Text}");
            terminal.ForegroundColor = this.Questionnaire.Settings.QuestionColor;
        }
 public ParkingTerminal(
     IUserTerminal userTerminal,
     ICommandExtractor commandExtractor,
     ICommandExecutor commandExecutor)
 {
     _userTerminal     = userTerminal;
     _commandExtractor = commandExtractor;
     _commandExecutor  = commandExecutor;
 }
Ejemplo n.º 4
0
        public Questionnaire(IUserTerminal userConsole = null, QuestionnaireSetting settings = null)
        {
            //this.Builder = builder ?? new QuestionBuilder();
            this.Terminal = userConsole ?? new UserTerminal();
            this.Settings = settings ?? new QuestionnaireSetting();

            this._branches           = new List <IBranch>();
            this._questions          = new List <IQuestion>();
            this._processedQuestions = new List <IQuestion>();
        }
Ejemplo n.º 5
0
        protected override IQuestion TakeAnswer( )
        {
            IUserTerminal terminal = this.Terminal;

            // this should be before ReadLine,
            // because ReadLine will eliminate the current Lef position and reset it to 0
            int cursorLeft = Console.CursorLeft;

            // always set the color of terminal to AnswerColor
            terminal.ForegroundColor = this.Questionnaire.Settings.AnswerColor;
            this.Answer = terminal.Scanner.ReadLine( );

            // this should be after ReadLine because
            // before the user enters the input he/she might resize the console,
            // hence the top will change
            int cursorTop = Console.CursorTop;

            if (this.Answer?.Trim().Length == 0 && this.DefaultAnswer != null)
            {
                Console.SetCursorPosition(left: cursorLeft, top: cursorTop - 1);
                terminal.Printer.Write(this.DefaultAnswer);
                this.Answer = this.DefaultAnswer;
            }

            bool result = this.Validate( );

            this.State = result ? QuestionStates.Valid : QuestionStates.Invalid;

            if (result)
            {
                this.ClearLine(cursorTop);
            }
            else
            {
                this.PrintValidationErrors( );

                // -1 beacause of readline
                var line = cursorTop - 1;

                this.ClearAnswer(line: line);
                Console.SetCursorPosition(left: cursorLeft, top: line);
                return(this.TakeAnswer( ));
            }

            terminal.ResetColor( );

            return(this);
        }
Ejemplo n.º 6
0
        protected override void DrawOptions(int active = -1)
        {
            IUserTerminal terminal = this.Terminal;

            // set terminal to next line
            terminal.ForegroundColor = this.Questionnaire.Settings.QuestionColor;
            terminal.Printer.WriteLine();

            int            page          = active / this.VisibleOptions;
            List <IOption> visible_items = this.Options.ToList().Skip(page * this.VisibleOptions).Take(this.VisibleOptions).ToList();

            for (int index = 0; index < visible_items.Count; index++)
            {
                this.PrintIndividualOption(visible_items[index], visible_items[index].Selected, active % this.VisibleOptions == index);
            }
        }
Ejemplo n.º 7
0
        protected virtual void DrawOptions(int active = -1)
        {
            IUserTerminal terminal = this.Terminal;

            // set terminal to next line
            terminal.ForegroundColor = this.Questionnaire.Settings.QuestionColor;
            terminal.Printer.WriteLine();

            int            page          = active / this.VisibleOptions;
            List <IOption> visible_items = this._options.Skip(page * this.VisibleOptions).Take(this.VisibleOptions).ToList();

            for (int index = 0; index < visible_items.Count; index++)
            {
                this.PrintIndividualOption(visible_items[index], index == (active >= this.VisibleOptions ? active - this.VisibleOptions : active));
            }
        }
Ejemplo n.º 8
0
        protected override IQuestion TakeAnswer()
        {
            IUserTerminal terminal = this.Terminal;

            int column = Console.CursorLeft;
            int line   = Console.CursorTop;

            int activeOption = -1;

            if (this.State == QuestionStates.Initilaized)
            {
                this.DrawOptions();
            }

            Console.SetCursorPosition(column, line);
            activeOption = this.HandleInput(column, line, activeOption);

            this.State = this.Validate() ? QuestionStates.Valid : QuestionStates.Invalid;

            if (this.State == QuestionStates.Invalid)
            {
                Console.SetCursorPosition(0, line + this.VisibleOptions + 1);
                this.PrintValidationErrors();
                Console.SetCursorPosition(column, line);
                this.TakeAnswer();
            }
            else
            {
                this.ClearLines(line + 1, line + this.VisibleOptions + 1);
            }

            // resets the cursor to the next line,
            // because of the options the cursor might be in wrong position for the next question
            Console.SetCursorPosition(0, line + 1);

            terminal.ResetColor();
            return(this);
        }
Ejemplo n.º 9
0
        protected override void PrintIndividualOption(IOption option, bool isActive, bool highlight = false)
        {
            IUserTerminal terminal = this.Terminal;

            if (highlight)
            {
                terminal.ForegroundColor = this.Questionnaire.Settings.AnswerColor;
            }

            // which option is selected
            if (isActive)
            {
                terminal.Printer.Write("    [x] ");
            }
            else
            {
                terminal.Printer.Write("    [ ] ");
            }
            //	**********************************************************

            terminal.Printer.WriteLine($"{option.Text}");
            terminal.ForegroundColor = this.Questionnaire.Settings.QuestionColor;
        }
Ejemplo n.º 10
0
        protected override int HandleInput(int column, int line, int activeOptionIndex)
        {
            IUserTerminal terminal = this.Terminal;

            bool answered = false;

            while (!answered)
            {
                ConsoleKeyInfo keyInfo = Console.ReadKey();

                switch (keyInfo.Key)
                {
                case ConsoleKey.Enter:
                    this.Answer = this.RedrawAnswer(line);

                    if (this.Answer.Trim().Length == 0 && this.DefaultAnswer != null)
                    {
                        this._options
                        .Where(op => this.DefaultAnswer.Split(',').Contains(op.Text))
                        .Select(op => op).ToList().ForEach(defaultSelected => defaultSelected.Selected = true);;

                        this.Answer = this.RedrawAnswer(line);
                    }

                    answered = true;
                    break;

                case ConsoleKey.UpArrow:
                    Console.SetCursorPosition(column, line);

                    this.ClearLines(line + 1, line + this._options.Count);
                    Console.SetCursorPosition(column, line);

                    --activeOptionIndex;
                    if (activeOptionIndex == -1)
                    {
                        activeOptionIndex = this._options.Count - 1;
                    }

                    this.DrawOptions(activeOptionIndex);
                    Console.SetCursorPosition(column, line);

                    break;

                case ConsoleKey.DownArrow:
                    Console.SetCursorPosition(column, line);

                    this.ClearLines(line + 1, line + this._options.Count);
                    Console.SetCursorPosition(column, line);

                    ++activeOptionIndex;
                    if (activeOptionIndex == this._options.Count)
                    {
                        activeOptionIndex = 0;
                    }

                    this.DrawOptions(activeOptionIndex);
                    Console.SetCursorPosition(column, line);

                    break;

                case ConsoleKey.Spacebar:

                    Console.SetCursorPosition(column, line);

                    IOption selectedItem = activeOptionIndex > -1 ? this._options[activeOptionIndex] : null;
                    if (selectedItem != null)
                    {
                        selectedItem.Selected = !selectedItem.Selected;
                        this.DrawOptions(activeOptionIndex);
                    }

                    this.RedrawAnswer(line);

                    break;

                case ConsoleKey.F2:
                default:
                    this.RedrawAnswer(line);
                    break;
                }
            }

            return(activeOptionIndex);
        }
Ejemplo n.º 11
0
        protected virtual int HandleInput(int column, int line, int activeOptionIndex)
        {
            IUserTerminal terminal = this.Terminal;

            bool answered = false;

            while (!answered)
            {
                ConsoleKeyInfo keyInfo = Console.ReadKey();

                switch (keyInfo.Key)
                {
                case ConsoleKey.Enter:
                    this.Answer = activeOptionIndex > -1 ? this._options[activeOptionIndex].Text : null;

                    if (this.Answer == null && this.DefaultAnswer != null)
                    {
                        this._options
                        .Where(op => this.DefaultAnswer.Split(',').Contains(op.Text))
                        .Select(op => op).ToList().ForEach(defaultSelected => defaultSelected.Selected = true);;

                        Console.SetCursorPosition(column, line);
                        terminal.ForegroundColor = this.Questionnaire.Settings.AnswerColor;
                        terminal.Printer.WriteLine(this.DefaultAnswer);

                        this.Answer = this.DefaultAnswer;
                    }

                    answered = true;
                    break;

                case ConsoleKey.UpArrow:
                    Console.SetCursorPosition(column, line);


                    this.ClearLines(line + 1, line + this._options.Count);
                    Console.SetCursorPosition(column, line);

                    --activeOptionIndex;
                    if (activeOptionIndex == -1)
                    {
                        activeOptionIndex = this._options.Count - 1;
                    }

                    this.DrawOptions(activeOptionIndex);
                    Console.SetCursorPosition(column, line);

                    this.ClearAnswer(line);
                    terminal.ForegroundColor = this.Questionnaire.Settings.AnswerColor;
                    terminal.Printer.Write(this._options[activeOptionIndex].Text);

                    break;

                case ConsoleKey.DownArrow:
                    Console.SetCursorPosition(column, line);

                    this.ClearLines(line + 1, line + this._options.Count);
                    Console.SetCursorPosition(column, line);

                    ++activeOptionIndex;
                    if (activeOptionIndex == this._options.Count)
                    {
                        activeOptionIndex = 0;
                    }

                    this.DrawOptions(activeOptionIndex);
                    Console.SetCursorPosition(column, line);

                    this.ClearAnswer(line);
                    terminal.ForegroundColor = this.Questionnaire.Settings.AnswerColor;
                    terminal.Printer.Write(this._options[activeOptionIndex].Text);
                    break;

                default:
                    break;
                }
            }

            return(activeOptionIndex);
        }