protected TSelection GetSelection(
     Func <string, string, object> descriptionSelector = null,
     bool isOptional            = false,
     CollectionWriteStyle style = CollectionWriteStyle.Rows)
 {
     return(GetSelection(DefaultSource, descriptionSelector, isOptional, style));
 }
Example #2
0
        protected override string GetSelection <TKey, TValue>(
            IDictionary <TKey, TValue> source,
            Func <TKey, TValue, object> descriptionSelector = null,
            bool isOptional            = false,
            CollectionWriteStyle style = CollectionWriteStyle.Rows)
        {
            var collection = new InputCollection <TKey, TValue>(source, descriptionSelector, isOptional);

            return(collection.GetSelection(_console, style)?.ToString());
        }
Example #3
0
        protected virtual string GetSelectionWithDefault <TKey, TValue>(
            IDictionary <TKey, TValue> source,
            Func <TKey, TValue, object> descriptionSelector = null,
            string defaultValue        = null,
            CollectionWriteStyle style = CollectionWriteStyle.Rows)
        {
            var collection = new InputCollection <TKey, TValue>(source, descriptionSelector, true, defaultValue: defaultValue);

            return(collection.GetSelection(_console, style)?.ToString());
        }
 protected override string[] GetSelection <TKey, TValue>(
     IDictionary <TKey, TValue> source,
     Func <TKey, TValue, object> descriptionSelector = null,
     bool isOptional            = false,
     CollectionWriteStyle style = CollectionWriteStyle.Rows)
 {
     return(_console
            .PromptInputSelections(source, descriptionSelector, isOptional, style)
            .Select(x => x?.ToString())
            .ToArray());
 }
Example #5
0
        public static TValue PromptInputSelection <TKey, TValue>(
            this IConsole console,
            IDictionary <TKey, TValue> source,
            Func <TKey, TValue, object> descriptionSelector = null,
            bool isOptional            = false,
            CollectionWriteStyle style = CollectionWriteStyle.Rows)
        {
            var indexCollection = new InputCollection <TKey, TValue>(source, descriptionSelector, isOptional);

            return(indexCollection.GetSelection(console, style));
        }
Example #6
0
 protected override string GetSelectionWithDefault <TKey, TValue>(
     IDictionary <TKey, TValue> source,
     Func <TKey, TValue, object> descriptionSelector = null,
     string defaultValue        = null,
     CollectionWriteStyle style = CollectionWriteStyle.Rows)
 {
     return(_console.PromptInputSelection(
                source,
                descriptionSelector,
                defaultValue,
                style)?.ToString());
 }
Example #7
0
        protected override TEntity[] GetSelectionInternal(IConsole console, CollectionWriteStyle style, IList <KeyValuePair <TKey, TEntity> > source, bool writeCollection)
        {
            if (writeCollection)
            {
                var display = GenerateDisplay(source);

                console.WriteLine();
                console.WriteCollection(display, style);
            }

            var inputs = console.PromptInputs(null, _allowEmpty);

            if (!inputs.Any())
            {
                return(new TEntity[0]);
            }

            var sourceKeys = source.Select(x => GetKey(x.Key)).ToArray();

            if (inputs[0]?.StartsWith("%") == true && !sourceKeys.Contains(inputs[0]))
            {
                var filter         = inputs[0].Substring(1).Trim();
                var newSourceItems = _source
                                     .Where(x => GetDescription(x.Key, x.Value)?.Contains(filter, StringComparison.InvariantCultureIgnoreCase) == true);

                var newSource = MapSource(newSourceItems);

                return(GetSelectionInternal(console, style, newSource.Any() ? newSource : _source, true));
            }

            var foundInputs = inputs.SelectMany(MapInput).Intersect(sourceKeys);

            if (!foundInputs.Any() && !_allowEmpty)
            {
                return(GetSelectionInternal(console, style, source, false));
            }

            return(foundInputs
                   .Select(x => source.Single(a => GetKey(a.Key) == x).Value)
                   .ToArray());
        }
