Esempio n. 1
0
        /// <summary>
        /// Waits for user input and reads the user response.
        /// </summary>
        /// <returns>Value from the user</returns>
        public static string Read()
        {
            bool         keyControl = false, keyAlt = false, keyShift = false;
            RoutineInput input = new RoutineInput();

            if (Routines.HasBuffer())
            {
                input = Routines.Next();

                long delayTicks = 0;
                if (Routines.UseDelays && input.Delay.Value != null)
                {
                    delayTicks = input.Delay.Value.Ticks / 2;
                }
                TimeSpan delay = new TimeSpan(delayTicks);
                if (!string.IsNullOrEmpty(input.Description))
                {
                    Write(input.Description, RenderOptions.SubnoteColor);
                }

                System.Threading.Thread.Sleep(delay);
                Write(input.Value, ConsoleColor.Cyan);
                System.Threading.Thread.Sleep(delay);
            }
            else
            {
                string userInput = string.Empty;
                input.Value = Console.ReadLine();
            }

            if (Routines.PromptRegistry.Any())
            {
                input.OptionReference = Routines.PromptRegistry.FirstOrDefault(o => (o.Index + 1).ToString() == input.Value);
                //if (keyControl)
                //{
                //    input.Method = RoutineInput.InputMethod.OptionText;
                //}
            }

            // Check if we should save the input to the Routine Stack
            if (Routines.MonitorInputs)
            {
                Routines.UserInputs.Push(input);
            }

            return(input.Value);
        }
Esempio n. 2
0
        /// <summary>
        /// Displays the options for this prompt. Loops until the user "selects" the appropriate option.
        /// </summary>
        /// <returns>Zero-based index of the selected option.</returns>
        public int Run()
        {
            string[] escapePhrases = new string[]
            {
                "go back",
                "back",
                "exit",
                "goback"
            };
            string       input         = "";
            int          selection     = -1;
            PromptOption defaultOption = _options.FirstOrDefault(o => o.IsDefault);

            if (defaultOption != null)
            {
                _options.Where(o => o.Index != defaultOption.Index && o.IsDefault).ToList().ForEach(o => o.IsDefault = false);
            }
            do
            {
                if (ClearConsole)
                {
                    Console.Clear();
                }
                Consoul._write(Message, RenderOptions.PromptColor);
                Consoul._write("Choose the corresponding number from the options below:", RenderOptions.SubnoteColor);
                int i = 0;

                Routines.RegisterOptions(this);
                foreach (PromptOption option in Options)
                {
                    Consoul._write(option.ToString(), option.Color);
                    i++;
                }
                Console.ForegroundColor = RenderOptions.DefaultColor;
                input = Consoul.Read();// Console.ReadLine();
                if (string.IsNullOrEmpty(input) && defaultOption != null)
                {
                    selection = defaultOption.Index;
                }
                else if (escapePhrases.Any(o => input.Equals(o, StringComparison.OrdinalIgnoreCase)))
                {
                    return(Consoul.EscapeIndex);
                }
                else
                {
                    Int32.TryParse(input, out selection);
                    if (selection <= 0 || selection > (_options.Count + 1))
                    {
                        Consoul._write("Invalid selection!", RenderOptions.InvalidColor);
                        selection = -1;
                    }
                    else
                    {
                        selection--;
                    }
                }
            } while (selection < 0);

            _options[selection].Selected = true;

            return(selection);
        }