Skip to content

Commit

Permalink
add -l (--files-with-matches) option
Browse files Browse the repository at this point in the history
  • Loading branch information
gusarov committed Jul 16, 2020
1 parent 48ba70f commit bae4f11
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 42 deletions.
Binary file modified Grep Options Analysis.xlsx
Binary file not shown.
103 changes: 103 additions & 0 deletions Grepl.Tests/GrepCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,46 @@ some cat2
Assert.AreEqual("some data3\r\nqwe\n", File.ReadAllText($"dir1{_s}file.txt"));
}

[TestMethod]
public void Should_20_replace_to_empty()
{
CreateData();
var r = Grepl("data", "-r", "-$", "");
Assert.AreEqual(0, r.Code);

var raw = r.Output;
var exp = @"
dir1\dir11\file.txt
some data5
some 5
dir1\file.txt
some data3
some 3
dir2\file.txt
some data4
some 4
file1.txt
some data1
some 1
file2.txt
some data2
some 2
".Replace('\\', Path.DirectorySeparatorChar);

CompareDetails(exp, raw);


Assert.AreEqual(exp, raw);

Assert.AreEqual("some data1\r\ndef\r", File.ReadAllText("file1.txt"));
Assert.AreEqual("some data2\r\nabc\r\n", File.ReadAllText("file2.txt"));
Assert.AreEqual("some data3\r\nqwe\n", File.ReadAllText($"dir1{_s}file.txt"));
}

[TestMethod]
public void Should_30_ReplaceAndSave()
{
Expand Down Expand Up @@ -557,5 +597,68 @@ public void Should_30_read_from_console()

Assert.AreEqual(exp, raw);
}

[TestMethod]
public void Should_30_show_only_file_name()
{
CreateData("3");

var r = GreplProc("bbbb", "-l", "*");

Assert.AreEqual(0, r.Code);

var raw = r.Output;
var exp = @"file1.txt
file2.txt
";

CompareDetails(exp, raw);

Assert.AreEqual(exp, raw);
}

[TestMethod]
public void Should_30_show_only_file_name_replace()
{
CreateData("3");

var r = GreplProc("bbbb", "-l", "*", "-$", "xx");

Assert.AreEqual(0, r.Code);

var raw = r.Output;
var exp = @"file1.txt
file2.txt
";

CompareDetails(exp, raw);

Assert.AreEqual(exp, raw);
}


