/// <summary> /// Append the context lines to the rtf output. /// </summary> /// <param name="builder">String builder to append the context lines to</param> /// <param name="contextLines">The context lines to append</param> /// <param name="resultIndexInFile">The 0 based index of the result in the file</param> /// <param name="maxLineWidth">The longest line, used for padding</param> private static void appendContextLines(StringBuilder builder, List <LineText> contextLines, int resultIndexInFile, int maxLineWidth) { ColorSettings colors = Settings.get().Colors; contextLines = contextLines.OrderBy(cl => cl.LineNumber).ToList(); foreach (LineText contextLine in contextLines) { bool useAltColor = resultIndexInFile % 2 == 1; append(builder, colors.ContextLineNumText.getColor(useAltColor), colors.ContextLineNumBack.getColor(useAltColor), @" {0}: ", contextLine.LineNumber.ToString(), 0); append(builder, colors.ContextText.getColor(useAltColor), colors.ContextBack.getColor(useAltColor), @" {0}\par", contextLine.Text, maxLineWidth); } }
public static String createRtf(SearchResultList results, bool filenameSearch, Regex regex) { ColorSettings colors = Settings.get().Colors; StringBuilder builder = new StringBuilder(); if (filenameSearch || results.FileName.StartsWith("Error")) { append(builder, colors.NormalText.getColor(), colors.NormalBack.getColor(), @"{0}\par", results.FileName, 0); } else { append(builder, colors.FileText.getColor(), colors.FileBack.getColor(), @"{0}\par", results.FileName, 0); } results.removeExtraContext(); for (int resultIndex = 0; resultIndex < results.Results.Count; ++resultIndex) { bool useAltColor = Settings.get().ContextLineCount > 0 && resultIndex % 2 == 1; SearchResult result = results.Results[resultIndex]; int maxLineWidth = result.getMaxWidth(); // Print context lines appendContextLines(builder, result.BeforeContext, resultIndex, maxLineWidth); MatchCollection matches = regex.Matches(result.MatchLine.Text); int index = 0; append(builder, colors.LineNumText.getColor(useAltColor), colors.LineNumBack.getColor(useAltColor), @" {0}: ", result.MatchLine.LineNumber.ToString(), 0); for (int mi = 0; mi < matches.Count; mi++) { append(builder, colors.NormalText.getColor(useAltColor), colors.NormalBack.getColor(useAltColor), @" {0}", result.MatchLine.Text.Substring(index, matches[mi].Index - index), 0); append(builder, colors.MatchHighText.getColor(useAltColor), colors.MatchHighBack.getColor(useAltColor), @" {0}", result.MatchLine.Text.Substring(matches[mi].Index, matches[mi].Length), 0); index = matches[mi].Index + matches[mi].Length; } append(builder, colors.NormalText.getColor(useAltColor), colors.NormalBack.getColor(useAltColor), @" {0} \par", result.MatchLine.Text.Substring(index), maxLineWidth - index); // Print context lines appendContextLines(builder, result.AfterContext, resultIndex, maxLineWidth); } return(builder.ToString().Replace("\0", "").Replace("\n", "").Replace("\r", "")); }