Ejemplo n.º 1
0
        public void WriteValues(string indent, SearchTelemetry telemetry, AggregateManager?aggregate)
        {
            ConsoleColors colors = default;

            if (Options.HighlightMatch)
            {
                if (Options.ContentDisplayStyle == ContentDisplayStyle.Value ||
                    Options.ContentDisplayStyle == ContentDisplayStyle.ValueDetail)
                {
                    colors = Colors.Match;
                }
            }

            var valueWriter = new ValueWriter(
                ContentTextWriter.Default,
                indent,
                includeEndingIndent: false);

            foreach (string value in FileValues !.Modify(Options.ModifyOptions))
            {
                Write(indent, Verbosity.Normal);
                valueWriter.Write(value, Symbols, colors, Colors.MatchBoundary);
                WriteLine(Verbosity.Normal);

                aggregate?.Storage.Add(value);
                telemetry.MatchCount++;
            }
        }
Ejemplo n.º 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));
            }
        }
Ejemplo n.º 3
0
        private void WriteMatches(
            string input,
            Match match,
            ContentWriterOptions writerOptions,
            SearchContext context)
        {
            SearchTelemetry telemetry = context.Telemetry;

            ContentWriter  contentWriter = null;
            List <Capture> captures      = null;

            try
            {
                captures = ListCache <Capture> .GetInstance();

                GetCaptures(match, writerOptions.GroupNumber, context, isPathWritten: !Options.OmitPath, predicate: ContentFilter.Predicate, captures: captures);

                bool hasAnyFunction = Options.ModifyOptions.HasAnyFunction;

                if (hasAnyFunction ||
                    Options.AskMode == AskMode.Value ||
                    ShouldLog(Verbosity.Normal))
                {
                    if (hasAnyFunction)
                    {
                        if (_fileValues == null)
                        {
                            _fileValues = new List <string>();
                        }
                        else
                        {
                            _fileValues.Clear();
                        }

                        if (_fileStorage == null)
                        {
                            _fileStorage = new ListResultStorage(_fileValues);
                        }
                    }

                    contentWriter = ContentWriter.CreateFind(
                        contentDisplayStyle: Options.ContentDisplayStyle,
                        input: input,
                        options: writerOptions,
                        storage: (hasAnyFunction) ? _fileStorage : _storage,
                        outputInfo: Options.CreateOutputInfo(input, match),
                        writer: (hasAnyFunction) ? null : ContentTextWriter.Default,
                        ask: _askMode == AskMode.Value);
                }
                else
                {
                    contentWriter = new EmptyContentWriter(null, writerOptions);
                }

                WriteMatches(contentWriter, captures, context);

                if (hasAnyFunction)
                {
                    ConsoleColors colors         = (Options.HighlightMatch) ? Colors.Match : default;
                    ConsoleColors boundaryColors = (Options.HighlightBoundary) ? Colors.MatchBoundary : default;

                    var valueWriter = new ValueWriter(ContentTextWriter.Default, writerOptions.Indent, includeEndingIndent: false);

                    foreach (string value in _fileValues.Modify(Options.ModifyOptions))
                    {
                        Write(writerOptions.Indent, Verbosity.Normal);
                        valueWriter.Write(value, Symbols, colors, boundaryColors);
                        WriteLine(Verbosity.Normal);

                        _storage?.Add(value);
                        telemetry.MatchCount++;
                    }

                    _storageIndexes?.Add(_storage !.Count);
                }
                else
                {
                    telemetry.MatchCount += contentWriter.MatchCount;

                    if (contentWriter.MatchingLineCount >= 0)
                    {
                        if (telemetry.MatchingLineCount == -1)
                        {
                            telemetry.MatchingLineCount = 0;
                        }

                        telemetry.MatchingLineCount += contentWriter.MatchingLineCount;
                    }
                }

                if (_askMode == AskMode.Value)
                {
                    if (contentWriter is AskValueContentWriter askValueContentWriter)
                    {
                        if (!askValueContentWriter.Ask)
                        {
                            _askMode = AskMode.None;
                        }
                    }
                    else if (contentWriter is AskLineContentWriter askLineContentWriter)
                    {
                        if (!askLineContentWriter.Ask)
                        {
                            _askMode = AskMode.None;
                        }
                    }
                }
            }
            finally
            {
                contentWriter?.Dispose();

                if (captures != null)
                {
                    ListCache <Capture> .Free(captures);
                }
            }
        }