示例#1
0
        /// <summary>
        /// Prompts the user to input a file path.
        /// </summary>
        /// <param name="message"><inheritdoc cref="Write" path="/param[@name='message']"/></param>
        /// <param name="checkExists">Indicates whether to check the file exists before allowing the user exit the loop.</param>
        /// <param name="color"><inheritdoc cref="Write" path="/param[@name='color']"/></param>
        /// <returns></returns>
        public static string PromptForFilepath(string message, bool checkExists, ConsoleColor?color = null)
        {
            string path;

            do
            {
                Consoul.Write(message, color);
                path = Consoul.Read();
            } while (string.IsNullOrEmpty(path) && (checkExists ? !File.Exists(path) : true));
            if (path.StartsWith("\"") && path.EndsWith("\""))
            {
                path = path.Substring(1, path.Length - 2);
            }
            return(path);
        }
示例#2
0
文件: Prompt.cs 项目: tbm0115/Consoul
        /// <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);
        }