diff --git a/Grep Options Analysis.xlsx b/Grep Options Analysis.xlsx index e8d7fd0..1ab8910 100644 Binary files a/Grep Options Analysis.xlsx and b/Grep Options Analysis.xlsx differ diff --git a/Grepl.Tests/Commands/BasicCommands.cs b/Grepl.Tests/Commands/BasicCommands.cs index 9040c29..775eb23 100644 --- a/Grepl.Tests/Commands/BasicCommands.cs +++ b/Grepl.Tests/Commands/BasicCommands.cs @@ -93,6 +93,87 @@ public void Should_10_search_multiple_entries_per_line() } + [TestMethod] + public void Should_10_search_with_context() + { + CreateData("2"); + var r = GreplEntry("-e", "//3", "file1.txt", "-C", "1"); + Assert.AreEqual(0, r.Code); + + var raw = r.Output; + var exp = + @"//2 aaaa aaaa data aaaa +//3 bbbb aaaa +//4 cccc data bbbb data aaaa +"; + + CompareDetails(exp, raw); + + Assert.AreEqual(exp, raw); + + } + + [TestMethod] + public void Should_10_search_with_context_2() + { + CreateData("2"); + var r = GreplEntry("-e", "//3", "file1.txt", "-C", "2"); + Assert.AreEqual(0, r.Code); + + var raw = r.Output; + var exp = + @"//1 aaaa bbbb cccc dddd +//2 aaaa aaaa data aaaa +//3 bbbb aaaa +//4 cccc data bbbb data aaaa +//5 aaaa +"; + + CompareDetails(exp, raw); + + Assert.AreEqual(exp, raw); + + } + + [TestMethod] + public void Should_10_search_with_context_before_1() + { + CreateData("2"); + var r = GreplEntry("-e", "//3", "file1.txt", "-B", "1"); + Assert.AreEqual(0, r.Code); + + var raw = r.Output; + var exp = + @"//2 aaaa aaaa data aaaa +//3 bbbb aaaa +"; + + CompareDetails(exp, raw); + + Assert.AreEqual(exp, raw); + + } + + [TestMethod] + public void Should_10_search_with_context_before_2() + { + CreateData("2"); + var r = GreplEntry("-e", "//3", "file1.txt", "-B", "2"); + Assert.AreEqual(0, r.Code); + + var raw = r.Output; + var exp = + @"//1 aaaa bbbb cccc dddd +//2 aaaa aaaa data aaaa +//3 bbbb aaaa +"; + + CompareDetails(exp, raw); + + Assert.AreEqual(exp, raw); + + } + [TestMethod] public void Should_10_SearchRecursivelyProc() { diff --git a/Grepl/Executor.cs b/Grepl/Executor.cs index 12f746b..d34f733 100644 --- a/Grepl/Executor.cs +++ b/Grepl/Executor.cs @@ -33,10 +33,18 @@ class FilesSelectorOptions public List ExcludeGlobs { get; } = new List(); } + class ContextCaptureOptions + { + public int Before { get; set; } + public int After { get; set; } + public int ContextAround { get; set; } + } + public class Executor { internal OutputControlOptions OutputControlOptions { get; } = new OutputControlOptions(); internal FilesSelectorOptions FilesSelectorOptions { get; } = new FilesSelectorOptions(); + internal ContextCaptureOptions ContextCaptureOptions { get; } = new ContextCaptureOptions(); private bool Recursive => FilesSelectorOptions.Recursive; private const int FileBufferSize = 16 * 1024; @@ -290,6 +298,49 @@ void Process(string file, bool printFileName, Dictionary 0; before--) + { + if (prev >= 2) + { + prev = body.LastIndexOf('\n', prev - 2); + prev++; + if (!matchLines.ContainsKey(prev)) + { + matchLines[prev] = new Line + { + Start = prev, + }; + } + } + } + prev = line.Start; + var after = Math.Max(ContextCaptureOptions.After, ContextCaptureOptions.ContextAround); + for (; after > 0; after--) + { + prev = body.IndexOf('\n', prev); + if (prev > 0) + { + prev++; + if (!matchLines.ContainsKey(prev)) + { + matchLines[prev] = new Line + { + Start = prev, + }; + } + } + } + } + } foreach (var line in matchLines.Values) { diff --git a/Grepl/Grepl.cs b/Grepl/Grepl.cs index 647a981..b195729 100644 --- a/Grepl/Grepl.cs +++ b/Grepl/Grepl.cs @@ -132,7 +132,15 @@ public static int MainHandler(params string[] args) case "-include": executor.FilesSelectorOptions.IncludeGlobs.Add(args[++i]); break; - + case "A": + executor.ContextCaptureOptions.After = int.Parse(args[++i]); + break; + case "B": + executor.ContextCaptureOptions.Before = int.Parse(args[++i]); + break; + case "C": + executor.ContextCaptureOptions.ContextAround = int.Parse(args[++i]); + break; default: using (Color(ConsoleColor.Red)) {