protected TSelection GetSelection( Func <string, string, object> descriptionSelector = null, bool isOptional = false, CollectionWriteStyle style = CollectionWriteStyle.Rows) { return(GetSelection(DefaultSource, descriptionSelector, isOptional, style)); }
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()); }
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()); }
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)); }
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()); }
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()); }
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)); }
protected abstract TResult GetSelectionInternal(IConsole console, CollectionWriteStyle style, IList <KeyValuePair <TKey, TEntity> > source, bool writeCollection);
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)); }
private void OnPromptInputSelection(CollectionWriteStyle style) { DisplaySelection(() => _console.PromptInputSelection(_source, style: style, isOptional: true)); }
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)); }
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); } }
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);
public static void WriteCollection <TKey, TValue>(this IConsole console, IDictionary <TKey, TValue> collection, CollectionWriteStyle style) => console.WriteCollection(collection?.ToArray(), style);
public static T PromptIndexSelection <T>(this IConsole console, IEnumerable <T> source, CollectionWriteStyle style) { var indexCollection = new IndexCollection <T>(source); return(indexCollection.GetSelection(console, style)); }