Example #8
0
        protected override TEntity GetSelectionInternal(IConsole console,
                                                        CollectionWriteStyle style,
                                                        IList <KeyValuePair <TKey, TEntity> > source,
                                                        bool writeCollection)
        {
            if (writeCollection)
            {
                var display = GenerateDisplay(source);

                console.WriteLine();
                console.WriteCollection(display, style);
            }

            var input = PromptInput(console);

            var foundInput = source.Select(x => GetKey(x.Key)).Contains(input);

            if (foundInput)
            {
                return(source.Single(x => GetKey(x.Key) == input).Value);
            }

            if (input.StartsWith("%") == true)
            {
                var filter         = input.Substring(1).Trim();
                var newSourceItems = _source
                                     .Where(x => GetDescription(x.Key, x.Value)?.Contains(filter, StringComparison.InvariantCultureIgnoreCase) == true);

                var newSource = MapSource(newSourceItems).ToArray();

                return(GetSelectionInternal(console, style, newSource.Any() ? newSource : _source, true));
            }

            if (input == null || _isOptional)
            {
                return(source.SingleOrDefault(x => GetKey(x.Key) == _defaultValue).Value);
            }

            return(GetSelectionInternal(console, style, source, false));
        }
Example #9
0
 protected abstract TResult GetSelectionInternal(IConsole console,
                                                 CollectionWriteStyle style,
                                                 IList <KeyValuePair <TKey, TEntity> > source,
                                                 bool writeCollection);
Example #10
0
 public TResult GetSelection(IConsole console, CollectionWriteStyle style)
 {
     return(GetSelectionInternal(console, style, _source, true));
 }
        public static TEnum PromptIndexSelection <TEnum>(this IConsole console, CollectionWriteStyle style = CollectionWriteStyle.Rows) where TEnum : Enum
        {
            var source = Enum.GetValues(typeof(TEnum)).Cast <TEnum>();

            return(console.PromptIndexSelection(source, style));
        }
        public static TEnum[] PromptIndexSelections <TEnum>(this IConsole console, bool allowEmpty = false, CollectionWriteStyle style = CollectionWriteStyle.Rows) where TEnum : Enum
        {
            var source = Enum.GetValues(typeof(TEnum)).Cast <TEnum>();

            return(console.PromptIndexSelections(source, allowEmpty, style));
        }
Example #13
0
 private void OnPromptInputSelection(CollectionWriteStyle style)
 {
     DisplaySelection(() => _console.PromptInputSelection(_source, style: style, isOptional: true));
 }
Example #14
0
        public static T PromptIndexSelection <T>(this IConsole console, IEnumerable <T> source, Func <T, object> descriptionSelector, CollectionWriteStyle style, int?defaultValue)
        {
            var indexCollection = new IndexCollection <T>(source, descriptionSelector, isOptional: true, defaultValue: defaultValue);

            return(indexCollection.GetSelection(console, style));
        }
Example #15
0
        public static void WriteCollection <TKey, TValue>(this IConsole console, IList <KeyValuePair <TKey, TValue> > collection, CollectionWriteStyle style)
        {
            if (collection?.Any() != true)
            {
                throw new ArgumentException("Collection to display must be populated");
            }

            var writableCollection = collection.ToDictionary(key => $"[{key.Key}] ", value => value.Value?.ToString()).ToList();

            switch (style)
            {
            case CollectionWriteStyle.Rows:
                WriteCollectionInternal(console, writableCollection, (x) => console.WriteLine(x));
                break;

            case CollectionWriteStyle.Inline:
                WriteCollectionInternal(console, writableCollection, (x) => console.Write(x + " "));
                break;

            case CollectionWriteStyle.Columns:
                WriteCollectionColumns(console, writableCollection);
                break;

            default:
                throw new ArgumentException("Cannot write collection with style: " + style);
            }
        }
Example #16
0
        public static T[] PromptIndexSelections <T>(this IConsole console, IEnumerable <T> source, Func <T, object> descriptionSelector, bool allowEmpty, CollectionWriteStyle style)
        {
            var indexCollection = new MultipleIndexCollection <T>(source, descriptionSelector, allowEmpty);

            return(indexCollection.GetSelection(console, style));
        }
 protected abstract TSelection GetSelection <TKey, TValue>(
     IDictionary <TKey, TValue> source,
     Func <TKey, TValue, object> descriptionSelector = null,
     bool isOptional            = false,
     CollectionWriteStyle style = CollectionWriteStyle.Rows);
Example #18
0
 public static void WriteCollection <TKey, TValue>(this IConsole console, IDictionary <TKey, TValue> collection, CollectionWriteStyle style)
 => console.WriteCollection(collection?.ToArray(), style);
Example #19
0
        public static T PromptIndexSelection <T>(this IConsole console, IEnumerable <T> source, CollectionWriteStyle style)
        {
            var indexCollection = new IndexCollection <T>(source);

            return(indexCollection.GetSelection(console, style));
        }