Skip to content

Commit

Permalink
add context lines
Browse files Browse the repository at this point in the history
  • Loading branch information
gusarov committed Jul 19, 2020
1 parent fae93bb commit f543059
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 1 deletion.
81 changes: 81 additions & 0 deletions Grepl.Tests/Commands/BasicCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
51 changes: 51 additions & 0 deletions Grepl/Executor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,18 @@ class FilesSelectorOptions
public List<string> ExcludeGlobs { get; } = new List<string>();
}

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;
Expand Down Expand Up @@ -290,6 +298,49 @@ void Process(string file, bool printFileName, Dictionary<ColoredMessage, Capture

if (!OutputControlOptions.FilesWithMatches)
{
// fill context lines
if (ContextCaptureOptions.ContextAround != 0
|| ContextCaptureOptions.Before != 0
|| ContextCaptureOptions.After != 0)
{
foreach (var line in matchLines.Values.ToArray())
{
var prev = line.Start;
var before = Math.Max(ContextCaptureOptions.Before, ContextCaptureOptions.ContextAround);
for (; before > 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)
{
Expand Down
10 changes: 9 additions & 1 deletion Grepl/Grepl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down

0 comments on commit f543059

Please sign in to comment.