// 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); }
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); }
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); }