Example #1
0
        public void WriteAggregatedValues(CancellationToken cancellationToken)
        {
            int count = 0;

            IEnumerable <string> values = Storage.Values;

            if (Sections?.Count > 1 &&
                (ModifyOptions.HasFunction(ModifyFunctions.Except_Intersect)))
            {
                if (ModifyOptions.HasFunction(ModifyFunctions.Except))
                {
                    values = ExceptOrIntersect(Storage.Values, (x, y, c) => x.Except(y, c));
                }
                else if (ModifyOptions.HasFunction(ModifyFunctions.Intersect))
                {
                    values = ExceptOrIntersect(Storage.Values, (x, y, c) => x.Intersect(y, c));
                }
            }

            Dictionary <string, List <StorageSection> >?valuesMap = null;

            if (Sections?.Count > 0 &&
                ModifyOptions.HasFunction(ModifyFunctions.Group))
            {
                if (Options.ContentFilter != null)
                {
                    valuesMap = GroupByValues(Storage.Values);
                }
                else
                {
                    valuesMap = Storage.Values
                                .Zip(Sections)
                                .GroupBy(f => f.First)
                                .ToDictionary(f => f.Key, f => f.Select(f => f.Second).ToList());
                }

                values = valuesMap
                         .Select(f => f.Key)
                         .Modify(ModifyOptions, filter: ModifyFunctions.Enumerable & ~ModifyFunctions.Distinct);
            }
            else
            {
                values = values.Modify(ModifyOptions, filter: ModifyFunctions.Enumerable);
            }

            using (IEnumerator <string> en = values.GetEnumerator())
            {
                if (en.MoveNext())
                {
                    OutputSymbols symbols = OutputSymbols.Create(Options.HighlightOptions);

                    ConsoleColors colors = ((Options.HighlightOptions & HighlightOptions.Match) != 0)
                        ? Colors.Match
                        : default;

                    ConsoleColors boundaryColors = (Options.HighlightBoundary) ? Colors.MatchBoundary : default;
                    var           valueWriter    = new ValueWriter(new ContentTextWriter(Verbosity.Minimal), includeEndingIndent: false);

                    if (!Options.AggregateOnly)
                    {
                        ConsoleOut.WriteLineIf(ShouldWriteLine(ConsoleOut.Verbosity));
                        Out?.WriteLineIf(ShouldWriteLine(Out.Verbosity));
                    }

                    do
                    {
                        cancellationToken.ThrowIfCancellationRequested();

                        valueWriter.Write(en.Current, symbols, colors, boundaryColors);

                        if (valuesMap != null &&
                            ShouldLog(Verbosity.Detailed))
                        {
                            int groupCount = valuesMap[en.Current].Count;

                            Write("  ", Colors.Message_OK, Verbosity.Detailed);
                            Write(groupCount.ToString("n0"), Colors.Message_OK, Verbosity.Detailed);
                        }

                        WriteLine(Verbosity.Minimal);

                        if (valuesMap != null)
                        {
                            WriteGroup(valuesMap[en.Current], cancellationToken);
                        }

                        count++;
                    } while (en.MoveNext());

                    if (Options.IncludeSummary ||
                        ShouldLog(Verbosity.Detailed))
                    {
                        Verbosity verbosity = (Options.IncludeSummary)
                            ? Verbosity.Minimal
                            : Verbosity.Detailed;

                        if (valuesMap != null)
                        {
                            count = valuesMap.Sum(f => f.Value.Count);
                        }

                        WriteLine(verbosity);

                        if (valuesMap != null)
                        {
                            WriteCount("Groups", valuesMap.Count, verbosity: verbosity);
                            Write("  ", verbosity);
                        }

                        WriteCount("Values", count, verbosity: verbosity);
                        WriteLine(verbosity);
                    }
                }
            }

            bool ShouldWriteLine(Verbosity verbosity)
            {
                if (verbosity > Verbosity.Minimal)
                {
                    return(true);
                }

                return(verbosity == Verbosity.Minimal &&
                       (Options.PathDisplayStyle != PathDisplayStyle.Omit || Options.IncludeSummary));
            }
        }
Example #2
0
        private void WriteAggregatedValues()
        {
            int           count         = 0;
            ModifyOptions modifyOptions = Options.ModifyOptions;
            List <string> allValues     = ((ListResultStorage)_storage).Values;

            if (_storageIndexes?.Count > 1)
            {
                Debug.Assert((modifyOptions.Functions & ModifyFunctions.ExceptIntersect) != 0, modifyOptions.Functions.ToString());

                if ((modifyOptions.Functions & ModifyFunctions.Except) != 0)
                {
                    if (_storageIndexes.Count > 2)
                    {
                        throw new InvalidOperationException($"'Except' operation cannot be applied on more than two {((ContentFilter != null) ? "files" : "directories")}.");
                    }

                    int index = _storageIndexes[0];

                    allValues = allValues
                                .Take(index)
                                .Except(GetRange(index, allValues.Count))
                                .ToList();
                }
                else if ((modifyOptions.Functions & ModifyFunctions.Intersect) != 0)
                {
                    allValues = Intersect();
                }
            }

            using (IEnumerator <string> en = allValues
                                             .Modify(modifyOptions, filter: ModifyFunctions.Enumerable)
                                             .GetEnumerator())
            {
                if (en.MoveNext())
                {
                    OutputSymbols symbols        = OutputSymbols.Create(Options.HighlightOptions);
                    ConsoleColors colors         = ((Options.HighlightOptions & HighlightOptions.Match) != 0) ? Colors.Match : default;
                    ConsoleColors boundaryColors = (Options.HighlightBoundary) ? Colors.MatchBoundary : default;
                    var           valueWriter    = new ValueWriter(new ContentTextWriter(Verbosity.Minimal), includeEndingIndent: false);

                    ConsoleOut.WriteLineIf(ShouldWriteLine(ConsoleOut.Verbosity));
                    Out?.WriteLineIf(ShouldWriteLine(Out.Verbosity));

                    do
                    {
                        valueWriter.Write(en.Current, symbols, colors, boundaryColors);
                        WriteLine(Verbosity.Minimal);
                        count++;
                    } while (en.MoveNext());

                    if (ShouldLog(Verbosity.Detailed) ||
                        Options.IncludeSummary)
                    {
                        WriteLine(Verbosity.Minimal);
                        WriteCount("Values", count, verbosity: Verbosity.Minimal);
                        WriteLine(Verbosity.Minimal);
                    }
                }
            }

            List <string> Intersect()
            {
                var list = new List <string>(GetRange(0, _storageIndexes[0]));

                for (int i = 1; i < _storageIndexes.Count; i++)
                {
                    IEnumerable <string> second = GetRange(_storageIndexes[i - 1], _storageIndexes[i]);

                    list = list.Intersect(second, modifyOptions.StringComparer).ToList();
                }

                return(list);
            }

            IEnumerable <string> GetRange(int start, int end)
            {
                for (int i = start; i < end; i++)
                {
                    yield return(allValues[i]);
                }
            }

            bool ShouldWriteLine(Verbosity verbosity)
            {
                if (verbosity > Verbosity.Minimal)
                {
                    return(true);
                }

                return(verbosity == Verbosity.Minimal &&
                       (Options.PathDisplayStyle != PathDisplayStyle.Omit || Options.IncludeSummary));
            }
        }