public FormattedGrepLine(GrepLine line, FormattedGrepResult parent, int initialColumnWidth, bool breakSection) { Parent = parent; GrepLine = line; Parent.PropertyChanged += Parent_PropertyChanged; LineNumberColumnWidth = initialColumnWidth; IsSectionBreak = breakSection; WrapText = Parent.WrapText; int lineSize = GrepSettings.Instance.Get <int>(GrepSettings.Key.HexResultByteLength); LineNumberAlignment = TranslationSource.Instance.CurrentCulture.TextInfo.IsRightToLeft ? TextAlignment.Left : TextAlignment.Right; FormattedLineNumber = line.LineNumber == -1 ? string.Empty : line.IsHexFile ? string.Format("{0:X8}", (line.LineNumber - 1) * lineSize) : line.LineNumber.ToString(); //string fullText = lineSummary; if (line.IsContext) { Style = "Context"; } if (line.LineNumber == -1 && string.IsNullOrEmpty(line.LineText)) { Style = "Empty"; } }
private string FormatHexValues(GrepLine grepLine) { string[] parts = grepLine.LineText.TrimEnd().Split(' '); List <byte> list = new List <byte>(); foreach (string num in parts) { if (byte.TryParse(num, System.Globalization.NumberStyles.HexNumber, null, out byte result)) { list.Add(result); } } string text = Parent.GrepResult.Encoding.GetString(list.ToArray()); List <char> nonPrintableChars = new List <char>(); for (int idx = 0; idx < text.Length; idx++) { if (!char.IsLetterOrDigit(text[idx]) && !char.IsPunctuation(text[idx]) && text[idx] != ' ') { nonPrintableChars.Add(text[idx]); } } foreach (char c in nonPrintableChars) { text = text.Replace(c, '.'); } return(text); }
public LazyResultsList(GrepSearchResult result, FormattedGrepResult formattedResult) { this.result = result; this.formattedResult = formattedResult; if ((result.Matches != null && result.Matches.Count > 0) || !result.IsSuccess) { GrepLine emptyLine = new GrepLine(-1, "", true, null); var dummyLine = new FormattedGrepLine(emptyLine, formattedResult, 30, false); Add(dummyLine); IsLoaded = false; } }
public FormattedGrepLine(GrepLine line, FormattedGrepResult parent, int initialColumnWidth, bool breakSection) { Parent = parent; GrepLine = line; Parent.PropertyChanged += new PropertyChangedEventHandler(Parent_PropertyChanged); LineNumberColumnWidth = initialColumnWidth; IsSectionBreak = breakSection; FormattedLineNumber = (line.LineNumber == -1 ? "" : line.LineNumber.ToString()); //string fullText = lineSummary; if (line.IsContext) { Style = "Context"; } if (line.LineNumber == -1 && line.LineText == "") { Style = "Empty"; } }
private InlineCollection FormatLine(GrepLine line) { Paragraph paragraph = new Paragraph(); string fullLine = line.LineText; if (line.LineText.Length > MaxLineLength) { fullLine = line.LineText.Substring(0, MaxLineLength); } if (line.Matches.Count == 0) { Run mainRun = new Run(fullLine); paragraph.Inlines.Add(mainRun); } else { int counter = 0; GrepMatch[] lineMatches = new GrepMatch[line.Matches.Count]; line.Matches.CopyTo(lineMatches); foreach (GrepMatch m in lineMatches) { Parent.MatchIdx++; try { string regLine = null; string fmtLine = null; if (m.StartLocation < fullLine.Length) { regLine = fullLine.Substring(counter, m.StartLocation - counter); } if (m.StartLocation + m.Length <= fullLine.Length) { fmtLine = fullLine.Substring(m.StartLocation, m.Length); } else if (fullLine.Length > m.StartLocation) { // match may include the non-printing newline chars at the end of the line: don't overflow the length fmtLine = fullLine.Substring(m.StartLocation, fullLine.Length - m.StartLocation); } else { // binary file? regLine = fullLine; } if (regLine != null) { Run regularRun = new Run(regLine); paragraph.Inlines.Add(regularRun); } if (fmtLine != null) { if (HighlightCaptureGroups && m.Groups.Count > 0) { FormatCaptureGroups(paragraph, m, fmtLine); } else { var run = new Run(fmtLine); run.SetResourceReference(Run.ForegroundProperty, "Match.Highlight.Foreground"); run.SetResourceReference(Run.BackgroundProperty, "Match.Highlight.Background"); paragraph.Inlines.Add(run); } } else { break; } } catch { Run regularRun = new Run(fullLine); paragraph.Inlines.Add(regularRun); } finally { counter = m.StartLocation + m.Length; } } if (counter < fullLine.Length) { try { string regLine = fullLine.Substring(counter); Run regularRun = new Run(regLine); paragraph.Inlines.Add(regularRun); } catch { Run regularRun = new Run(fullLine); paragraph.Inlines.Add(regularRun); } } if (line.LineText.Length > MaxLineLength) { string msg = TranslationSource.Format(Resources.Main_ResultList_CountAdditionalCharacters, line.LineText.Length - MaxLineLength); var msgRun = new Run(msg); msgRun.SetResourceReference(Run.ForegroundProperty, "TreeView.Message.Highlight.Foreground"); msgRun.SetResourceReference(Run.BackgroundProperty, "TreeView.Message.Highlight.Background"); paragraph.Inlines.Add(msgRun); var hiddenMatches = line.Matches.Where(m => m.StartLocation > MaxLineLength).Select(m => m); int count = hiddenMatches.Count(); if (count > 0) { paragraph.Inlines.Add(new Run(" " + Resources.Main_ResultList_AdditionalMatches)); } // if close to getting them all, then take them all, // otherwise, stop at 20 and just show the remaining count int takeCount = count > 25 ? 20 : count; foreach (GrepMatch m in hiddenMatches.Take(takeCount)) { if (m.StartLocation + m.Length <= line.LineText.Length) { paragraph.Inlines.Add(new Run(" ")); string fmtLine = line.LineText.Substring(m.StartLocation, m.Length); var run = new Run(fmtLine); run.SetResourceReference(Run.ForegroundProperty, "Match.Highlight.Foreground"); run.SetResourceReference(Run.BackgroundProperty, "Match.Highlight.Background"); paragraph.Inlines.Add(run); if (m.StartLocation + m.Length == line.LineText.Length) { paragraph.Inlines.Add(new Run(" " + Resources.Main_ResultList_AtEndOfLine)); } else { paragraph.Inlines.Add(new Run(" " + TranslationSource.Format(Resources.Main_ResultList_AtPosition, m.StartLocation))); } } } if (count > takeCount) { paragraph.Inlines.Add(new Run(TranslationSource.Format(Resources.Main_ResultList_PlusCountMoreMatches, count - takeCount))); } } } return(paragraph.Inlines); }
public async Task <bool> LoadAsync() { if (IsLoaded || IsLoading) { return(false); } IsLoading = true; int currentLine = -1; List <GrepLine> linesWithContext = await Task.Run(() => { List <GrepLine> list = new List <GrepLine>(); if (GrepSettings.Instance.Get <bool>(GrepSettings.Key.ShowLinesInContext)) { list = result.GetLinesWithContext(GrepSettings.Instance.Get <int>(GrepSettings.Key.ContextLinesBefore), GrepSettings.Instance.Get <int>(GrepSettings.Key.ContextLinesAfter)); } else { list = result.GetLinesWithContext(0, 0); } return(list); }); if (this.Count == 1 && this[0].GrepLine.LineNumber == -1) { if (Application.Current != null) { Application.Current.Dispatcher.Invoke(new Action(() => this.Clear() )); } } List <FormattedGrepLine> tempList = new List <FormattedGrepLine>(); for (int i = 0; i < linesWithContext.Count; i++) { GrepLine line = linesWithContext[i]; bool isSectionBreak = false; // Adding separator if (tempList.Count > 0 && GrepSettings.Instance.Get <bool>(GrepSettings.Key.ShowLinesInContext) && (currentLine != line.LineNumber && currentLine + 1 != line.LineNumber)) { isSectionBreak = true; } currentLine = line.LineNumber; if (currentLine <= 999 && LineNumberColumnWidth < 30) { LineNumberColumnWidth = 30; } else if (currentLine > 999 && LineNumberColumnWidth < 35) { LineNumberColumnWidth = 35; } else if (currentLine > 9999 && LineNumberColumnWidth < 47) { LineNumberColumnWidth = 47; } else if (currentLine > 99999 && LineNumberColumnWidth < 50) { LineNumberColumnWidth = 50; } tempList.Add(new FormattedGrepLine(line, formattedResult, LineNumberColumnWidth, isSectionBreak)); } if (Application.Current != null) { Application.Current.Dispatcher.Invoke(new Action(() => { foreach (var l in tempList) { this.Add(l); } } )); } IsLoaded = true; IsLoading = false; LoadFinished?.Invoke(this, EventArgs.Empty); return(true); }
private InlineCollection FormatLine(GrepLine line) { Paragraph paragraph = new Paragraph(); const int MAX_LINE_LENGTH = 500; string fullLine = line.LineText; if (line.LineText.Length > MAX_LINE_LENGTH) { fullLine = line.LineText.Substring(0, MAX_LINE_LENGTH); } if (line.Matches.Count == 0) { Run mainRun = new Run(fullLine); paragraph.Inlines.Add(mainRun); } else { int counter = 0; GrepMatch[] lineMatches = new GrepMatch[line.Matches.Count]; line.Matches.CopyTo(lineMatches); foreach (GrepMatch m in lineMatches) { try { string regLine = null; string fmtLine = null; if (fullLine.Length < m.StartLocation + m.Length) { regLine = fullLine.Substring(counter, fullLine.Length - counter); } else { regLine = fullLine.Substring(counter, m.StartLocation - counter); fmtLine = fullLine.Substring(m.StartLocation, m.Length); } Run regularRun = new Run(regLine); paragraph.Inlines.Add(regularRun); if (fmtLine != null) { paragraph.Inlines.Add(new Run(fmtLine) { Background = Brushes.Yellow }); } else { break; } } catch { Run regularRun = new Run(fullLine); paragraph.Inlines.Add(regularRun); } finally { counter = m.StartLocation + m.Length; } } if (counter < fullLine.Length) { try { string regLine = fullLine.Substring(counter); Run regularRun = new Run(regLine); paragraph.Inlines.Add(regularRun); } catch { Run regularRun = new Run(fullLine); paragraph.Inlines.Add(regularRun); } } if (line.LineText.Length > MAX_LINE_LENGTH) { string msg = string.Format("...(+{0:n0} characters)", line.LineText.Length - MAX_LINE_LENGTH); paragraph.Inlines.Add(new Run(msg) { Background = Brushes.AliceBlue }); var hiddenMatches = line.Matches.Where(m => m.StartLocation > MAX_LINE_LENGTH).Select(m => m); int count = hiddenMatches.Count(); if (count > 0) { paragraph.Inlines.Add(new Run(" additional matches:")); } // if close to getting them all, then take them all, // otherwise, stop at 20 and just show the remaining count int takeCount = count > 25 ? 20 : count; foreach (GrepMatch m in hiddenMatches.Take(takeCount)) { if (m.StartLocation + m.Length <= line.LineText.Length) { paragraph.Inlines.Add(new Run(" ")); string fmtLine = line.LineText.Substring(m.StartLocation, m.Length); paragraph.Inlines.Add(new Run(fmtLine) { Background = Brushes.Yellow }); if (m.StartLocation + m.Length == line.LineText.Length) { paragraph.Inlines.Add(new Run(" (at end of line)")); } else { paragraph.Inlines.Add(new Run($" at position {m.StartLocation}")); } } } if (count > takeCount) { paragraph.Inlines.Add(new Run($", +{count - takeCount} more matches")); } } } return(paragraph.Inlines); }
private InlineCollection FormatLine(GrepLine line) { Paragraph paragraph = new Paragraph(); const int MAX_LINE_LENGTH = 500; string fullLine = line.LineText; if (line.LineText.Length > MAX_LINE_LENGTH) { fullLine = line.LineText.Substring(0, MAX_LINE_LENGTH); } if (line.Matches.Count == 0) { Run mainRun = new Run(fullLine); paragraph.Inlines.Add(mainRun); } else { int counter = 0; GrepMatch[] lineMatches = new GrepMatch[line.Matches.Count]; line.Matches.CopyTo(lineMatches); foreach (GrepMatch m in lineMatches) { Parent.MatchIdx++; try { string regLine = null; string fmtLine = null; if (m.StartLocation < fullLine.Length) { regLine = fullLine.Substring(counter, m.StartLocation - counter); } if (m.StartLocation + m.Length <= fullLine.Length) { fmtLine = fullLine.Substring(m.StartLocation, m.Length); } else { // match may include the non-printing newline chars at the end of the line: don't overflow the length fmtLine = fullLine.Substring(m.StartLocation, fullLine.Length - m.StartLocation); } if (regLine != null) { Run regularRun = new Run(regLine); paragraph.Inlines.Add(regularRun); } if (fmtLine != null) { if (HighlightCaptureGroups && m.Groups.Count > 0) { FormatCaptureGroups(paragraph, m, fmtLine); } else { var run = new Run(fmtLine); run.SetResourceReference(Run.ForegroundProperty, "Match.Highlight.Foreground"); run.SetResourceReference(Run.BackgroundProperty, "Match.Highlight.Background"); paragraph.Inlines.Add(run); } } else { break; } } catch { Run regularRun = new Run(fullLine); paragraph.Inlines.Add(regularRun); } finally { counter = m.StartLocation + m.Length; } } if (counter < fullLine.Length) { try { string regLine = fullLine.Substring(counter); Run regularRun = new Run(regLine); paragraph.Inlines.Add(regularRun); } catch { Run regularRun = new Run(fullLine); paragraph.Inlines.Add(regularRun); } } if (line.LineText.Length > MAX_LINE_LENGTH) { string msg = string.Format("...(+{0:n0} characters)", line.LineText.Length - MAX_LINE_LENGTH); var msgRun = new Run(msg); msgRun.SetResourceReference(Run.ForegroundProperty, "TreeView.Message.Highlight.Foreground"); msgRun.SetResourceReference(Run.BackgroundProperty, "TreeView.Message.Highlight.Background"); paragraph.Inlines.Add(msgRun); var hiddenMatches = line.Matches.Where(m => m.StartLocation > MAX_LINE_LENGTH).Select(m => m); int count = hiddenMatches.Count(); if (count > 0) { paragraph.Inlines.Add(new Run(" additional matches:")); } // if close to getting them all, then take them all, // otherwise, stop at 20 and just show the remaining count int takeCount = count > 25 ? 20 : count; foreach (GrepMatch m in hiddenMatches.Take(takeCount)) { if (m.StartLocation + m.Length <= line.LineText.Length) { paragraph.Inlines.Add(new Run(" ")); string fmtLine = line.LineText.Substring(m.StartLocation, m.Length); var run = new Run(fmtLine); run.SetResourceReference(Run.ForegroundProperty, "Match.Highlight.Foreground"); run.SetResourceReference(Run.BackgroundProperty, "Match.Highlight.Background"); paragraph.Inlines.Add(run); if (m.StartLocation + m.Length == line.LineText.Length) { paragraph.Inlines.Add(new Run(" (at end of line)")); } else { paragraph.Inlines.Add(new Run($" at position {m.StartLocation}")); } } } if (count > takeCount) { paragraph.Inlines.Add(new Run($", +{count - takeCount} more matches")); } } } return(paragraph.Inlines); }