Skip to content

Commit

Permalink
Adds emphasis to Select-String default formatter (PowerShell#8963)
Browse files Browse the repository at this point in the history
  • Loading branch information
derek-xia authored and kilasuit committed Nov 9, 2019
1 parent 18a7de5 commit 8e12b00
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public string ToString(string directory)
}

/// <summary>
/// Returns the string representation of the match object with the matched line passed
/// Returns the string representation of the match object with the matched line passed
/// in as <paramref name="line"/> and trims the path to be relative to
/// the<paramref name="directory"/> argument.
/// </summary>
Expand Down Expand Up @@ -315,10 +315,10 @@ public string ToEmphasizedString(string directory)
/// <returns>The matched line with matched text inverted.</returns>
private string EmphasizeLine()
{
string invertColorsVT100 = VTUtility.GetEscapeSequence(VTUtility.VT.Inverse);
string resetVT100 = VTUtility.GetEscapeSequence(VTUtility.VT.Reset);
const string InvertColorsVT100 = "\u001b[7m";
const string ResetVT100 = "\u001b[0m";

char[] chars = new char[(_matchIndexes.Count * (invertColorsVT100.Length + resetVT100.Length)) + Line.Length];
char[] chars = new char[(_matchIndexes.Count * (InvertColorsVT100.Length + ResetVT100.Length)) + Line.Length];
int lineIndex = 0;
int charsIndex = 0;
for (int i = 0; i < _matchIndexes.Count; i++)
Expand All @@ -329,17 +329,17 @@ private string EmphasizeLine()
lineIndex = _matchIndexes[i];

// Adds opening vt sequence
invertColorsVT100.CopyTo(0, chars, charsIndex, invertColorsVT100.Length);
charsIndex += invertColorsVT100.Length;
InvertColorsVT100.CopyTo(0, chars, charsIndex, InvertColorsVT100.Length);
charsIndex += InvertColorsVT100.Length;

// Adds characters being emphasized
Line.CopyTo(lineIndex, chars, charsIndex, _matchLengths[i]);
lineIndex += _matchLengths[i];
charsIndex += _matchLengths[i];

// Adds closing vt sequence
resetVT100.CopyTo(0, chars, charsIndex, resetVT100.Length);
charsIndex += resetVT100.Length;
ResetVT100.CopyTo(0, chars, charsIndex, ResetVT100.Length);
charsIndex += ResetVT100.Length;
}

// Adds remaining characters in line
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ Describe "Select-String" -Tags "CI" {
$currentDirectory = $pwd.Path
}

AfterAll {
Push-Location $currentDirectory
}

Context "String actions" {
$testinputone = "hello","Hello","goodbye"
$testinputtwo = "hello","Hello"
Expand Down Expand Up @@ -80,47 +76,47 @@ Describe "Select-String" -Tags "CI" {
}

it "Should output a string with the first match highlighted" {
if ($Host.UI.SupportsVirtualTerminal -and !(Test-Path env:__SuppressAnsiEscapeSequences))
if ($Host.UI.SupportsVirtualTerminal)
{
$result = $testinputone | Select-String -Pattern "l" | Out-String
$result | Should -Be "${nl}he`e[7ml`e[0mlo${nl}He`e[7ml`e[0mlo${nl}${nl}"
$result | Should -Be "`nhe`e[7ml`e[0mlo`nHe`e[7ml`e[0mlo`n`n"
}
else
{
$result = $testinputone | Select-String -Pattern "l" | Out-String
$result | Should -Be "${nl}hello${nl}Hello${nl}${nl}"
$result | Should -Be "`nhello`nHello`n`n"
}
}

it "Should output a string with all matches highlighted when AllMatch is used" {
if ($Host.UI.SupportsVirtualTerminal -and !(Test-Path env:__SuppressAnsiEscapeSequences))
if ($Host.UI.SupportsVirtualTerminal)
{
$result = $testinputone | Select-String -Pattern "l" -AllMatch | Out-String
$result | Should -Be "${nl}he`e[7ml`e[0m`e[7ml`e[0mo${nl}He`e[7ml`e[0m`e[7ml`e[0mo${nl}${nl}"
$result | Should -Be "`nhe`e[7ml`e[0m`e[7ml`e[0mo`nHe`e[7ml`e[0m`e[7ml`e[0mo`n`n"
}
else
{
$result = $testinputone | Select-String -Pattern "l" -AllMatch | Out-String
$result | Should -Be "${nl}hello${nl}Hello${nl}${nl}"
$result | Should -Be "`nhello`nHello`n`n"
}
}

it "Should output a string with the first match highlighted when SimpleMatch is used" {
if ($Host.UI.SupportsVirtualTerminal -and !(Test-Path env:__SuppressAnsiEscapeSequences))
if ($Host.UI.SupportsVirtualTerminal)
{
$result = $testinputone | Select-String -Pattern "l" -SimpleMatch | Out-String
$result | Should -Be "${nl}he`e[7ml`e[0mlo${nl}He`e[7ml`e[0mlo${nl}${nl}"
$result | Should -Be "`nhe`e[7ml`e[0mlo`nHe`e[7ml`e[0mlo`n`n"
}
else
{
$result = $testinputone | Select-String -Pattern "l" -SimpleMatch | Out-String
$result | Should -Be "${nl}hello${nl}Hello${nl}${nl}"
$result | Should -Be "`nhello`nHello`n`n"
}
}

it "Should output a string without highlighting when NoEmphasis is used" {
$result = $testinputone | Select-String -Pattern "l" -NoEmphasis | Out-String
$result | Should -Be "${nl}hello${nl}Hello${nl}${nl}"
$result | Should -Be "`nhello`nHello`n`n"
}

it "Should return an array of matching strings without virtual terminal sequences" {
Expand Down Expand Up @@ -251,4 +247,8 @@ Describe "Select-String" -Tags "CI" {
Select-String second $testInputFile -Raw -Context 2,2 | Should -BeExactly $expected
}
}

AfterAll {
Push-Location $currentDirectory
}
}

0 comments on commit 8e12b00

Please sign in to comment.