[TestMethod]
public void Should_30_show_only_file_name_replace_save()
{
CreateData("3");

var r = GreplProc("bbbb", "-l", "*", "-$", "xx", "--save");

Assert.AreEqual(0, r.Code);

var raw = r.Output;
var exp = @"file1.txt
file2.txt
";

CompareDetails(exp, raw);

Assert.AreEqual(exp, raw);

Assert.AreEqual("//1 aaaa\n//2 xx\n//3 cccc\n", File.ReadAllText("file1.txt").Replace("\r", ""), "file1");
Assert.AreEqual("//1 aaaa\n//2 xx\n//3 cccc\n", File.ReadAllText("file2.txt").Replace("\r", ""), "file2");
Assert.AreEqual("//1 aaaa\n//2 xbbb\n//3 cccc\n", File.ReadAllText("file3.txt").Replace("\r", ""), "file3");

}
}
}
104 changes: 62 additions & 42 deletions Grepl/Executor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,19 @@ namespace Grepl
{
using static Tools;

class OutputControlOptions
{
/// <summary>
/// L
/// file names only, no content
/// </summary>
public bool FilesWithMatches { get; set; }
}

public class Executor
{
internal OutputControlOptions OutputControlOptions { get; } = new OutputControlOptions();

private const int FileBufferSize = 16 * 1024;

public string ReplaceTo { get; set; }
Expand Down Expand Up @@ -143,7 +154,7 @@ private string ImprovePattern(string pattern)

void PrintFileName(string file)
{
if (!GroupMatchesByContext)
if (!GroupMatchesByContext && !OutputControlOptions.FilesWithMatches)
{
Console.WriteLine();
}
Expand Down Expand Up @@ -229,65 +240,71 @@ void Process(string file, bool printFileName, Dictionary<ColoredMessage, Capture
PrintFileName(file);
}

foreach (var line in matchLines.Values)
if (!OutputControlOptions.FilesWithMatches)
{
var eol = body.IndexOf('\n', line.Start);
if (eol == -1)
{
eol = body.Length - 1; // jump to last char
}
else
{
eol--; // jump to char before \n
}

// Windows CR LF file
if (body[eol] == '\r')
foreach (var line in matchLines.Values)
{
eol--; // jump one more time
}
var eol = body.IndexOf('\n', line.Start);
if (eol == -1)
{
eol = body.Length - 1; // jump to last char
}
else
{
eol--; // jump to char before \n
}

// var str = body.Substring(line.Key, eol);
void PrintLine(bool replaceMode = false)
{
var printPosition = line.Start;
foreach (var lineMatch in line.Matches.Values)
// Windows CR LF file
if (body[eol] == '\r')
{
var match = lineMatch.Match;
eol--; // jump one more time
}

if (match.Index > printPosition)
// var str = body.Substring(line.Key, eol);
void PrintLine(bool replaceMode = false)
{
var printPosition = line.Start;
foreach (var lineMatch in line.Matches.Values)
{
output.Write(ConsoleColor.Gray, body.Substring(printPosition, match.Index - printPosition));
var match = lineMatch.Match;

if (match.Index > printPosition)
{
output.Write(ConsoleColor.Gray,
body.Substring(printPosition, match.Index - printPosition));
}

if (!replaceMode)
{
output.Write(ConsoleColor.Red, match.Value);
}
else
{
output.Write(ConsoleColor.Green,
GetReplacementString(lineMatch.Regex, match, body, ReplaceTo));
}

printPosition = match.Index + match.Length;
}

if (!replaceMode)
var len = eol + 1 - printPosition;
if (len >= 0 && printPosition < body.Length && (len - printPosition) < body.Length)
{
output.Write(ConsoleColor.Red, match.Value);
output.Write(ConsoleColor.Gray, body.Substring(printPosition, len) + Environment.NewLine);
}
else
{
output.Write(ConsoleColor.Green, GetReplacementString(lineMatch.Regex, match, body, ReplaceTo));
output.Write(null, Environment.NewLine);
}

printPosition = match.Index + match.Length;
}

var len = eol + 1 - printPosition;
if (len >= 0 && printPosition < body.Length && (len - printPosition) < body.Length)
{
output.Write(ConsoleColor.Gray, body.Substring(printPosition, len) + Environment.NewLine);
}
else
PrintLine();
if (ReplaceTo != null)
{
output.Write(null, Environment.NewLine);
PrintLine(true);
}
}

PrintLine();
if (ReplaceTo != null)
{
PrintLine(true);
}
}

if (Save && matchLines.Any() && body != replaced)
Expand Down Expand Up @@ -321,7 +338,10 @@ void PrintLine(bool replaceMode = false)
else
{
// no context grouping, print directly now
output.ToConsole();
if (!OutputControlOptions.FilesWithMatches)
{
output.ToConsole();
}
}
}

Expand Down
16 changes: 16 additions & 0 deletions Grepl/Grep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ public static int MainHandler(params string[] args)
}
}
break;
case "-files-with-matches":
case "l":
executor.OutputControlOptions.FilesWithMatches = true;
break;
case "-save":
executor.Save = true;
break;
Expand Down Expand Up @@ -129,6 +133,18 @@ public static int MainHandler(params string[] args)
}
}

if (executor.OutputControlOptions.FilesWithMatches)
{
if (executor.GroupMatchesByContext)
{
using (Color(ConsoleColor.Red))
{
Console.WriteLine("Grouping by context and not displaying file content does not make sense. Consider only one of those options: -l or --group");
return 1;
}
}
}

foreach (var patternFile in patternsToLoad)
{
var pattern = LoadPattern(patternFile);
Expand Down

0 comments on commit bae4f11

Please sign in to comment.