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,21 @@ public object Clone()
/// </summary>
public class MatchInfo
{
private bool isRaw;
david-wang24 marked this conversation as resolved.
Show resolved Hide resolved

/// <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)
{
isRaw = useRaw;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
isRaw = useRaw;
_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 +116,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 +242,12 @@ public string ToString(string directory)
// enable context-tracking.
if (Context == null)
{
return FormatLine(Line, this.LineNumber, displayPath, EmptyPrefix);
if(isRaw){
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if(isRaw){
if (isRaw)
{

return Line;
}else{
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
}else{
}
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 +256,29 @@ 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){
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since both cases of isRaw are processed here, may as well process the positive case first

Suggested change
if(!isRaw){
if (isRaw)
{

lines.Add(FormatLine(contextLine, displayLineNumber++, displayPath, ContextPrefix));
}else{
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
}else{
}
else
{

lines.Add(contextLine);
}
}

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

if(!isRaw){
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same as above on the positive case first

Suggested change
if(!isRaw){
if (isRaw)
{

lines.Add(FormatLine(Line, displayLineNumber++, displayPath, MatchPrefix));
}else{
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
}else{
}
else
{

lines.Add(Line);
}


foreach (string contextLine in Context.DisplayPostContext)
{
lines.Add(FormatLine(contextLine, displayLineNumber++, displayPath, ContextPrefix));
if(!isRaw){
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ditto on positive case first

Suggested change
if(!isRaw){
if (isRaw)
{

lines.Add(FormatLine(contextLine, displayLineNumber++, displayPath, ContextPrefix));
}else{
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
}else{
}
else
{

lines.Add(contextLine);
}

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 +1062,13 @@ public string[] LiteralPath
[Parameter]
public SwitchParameter SimpleMatch { get; set; }

/// <summary>
/// Gets or sets a value indicating if an undecorated string should be outputted.
david-wang24 marked this conversation as resolved.
Show resolved Hide resolved
/// If not (default) output a decorated string.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
/// If not (default) output a decorated string.
/// 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 +1657,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, true, 0, 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