Skip to content

Commit 8e12b00

Browse files
derek-xiakilasuit
authored andcommitted
Adds emphasis to Select-String default formatter (PowerShell#8963)
1 parent 18a7de5 commit 8e12b00

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ public string ToString(string directory)
256256
}
257257

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

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

331331
// Adds opening vt sequence
332-
invertColorsVT100.CopyTo(0, chars, charsIndex, invertColorsVT100.Length);
333-
charsIndex += invertColorsVT100.Length;
332+
InvertColorsVT100.CopyTo(0, chars, charsIndex, InvertColorsVT100.Length);
333+
charsIndex += InvertColorsVT100.Length;
334334

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

340340
// Adds closing vt sequence
341-
resetVT100.CopyTo(0, chars, charsIndex, resetVT100.Length);
342-
charsIndex += resetVT100.Length;
341+
ResetVT100.CopyTo(0, chars, charsIndex, ResetVT100.Length);
342+
charsIndex += ResetVT100.Length;
343343
}
344344

345345
// Adds remaining characters in line

test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ Describe "Select-String" -Tags "CI" {
77
$currentDirectory = $pwd.Path
88
}
99

10-
AfterAll {
11-
Push-Location $currentDirectory
12-
}
13-
1410
Context "String actions" {
1511
$testinputone = "hello","Hello","goodbye"
1612
$testinputtwo = "hello","Hello"
@@ -80,47 +76,47 @@ Describe "Select-String" -Tags "CI" {
8076
}
8177

8278
it "Should output a string with the first match highlighted" {
83-
if ($Host.UI.SupportsVirtualTerminal -and !(Test-Path env:__SuppressAnsiEscapeSequences))
79+
if ($Host.UI.SupportsVirtualTerminal)
8480
{
8581
$result = $testinputone | Select-String -Pattern "l" | Out-String
86-
$result | Should -Be "${nl}he`e[7ml`e[0mlo${nl}He`e[7ml`e[0mlo${nl}${nl}"
82+
$result | Should -Be "`nhe`e[7ml`e[0mlo`nHe`e[7ml`e[0mlo`n`n"
8783
}
8884
else
8985
{
9086
$result = $testinputone | Select-String -Pattern "l" | Out-String
91-
$result | Should -Be "${nl}hello${nl}Hello${nl}${nl}"
87+
$result | Should -Be "`nhello`nHello`n`n"
9288
}
9389
}
9490

9591
it "Should output a string with all matches highlighted when AllMatch is used" {
96-
if ($Host.UI.SupportsVirtualTerminal -and !(Test-Path env:__SuppressAnsiEscapeSequences))
92+
if ($Host.UI.SupportsVirtualTerminal)
9793
{
9894
$result = $testinputone | Select-String -Pattern "l" -AllMatch | Out-String
99-
$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}"
95+
$result | Should -Be "`nhe`e[7ml`e[0m`e[7ml`e[0mo`nHe`e[7ml`e[0m`e[7ml`e[0mo`n`n"
10096
}
10197
else
10298
{
10399
$result = $testinputone | Select-String -Pattern "l" -AllMatch | Out-String
104-
$result | Should -Be "${nl}hello${nl}Hello${nl}${nl}"
100+
$result | Should -Be "`nhello`nHello`n`n"
105101
}
106102
}
107103

108104
it "Should output a string with the first match highlighted when SimpleMatch is used" {
109-
if ($Host.UI.SupportsVirtualTerminal -and !(Test-Path env:__SuppressAnsiEscapeSequences))
105+
if ($Host.UI.SupportsVirtualTerminal)
110106
{
111107
$result = $testinputone | Select-String -Pattern "l" -SimpleMatch | Out-String
112-
$result | Should -Be "${nl}he`e[7ml`e[0mlo${nl}He`e[7ml`e[0mlo${nl}${nl}"
108+
$result | Should -Be "`nhe`e[7ml`e[0mlo`nHe`e[7ml`e[0mlo`n`n"
113109
}
114110
else
115111
{
116112
$result = $testinputone | Select-String -Pattern "l" -SimpleMatch | Out-String
117-
$result | Should -Be "${nl}hello${nl}Hello${nl}${nl}"
113+
$result | Should -Be "`nhello`nHello`n`n"
118114
}
119115
}
120116

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

126122
it "Should return an array of matching strings without virtual terminal sequences" {
@@ -251,4 +247,8 @@ Describe "Select-String" -Tags "CI" {
251247
Select-String second $testInputFile -Raw -Context 2,2 | Should -BeExactly $expected
252248
}
253249
}
250+
251+
AfterAll {
252+
Push-Location $currentDirectory
253+
}
254254
}

0 commit comments

Comments
 (0)