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++; } }
public static void WriteSearchedFilesAndDirectories( SearchTelemetry telemetry, SearchTarget searchTarget, Verbosity verbosity = Verbosity.Detailed) { if (!ShouldLog(verbosity)) { return; } WriteLine(verbosity); if (searchTarget != SearchTarget.Directories) { WriteCount("Files searched", telemetry.FileCount, verbosity: verbosity); } if (searchTarget != SearchTarget.Files) { if (searchTarget != SearchTarget.Directories) { Write(" ", verbosity); } WriteCount("Directories searched", telemetry.DirectoryCount, verbosity: verbosity); } if (telemetry.FilesTotalSize > 0) { if (searchTarget == SearchTarget.Files) { WriteCount(" Files total size", telemetry.FilesTotalSize, verbosity: verbosity); } else if (searchTarget == SearchTarget.Directories) { WriteCount(" Directories total size", telemetry.FilesTotalSize, verbosity: verbosity); } else if (searchTarget == SearchTarget.All) { WriteCount(" Total size", telemetry.FilesTotalSize, verbosity: verbosity); } else { throw new InvalidOperationException(); } } if (telemetry.Elapsed != default) { Write($" Elapsed time: {telemetry.Elapsed:mm\\:ss\\.ff}", verbosity); } WriteLine(verbosity); }
protected override void WriteSummary(SearchTelemetry telemetry, Verbosity verbosity) { WriteSearchedFilesAndDirectories(telemetry, Options.SearchTarget, verbosity); if (!ShouldLog(verbosity)) { return; } WriteLine(verbosity); string matchCount = telemetry.MatchCount.ToString("n0"); string matchingFileCount = telemetry.MatchingFileCount.ToString("n0"); string processedMatchCount = telemetry.ProcessedMatchCount.ToString("n0"); string processedFileCount = telemetry.ProcessedFileCount.ToString("n0"); const string matchesTitle = "Matches"; const string matchingFilesTitle = "Matching files"; const string replacementsTitle = "Replacements"; const string replacedFilesTitle = "Replaced files"; int width1 = Math.Max(matchesTitle.Length, replacementsTitle.Length); int width2 = Math.Max(matchCount.Length, processedMatchCount.Length); int width3 = Math.Max(matchingFilesTitle.Length, replacedFilesTitle.Length); int width4 = Math.Max(matchingFileCount.Length, processedFileCount.Length); ConsoleColors colors = Colors.Message_OK; WriteCount(matchesTitle, matchCount, width1, width2, colors, verbosity); Write(" ", colors, verbosity); int matchingLinesWidth = 0; if (telemetry.MatchingLineCount >= 0) { matchingLinesWidth += WriteCount("Matching lines", telemetry.MatchingLineCount, colors, verbosity); Write(" ", colors, verbosity); matchingLinesWidth += 2; } WriteCount(matchingFilesTitle, matchingFileCount, width3, width4, colors, verbosity); WriteLine(verbosity); colors = (Options.DryRun) ? Colors.Message_DryRun : Colors.Message_Change; WriteCount(replacementsTitle, processedMatchCount, width1, width2, colors, verbosity); Write(" ", colors, verbosity); Write(' ', matchingLinesWidth, colors, verbosity); WriteCount(replacedFilesTitle, processedFileCount, width3, width4, colors, verbosity); WriteLine(verbosity); }
protected override void WriteSummary(SearchTelemetry telemetry, Verbosity verbosity) { WriteSearchedFilesAndDirectories(telemetry, Options.SearchTarget, verbosity); string filesTitle = (Options.ContentOnly) ? "Deleted files content" : "Deleted files"; string directoriesTitle = (Options.ContentOnly) ? "Deleted directories content" : "Deleted directories"; WriteProcessedFilesAndDirectories(telemetry, Options.SearchTarget, filesTitle, directoriesTitle, Options.DryRun, verbosity); }
protected override void WriteSummary(SearchTelemetry telemetry, Verbosity verbosity) { base.WriteSummary(telemetry, verbosity); ConsoleColors colors = (Options.DryRun) ? Colors.Message_DryRun : Colors.Message_Change; if (telemetry.ProcessedFileCount > 0) { LogHelpers.WriteCount("Moved files", telemetry.ProcessedFileCount, colors, verbosity); } if (telemetry.ProcessedDirectoryCount > 0) { if (telemetry.ProcessedFileCount > 0) { Logger.Write(" ", verbosity); } LogHelpers.WriteCount("Moved directories", telemetry.ProcessedDirectoryCount, colors, verbosity); } Logger.WriteLine(verbosity); }
protected abstract void WriteSummary(SearchTelemetry telemetry, Verbosity verbosity);
public static void WriteProcessedFilesAndDirectories( SearchTelemetry telemetry, SearchTarget searchTarget, string processedFilesTitle, string processedDirectoriesTitle, bool dryRun, Verbosity verbosity = Verbosity.Detailed) { if (!ShouldLog(verbosity)) { return; } WriteLine(verbosity); const string filesTitle = "Matching files"; const string directoriesTitle = "Matching directories"; bool files = searchTarget != SearchTarget.Directories; bool directories = searchTarget != SearchTarget.Files; string matchingFileCount = telemetry.MatchingFileCount.ToString("n0"); string matchingDirectoryCount = telemetry.MatchingDirectoryCount.ToString("n0"); string processedFileCount = telemetry.ProcessedFileCount.ToString("n0"); string processedDirectoryCount = telemetry.ProcessedDirectoryCount.ToString("n0"); int width1 = Math.Max(filesTitle.Length, processedFilesTitle.Length); int width2 = Math.Max(matchingFileCount.Length, processedFileCount.Length); int width3 = Math.Max(directoriesTitle.Length, processedDirectoriesTitle.Length); int width4 = Math.Max(matchingDirectoryCount.Length, processedDirectoryCount.Length); ConsoleColors colors = Colors.Message_OK; if (files) { WriteCount(filesTitle, matchingFileCount, width1, width2, colors, verbosity); } if (directories) { if (files) { Write(" ", colors, verbosity); } WriteCount(directoriesTitle, matchingDirectoryCount, width3, width4, colors, verbosity); } WriteLine(verbosity); colors = (dryRun) ? Colors.Message_DryRun : Colors.Message_Change; if (files) { WriteCount(processedFilesTitle, processedFileCount, width1, width2, colors, verbosity); } if (directories) { if (files) { Write(" ", colors, verbosity); } WriteCount(processedDirectoriesTitle, processedDirectoryCount, width3, width4, colors, verbosity); } WriteLine(verbosity); }
private void ReplaceMatches( string filePath, Encoding encoding, string input, Match match, string indent, ContentWriterOptions writerOptions, SearchContext context) { SearchTelemetry telemetry = context.Telemetry; ContentWriter contentWriter = null; TextWriter textWriter = null; List <Capture> groups = null; try { groups = ListCache <Capture> .GetInstance(); GetCaptures(match, writerOptions.GroupNumber, context, isPathWritten: !Options.OmitPath, predicate: Options.ContentFilter.Predicate, captures: groups); int fileMatchCount = 0; int fileReplacementCount = 0; if (!Options.DryRun) { if (Options.AskMode == AskMode.File) { textWriter = new StringWriter(); } else if (Options.AskMode == AskMode.None) { textWriter = new StreamWriter(filePath, false, encoding); } } if (Options.AskMode == AskMode.Value || ShouldLog(Verbosity.Normal)) { MatchOutputInfo outputInfo = Options.CreateOutputInfo(input, match); if (Options.AskMode == AskMode.Value) { Lazy <TextWriter> lazyWriter = (Options.DryRun) ? null : new Lazy <TextWriter>(() => new StreamWriter(filePath, false, encoding)); contentWriter = AskReplacementWriter.Create(Options.ContentDisplayStyle, input, Options.ReplaceOptions, lazyWriter, writerOptions, outputInfo); } else { contentWriter = ContentWriter.CreateReplace(Options.ContentDisplayStyle, input, Options.ReplaceOptions, writerOptions, textWriter, outputInfo); } } else if (Options.DryRun) { contentWriter = new EmptyContentWriter(null, FileWriterOptions); } else { contentWriter = new TextWriterContentWriter(input, Options.ReplaceOptions, textWriter, writerOptions); } WriteMatches(contentWriter, groups, context); fileMatchCount = contentWriter.MatchCount; fileReplacementCount = (contentWriter is AskReplacementWriter askReplacementWriter) ? askReplacementWriter.ReplacementCount : fileMatchCount; telemetry.MatchCount += fileMatchCount; if (contentWriter.MatchingLineCount >= 0) { if (telemetry.MatchingLineCount == -1) { telemetry.MatchingLineCount = 0; } telemetry.MatchingLineCount += contentWriter.MatchingLineCount; } if (Options.AskMode == AskMode.File) { if (context.TerminationReason == TerminationReason.Canceled) { fileReplacementCount = 0; } else { try { if (Options.DryRun) { if (ConsoleHelpers.Question("Continue without asking?", indent)) { Options.AskMode = AskMode.None; } } else if (ConsoleHelpers.Question("Replace content?", indent)) { File.WriteAllText(filePath, textWriter !.ToString(), encoding); } else { fileReplacementCount = 0; } } catch (OperationCanceledException) { context.TerminationReason = TerminationReason.Canceled; fileReplacementCount = 0; } } } else if (Options.AskMode == AskMode.Value) { if (((AskReplacementWriter)contentWriter).ContinueWithoutAsking) { Options.AskMode = AskMode.None; } } telemetry.ProcessedMatchCount += fileReplacementCount; if (fileReplacementCount > 0) { telemetry.ProcessedFileCount++; } } catch (Exception ex) when(ex is IOException || ex is UnauthorizedAccessException) { WriteFileError(ex, indent: indent); } finally { contentWriter?.Dispose(); if (groups != null) { ListCache <Capture> .Free(groups); } } }
protected override void ExecuteMatchWithContentCore( FileMatch fileMatch, SearchContext context, ContentWriterOptions writerOptions, string?baseDirectoryPath = null, ColumnWidths?columnWidths = null) { string indent = GetPathIndent(baseDirectoryPath); if (!Options.OmitPath) { WritePath(context, fileMatch, baseDirectoryPath, indent, columnWidths); } SearchTelemetry telemetry = context.Telemetry; ContentWriter? contentWriter = null; TextWriter? textWriter = null; List <Capture>?groups = null; try { groups = ListCache <Capture> .GetInstance(); GetCaptures(fileMatch.ContentMatch !, writerOptions.GroupNumber, context, isPathWritten: !Options.OmitPath, predicate: Options.ContentFilter !.Predicate, captures: groups); int fileMatchCount = 0; int fileReplacementCount = 0; if (!Options.DryRun) { if (Options.AskMode == AskMode.File) { textWriter = new StringWriter(); } else if (Options.AskMode == AskMode.None) { textWriter = new StreamWriter(fileMatch.Path, false, fileMatch.Encoding); } } if (Options.AskMode == AskMode.Value || ShouldLog(Verbosity.Normal)) { MatchOutputInfo?outputInfo = Options.CreateOutputInfo(fileMatch.ContentText, fileMatch.ContentMatch !, ContentFilter !); if (Options.AskMode == AskMode.Value) { Lazy <TextWriter>?lazyWriter = (Options.DryRun) ? null : new Lazy <TextWriter>(() => new StreamWriter(fileMatch.Path, false, fileMatch.Encoding)); contentWriter = AskReplacementWriter.Create(Options.ContentDisplayStyle, fileMatch.ContentText, Options.ReplaceOptions, lazyWriter, writerOptions, outputInfo); } else { contentWriter = ContentWriter.CreateReplace(Options.ContentDisplayStyle, fileMatch.ContentText, Options.ReplaceOptions, writerOptions, textWriter, outputInfo); } } else if (Options.DryRun) { contentWriter = new EmptyContentWriter(FileWriterOptions); } else { contentWriter = new TextWriterContentWriter(fileMatch.ContentText, Options.ReplaceOptions, textWriter !, writerOptions); } WriteMatches(contentWriter, groups, context); fileMatchCount = contentWriter.MatchCount; fileReplacementCount = (contentWriter is AskReplacementWriter askReplacementWriter) ? askReplacementWriter.ReplacementCount : fileMatchCount; telemetry.MatchCount += fileMatchCount; if (contentWriter.MatchingLineCount >= 0) { if (telemetry.MatchingLineCount == -1) { telemetry.MatchingLineCount = 0; } telemetry.MatchingLineCount += contentWriter.MatchingLineCount; } if (Options.AskMode == AskMode.File) { if (context.TerminationReason == TerminationReason.Canceled) { fileReplacementCount = 0; } else { try { if (Options.DryRun) { if (ConsoleHelpers.AskToContinue(indent)) { Options.AskMode = AskMode.None; } } else if (ConsoleHelpers.AskToExecute("Replace content?", indent)) { File.WriteAllText(fileMatch.Path, textWriter !.ToString(), fileMatch.Encoding); } else { fileReplacementCount = 0; } } catch (OperationCanceledException) { context.TerminationReason = TerminationReason.Canceled; fileReplacementCount = 0; } } } else if (Options.AskMode == AskMode.Value) { if (((AskReplacementWriter)contentWriter).ContinueWithoutAsking) { Options.AskMode = AskMode.None; } } telemetry.ProcessedMatchCount += fileReplacementCount; if (fileReplacementCount > 0) { telemetry.ProcessedFileCount++; } } catch (Exception ex) when(ex is IOException || ex is UnauthorizedAccessException) { WriteFileError(ex, indent: indent); } finally { contentWriter?.Dispose(); if (groups != null) { ListCache <Capture> .Free(groups); } } }
public SearchContext( SearchTelemetry telemetry, ProgressReporter progress = null, List <SearchResult> results = null, in CancellationToken cancellationToken = default)
private void ExecuteMatchWithContentCore( IEnumerator <ICapture> groups, int count, MaxReason maxReason, FileMatch fileMatch, SearchContext context, ContentWriterOptions writerOptions, string?baseDirectoryPath = null, ColumnWidths?columnWidths = null) { string indent = GetPathIndent(baseDirectoryPath); if (!Options.OmitPath) { WritePath(context, fileMatch, baseDirectoryPath, indent, columnWidths, includeNewline: false); WriteFilePathEnd(count, maxReason, Options.IncludeCount); } SearchTelemetry telemetry = context.Telemetry; ContentWriter?contentWriter = null; TextWriter? textWriter = null; try { int fileMatchCount = 0; int fileReplacementCount = 0; if (!Options.DryRun) { if (Options.AskMode == AskMode.File) { textWriter = new StringWriter(); } else if (Options.AskMode != AskMode.Value && !Options.Interactive) { textWriter = (CanUseStreamWriter) ? new StreamWriter(fileMatch.Path, false, fileMatch.Encoding) : new StringWriter(); } } if (Options.AskMode == AskMode.Value || Options.Interactive || (!Options.OmitContent && ShouldLog(Verbosity.Normal))) { MatchOutputInfo?outputInfo = Options.CreateOutputInfo( fileMatch.ContentText, fileMatch.ContentMatch !, ContentFilter !); if (Options.AskMode == AskMode.Value || Options.Interactive) { Lazy <TextWriter>?lazyWriter = null; if (!Options.DryRun) { lazyWriter = new Lazy <TextWriter>(() => { textWriter = new StringWriter(); return(textWriter); }); } contentWriter = AskReplacementWriter.Create( Options.ContentDisplayStyle, fileMatch.ContentText, Options.Replacer, lazyWriter, writerOptions, outputInfo, isInteractive: Options.Interactive, SpellcheckState); } else { contentWriter = CreateReplacementWriter( Options.ContentDisplayStyle, fileMatch.ContentText, Options.Replacer, writerOptions, textWriter, outputInfo); } } else if (Options.DryRun && CanUseEmptyWriter) { contentWriter = new EmptyContentWriter(FileWriterOptions); } else { contentWriter = new TextWriterContentWriter( fileMatch.ContentText, Options.Replacer, writerOptions, textWriter, SpellcheckState); } WriteMatches(contentWriter, groups, context); fileMatchCount = contentWriter.MatchCount; fileReplacementCount = (contentWriter is IReportReplacement reportReplacement) ? reportReplacement.ReplacementCount : fileMatchCount; telemetry.MatchCount += fileMatchCount; if (contentWriter.MatchingLineCount >= 0) { if (telemetry.MatchingLineCount == -1) { telemetry.MatchingLineCount = 0; } telemetry.MatchingLineCount += contentWriter.MatchingLineCount; } if (Options.AskMode == AskMode.Value || Options.Interactive) { if (textWriter != null) { File.WriteAllText(fileMatch.Path, textWriter.ToString(), fileMatch.Encoding); if (Options.AskMode == AskMode.Value && ((AskReplacementWriter)contentWriter).ContinueWithoutAsking) { Options.AskMode = AskMode.None; } } } else if (Options.AskMode == AskMode.File) { if (context.TerminationReason == TerminationReason.Canceled) { fileReplacementCount = 0; } else { try { if (Options.DryRun) { if (ConsoleHelpers.AskToContinue(indent) == DialogResult.YesToAll) { Options.AskMode = AskMode.None; } } else if (fileReplacementCount > 0 && ConsoleHelpers.AskToExecute("Replace content?", indent)) { File.WriteAllText(fileMatch.Path, textWriter !.ToString(), fileMatch.Encoding); } else { fileReplacementCount = 0; } } catch (OperationCanceledException) { context.TerminationReason = TerminationReason.Canceled; fileReplacementCount = 0; } } } else if (fileReplacementCount > 0 && textWriter is StringWriter) { File.WriteAllText(fileMatch.Path, textWriter.ToString(), fileMatch.Encoding); } telemetry.ProcessedMatchCount += fileReplacementCount; if (fileReplacementCount > 0) { telemetry.ProcessedFileCount++; } } catch (Exception ex) when(ex is IOException || ex is UnauthorizedAccessException) { WriteFileError(ex, indent: indent); } finally { contentWriter?.Dispose(); } }
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); } } }