Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] adding select-string -raw #8962

Closed
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,23 @@ public object Clone()
/// </summary>
public class MatchInfo
{
private readonly bool _isRaw;

/// <summary>
/// Default constructor.
/// </summary>
public MatchInfo()
{
}
TylerLeonhardt marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Constructor.
/// </summary>
internal MatchInfo(bool useRaw, bool useColor, int matchIndex, int matchLength)
{
this._isRaw = useRaw;
}
TylerLeonhardt marked this conversation as resolved.
Show resolved Hide resolved

private static readonly string s_inputStream = "InputStream";

/// <summary>
Expand Down Expand Up @@ -101,6 +118,7 @@ public class MatchInfo
/// </remarks>
/// </summary>
/// <value>The file name.</value>

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove these newlines

public string Filename
{
get
Expand Down Expand Up @@ -226,7 +244,15 @@ public string ToString(string directory)
// enable context-tracking.
if (Context == null)
{
return FormatLine(Line, this.LineNumber, displayPath, EmptyPrefix);
if(_isRaw)
{
return Line;
}
else
{
return FormatLine(Line, this.LineNumber, displayPath, EmptyPrefix);
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this newline

}

// Otherwise, render the full context.
Expand All @@ -235,14 +261,38 @@ public string ToString(string directory)
int displayLineNumber = this.LineNumber - Context.DisplayPreContext.Length;
foreach (string contextLine in Context.DisplayPreContext)
{
lines.Add(FormatLine(contextLine, displayLineNumber++, displayPath, ContextPrefix));

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this newline

if(_isRaw)
{
lines.Add(contextLine);
}
else
{
lines.Add(FormatLine(contextLine, displayLineNumber++, displayPath, ContextPrefix));
}
}

lines.Add(FormatLine(Line, displayLineNumber++, displayPath, MatchPrefix));

if(_isRaw)
{
lines.Add(Line);
}
else
{
lines.Add(FormatLine(Line, displayLineNumber++, displayPath, MatchPrefix));
}

foreach (string contextLine in Context.DisplayPostContext)
{
lines.Add(FormatLine(contextLine, displayLineNumber++, displayPath, ContextPrefix));

if(_isRaw)
{
lines.Add(contextLine);
}
else
{
lines.Add(FormatLine(contextLine, displayLineNumber++, displayPath, ContextPrefix));
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the newline here

}

return string.Join(System.Environment.NewLine, lines.ToArray());
Expand Down Expand Up @@ -1026,6 +1076,12 @@ public string[] LiteralPath
[Parameter]
public SwitchParameter SimpleMatch { get; set; }

/// <summary>
/// If true, emits plain strings. If false, emits MatchInfo objects.
/// </summary>
[Parameter]
public SwitchParameter Raw { get; set; }

/// <summary>
/// Gets or sets a value indicating if the search is case sensitive.If true, then do case-sensitive searches.
/// </summary>
Expand Down Expand Up @@ -1614,12 +1670,15 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI
}

// otherwise construct and populate a new MatchInfo object
matchResult = new MatchInfo
{
IgnoreCase = !CaseSensitive,
Line = operandString,
Pattern = Pattern[patternIndex]
};
matchResult = new MatchInfo(Raw, useColor:true, matchIndex:0, matchLength:0)
{
IgnoreCase = !CaseSensitive,
Line = operandString,
Pattern = Pattern[patternIndex]
};




if (_preContext > 0 || _postContext > 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,23 @@ Describe "Select-String" -Tags "CI" {
(Select-String in $testInputFile)[4].Line | Should -BeNullOrEmpty
}

It "Should return an undecorated strings where 'text' is found in testfile1 when raw is used " {
$expected1 = "This is a text string, and another string"

Select-String text $testInputFile -Raw | Should -Be $expected1

}

It "Should return the third line in testfile1 and the lines above and below it undecorated when raw is used " {
$expectedLine = "This is the second line"
$expectedLineBefore = "This is the third line"
$expectedLineAfter = "This is the fourth line"

Select-String third $testInputFile -Context 1 -Raw | Should -Match $expectedLine
Select-String third $testInputFile -Context 1 -Raw | Should -Match $expectedLineBefore
Select-String third $testInputFile -Context 1 -Raw | Should -Match $expectedLineAfter
}

It "Should return empty because 'for' is not found in testfile1 " {
Select-String for $testInputFile | Should -BeNullOrEmpty
}
Expand Down