Example #1
0
        /// <summary>
        /// Shows the menu items and return the user's choice
        /// </summary>
        /// <returns>string with the user selected option</returns>
        public string Show()
        {
            var tokenizer = new AcceleratorCharTokenizer();
            int i         = 0;

            Keys.Clear();
            foreach (var item in Items)
            {
                if (Options.NumerationStyle == MenuNumeration.AcceleratorKey)
                {
                    Console.WriteLine(tokenizer.Parse(item));
                    Keys.Add(GetAcceleratorChar(item).ToString());
                }
                else
                {
                    (string key, IEnumerable <ColorToken> tokens) = ComposeMenuItem(i, item);
                    Console.WriteLine(tokens);
                    Keys.Add(key);
                }
                i++;
            }
            Console.Ask(BuildMessage());
            string value = Console.ReadLine <string>(x => IsDefaultValue(x) || Keys.Contains(x));

            return(IsDefaultValue(value) ? Options.DefaultItem : value);
        }
Example #2
0
        public void ParseWithEscapedAcceleratorChar()
        {
            string text   = "Hello world && moon";
            var    parser = new AcceleratorCharTokenizer();
            var    tokens = parser.Parse(text);

            Assert.Single(tokens);
            Assert.Equal(text.Replace("&&", "&"), tokens.First().Text);
        }
Example #3
0
        public void ParseWithNoAcceleratorChar()
        {
            string text   = "Hello world!";
            var    parser = new AcceleratorCharTokenizer();
            var    tokens = parser.Parse(text);

            Assert.Single(tokens);
            Assert.Equal(text, tokens.First().Text);
        }
Example #4
0
        public void ParseAtFirstChar()
        {
            string text   = "&Hello world!";
            var    parser = new AcceleratorCharTokenizer();
            var    tokens = parser.Parse(text);

            Assert.Collection(tokens,
                              x => Assert.Equal("H", x.Text),
                              x => Assert.Equal(text.Substring(2), x.Text)
                              );
        }
Example #5
0
        public void ParseAtEnd()
        {
            string text   = "Hello &world&";
            var    parser = new AcceleratorCharTokenizer();
            var    tokens = parser.Parse(text);

            Assert.Collection(tokens,
                              x => Assert.Equal("Hello ", x.Text),
                              x => Assert.Equal("w", x.Text),
                              x => Assert.Equal("orld", x.Text)
                              );
        }
Example #6
0
        public void AcceleratorCharHasHighlightColor()
        {
            string text   = "Hello &world&";
            var    parser = new AcceleratorCharTokenizer();
            var    tokens = parser.Parse(text);

            Assert.Equal(3, tokens.Count());
            var token = tokens.First();

            Assert.Equal(Console.Instance.Colors.ForeColor, token.Color.Foreground);
            Assert.Equal(Console.Instance.Colors.BackColor, token.Color.Background);

            token = tokens.Skip(1).First();
            Assert.Equal(Console.Instance.Colors.HighlightColor, token.Color.Foreground);
            Assert.Equal(Console.Instance.Colors.BackColor, token.Color.Background);
        }