Skip to content

Commit

Permalink
Merge pull request #148 from StartAutomating/IrregularANSIFix
Browse files Browse the repository at this point in the history
Irregular ansi fix
  • Loading branch information
StartAutomating authored Sep 29, 2022
2 parents aa63deb + 1161c31 commit 41c3c12
Show file tree
Hide file tree
Showing 20 changed files with 193 additions and 84 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
## 0.7.2:
## 0.7.3:
* ANSI Improvements:
* Fixed ?<ANSI_Style> (Fixes #143)
* ?<ANSI_4BitColor> now supports bright ranges (Fixes #145)
* ?<ANSI_8BitColor>/?<ANSI_24BitColor> now supports background colors (Fixes #144)
* ?<ANSI_8BitColor>/?<ANSI_24BitColor> now supports underline colors (Fixes #146)
---

## 0.7.2:
* Lots More ANSI support:
* ?<ANSI_Bold> (Fixes #131)
* ?<ANSI_Blink> (Fixes #132)
Expand Down
2 changes: 1 addition & 1 deletion Irregular.format.ps1xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-16"?>
<!-- Generated with EZOut 1.9.3: Install-Module EZOut or https://github.com/StartAutomating/EZOut -->
<!-- Generated with EZOut 1.9.4: Install-Module EZOut or https://github.com/StartAutomating/EZOut -->
<Configuration>
<ViewDefinitions>
<View>
Expand Down
10 changes: 9 additions & 1 deletion Irregular.psd1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@{
ModuleVersion = '0.7.2'
ModuleVersion = '0.7.3'
RootModule = 'Irregular.psm1'
Description = 'Regular Expressions made Strangely Simple'
FormatsToProcess = 'Irregular.format.ps1xml'
Expand All @@ -14,6 +14,14 @@
LicenseURI = 'https://github.com/StartAutomating/Irregular/blob/master/LICENSE'
IconURI = 'https://github.com/StartAutomating/Irregular/blob/master/Assets/Irregular_600_Square.png'
ReleaseNotes = @'
## 0.7.3:
* ANSI Improvements:
* Fixed ?<ANSI_Style> (Fixes #143)
* ?<ANSI_4BitColor> now supports bright ranges (Fixes #145)
* ?<ANSI_8BitColor>/?<ANSI_24BitColor> now supports background colors (Fixes #144)
* ?<ANSI_8BitColor>/?<ANSI_24BitColor> now supports underline colors (Fixes #146)
---
## 0.7.2:
* Lots More ANSI support:
* ?<ANSI_Bold> (Fixes #131)
Expand Down
2 changes: 1 addition & 1 deletion Irregular.types.ps1xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-16"?>
<!-- Generated with EZOut 1.9.3: Install-Module EZOut or https://github.com/StartAutomating/EZOut -->
<!-- Generated with EZOut 1.9.4: Install-Module EZOut or https://github.com/StartAutomating/EZOut -->
<Types>
<Type>
<Name>Irregular.Match.Extract</Name>
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<h2>Regular Expressions made Strangely Simple</h2>
<h3>A PowerShell module that helps you understand, use, and build Regular Expressions.</h3>
<h4>
<a href='https://github.com/StartAutomating/Irregular/releases/tag/v0.7.2'>v 0.7.2 </a>
<a href='https://github.com/StartAutomating/Irregular/releases/tag/v0.7.3'>v 0.7.3 </a>
</h4>
<a href='https://www.powershellgallery.com/packages/Irregular/'>
<img src='https://img.shields.io/powershellgallery/dt/Irregular' />
Expand Down Expand Up @@ -32,7 +32,7 @@ Once you understand some basics of that syntax, regular expressions become a lot
3. A Regex can have comments! ( # Like this in .NET ( or like (?#this comment) in ECMAScript ) ).
4. You don't have to do it all in one expression!

Irregular comes with 128 useful [named expressions](SavedPatterns.md), and lets you create more.
Irregular comes with 130 useful [named expressions](SavedPatterns.md), and lets you create more.

To see the expressions that ship with Irregular, run:

Expand Down
9 changes: 7 additions & 2 deletions RegEx/ANSI/24BitColor.regex.source.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ $myRoot = $MyInvocation.MyCommand.ScriptBlock.File | Split-Path
New-RegEx -Description "Matches an ANSI 24-bit color" -Modifier IgnoreCase -Not |
New-RegEx -CharacterClass Escape -Comment 'An Escape' |
New-RegEx -LiteralCharacter '[' -Comment 'Followed by a bracket' |
New-RegEx -Pattern '38;2;' |
New-RegEx -Atomic -Or @(
New-RegEx -Pattern '38' -Name IsForegroundColor
New-RegEx -Pattern '48' -Name IsBackgroundColor
New-RegEx -Pattern '58' -Name IsUnderlineColor
) |
New-RegEx -Pattern ';2;' |
New-RegEx -Name Color -Pattern (
New-Regex -Name Red -Pattern '(?>[0-2][0-5][0-5]|[0-1]\d\d|\d{1,2})' -Comment 'Red is the first 0-255 value' |
New-RegEx -Pattern ';' |
New-Regex -Name Red -Pattern '(?>[0-2][0-5][0-5]|[0-1]\d\d|\d{1,2})' -Comment 'Green is the second 0-255 value' |
New-Regex -Name Green -Pattern '(?>[0-2][0-5][0-5]|[0-1]\d\d|\d{1,2})' -Comment 'Green is the second 0-255 value' |
New-RegEx -Pattern ';' |
New-Regex -Name Blue -Pattern '(?>[0-2][0-5][0-5]|[0-1]\d\d|\d{1,2})' -Comment 'Blue is the third 0-255 value' |
New-RegEx -Pattern 'm'
Expand Down
13 changes: 8 additions & 5 deletions RegEx/ANSI/24BitColor.regex.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Matches an ANSI 24-bit color
(?-i)\e # An Escape
\[ # Followed by a bracket
38;2;(?<Color>(?<Red>(?>[0-2][0-5][0-5]|[0-1]\d\d|\d{1,2})) # Red is the first 0-255 value
;(?<Red>(?>[0-2][0-5][0-5]|[0-1]\d\d|\d{1,2})) # Green is the second 0-255 value
;(?<Blue>(?>[0-2][0-5][0-5]|[0-1]\d\d|\d{1,2})) # Blue is the third 0-255 value
(?-i)\e # An Escape
\[ # Followed by a bracket
(?>
(?<IsForegroundColor>38) |
(?<IsBackgroundColor>48) |
(?<IsUnderlineColor>58));2;(?<Color>(?<Red>(?>[0-2][0-5][0-5]|[0-1]\d\d|\d{1,2})) # Red is the first 0-255 value
;(?<Green>(?>[0-2][0-5][0-5]|[0-1]\d\d|\d{1,2})) # Green is the second 0-255 value
;(?<Blue>(?>[0-2][0-5][0-5]|[0-1]\d\d|\d{1,2})) # Blue is the third 0-255 value
m)
29 changes: 21 additions & 8 deletions RegEx/ANSI/4BitColor.regex.source.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,25 @@ New-RegEx -Description "Matches an ANSI 3 or 4-bit color" |
New-RegEx -CharacterClass Escape -Comment 'An Escape' |
New-RegEx -LiteralCharacter '[' -Comment 'Followed by a bracket' |
New-RegEx -Pattern (
New-RegEx -Pattern 1 -Optional -Name IsBright |
New-RegEx -LiteralCharacter ';' -Optional |
New-RegEx -Pattern '3[0-7]' -Name ForegroundColor -Optional |
New-RegEx -If ForegroundColor -Then 'm' -Else (
New-RegEx -Pattern '4[0-7]' -Name BackgroundColor |
New-RegEx -Pattern 'm' -Modifier IgnoreCase -Not
)
) -Name Color |

New-RegEx -Atomic -Or @(
New-RegEx -Pattern 1 -Optional -Name IsBright |
New-RegEx -LiteralCharacter ';' -Min 0 -Max 1 |
New-RegEx -Pattern '3' -Name IsForegroundColor

New-RegEx -Name IsBright (
New-RegEx -Pattern '9' -Name IsForegroundColor
)

New-RegEx -Pattern 1 -Optional -Name IsBright |
New-RegEx -LiteralCharacter ';' -Min 0 -Max 1 |
New-RegEx -Pattern '4' -Name IsBackgroundColor

New-RegEx -Name IsBright (
New-RegEx -Pattern '10' -Name IsBackgroundColor
)
) |
New-RegEx -Pattern '[0-7]' -Name ColorNumber |
New-RegEx -LiteralCharacter 'm'
) -Name Color | #>
Set-Content -Path (Join-Path $myRoot $myName)
6 changes: 5 additions & 1 deletion RegEx/ANSI/4BitColor.regex.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Matches an ANSI 3 or 4-bit color
\e # An Escape
\[ # Followed by a bracket
(?<Color>(?<IsBright>1)?\;?(?<ForegroundColor>3[0-7])?(?(ForegroundColor)(m)|((?<BackgroundColor>4[0-7])m)))
(?<Color>(?>
(?<IsBright>1)?\;{0,1}(?<IsForegroundColor>3) |
(?<IsBright>(?<IsForegroundColor>9)) |
(?<IsBright>1)?\;{0,1}(?<IsBackgroundColor>4) |
(?<IsBright>(?<IsBackgroundColor>10)))(?<ColorNumber>[0-7])m)
7 changes: 6 additions & 1 deletion RegEx/ANSI/8BitColor.regex.source.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ $myRoot = $MyInvocation.MyCommand.ScriptBlock.File | Split-Path
New-RegEx -Description "Matches an ANSI 8-bit color" -Modifier IgnoreCase -Not |
New-RegEx -CharacterClass Escape -Comment 'An Escape' |
New-RegEx -LiteralCharacter '[' -Comment 'Followed by a bracket' |
New-RegEx -Pattern '38;5;' |
New-RegEx -Atomic -Or @(
New-RegEx -Pattern '38' -Name IsForegroundColor
New-RegEx -Pattern '48' -Name IsBackgroundColor
New-RegEx -Pattern '58' -Name IsUnderlineColor
) |
New-RegEx -Pattern ';5;' |
New-RegEx -Name Color -Pattern (
New-RegEx -Atomic -Or @(
New-Regex -Name StandardColor -Pattern '[0-7]' -Comment '0 -7 are standard colors' |
Expand Down
7 changes: 5 additions & 2 deletions RegEx/ANSI/8BitColor.regex.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Matches an ANSI 8 bit color
# Matches an ANSI 8-bit color
(?-i)\e # An Escape
\[ # Followed by a bracket
38;5;(?<Color>(?>
(?>
(?<IsForegroundColor>38) |
(?<IsBackgroundColor>48) |
(?<IsUnderlineColor>58));5;(?<Color>(?>
(?<StandardColor>[0-7]) # 0 -7 are standard colors
m |
(?<BrightColor>(?>[8-9]|1[0-5])) # 8-15 are bright colors
Expand Down
27 changes: 27 additions & 0 deletions RegEx/ANSI/ANSI.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
describe 'Irregular and ANSI' {
it 'Can match any ANSI sequence' {
"$([char]0x1b)[0m" | ?<ANSI_Code> | Select-Object -ExpandProperty Length | Should -be 4

"$([char]0x1b)[1;3;5,~" | ?<ANSI_Code> | Select-Object -ExpandProperty Length | Should -be 9
}
context 'ANSI Colors' {
it 'Can Match a 4-bit color' {
$x = "$([char]0x1b)[32m" | ?<ANSI_4BitColor> -Extract
$x.ColorNumber | Should -Be 2
$x.IsForegroundColor | Should -Be 3
}

it 'Can Match a 24-bit color' {
$x = "$([char]0x1b)[38;2;255;100;0m" | ?<ANSI_24BitColor> -Extract
$x.Red | Should -be 255
$x.Green | Should -be 100
$x.Blue | Should -be 0
}
}

context 'ANSI Styles' {
it 'Can Match ANSI Styles' {
@("$([char]0x1b)[3m Italics $([char]0x1b)[23m" | ?<ANSI_Style>).Count | Should -be 2
}
}
}
3 changes: 2 additions & 1 deletion RegEx/ANSI/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Note: Using these regular expressions in the terminal may result in awkward out
|------------------------------------------------|-------------------------------------------------------------|----------------------------------------|
|[?<ANSI_24BitColor>](24BitColor.regex.txt) |Matches an ANSI 24-bit color |[source](24BitColor.regex.source.ps1) |
|[?<ANSI_4BitColor>](4BitColor.regex.txt) |Matches an ANSI 3 or 4-bit color |[source](4BitColor.regex.source.ps1) |
|[?<ANSI_8BitColor>](8BitColor.regex.txt) |Matches an ANSI 8 bit color |[source](8BitColor.regex.source.ps1) |
|[?<ANSI_8BitColor>](8BitColor.regex.txt) |Matches an ANSI 8-bit color |[source](8BitColor.regex.source.ps1) |
|[?<ANSI_Blink>](Blink.regex.txt) |Matches ANSI Blink Start or End |[source](Blink.regex.source.ps1) |
|[?<ANSI_Bold>](Bold.regex.txt) |Matches an ANSI Bold Start or End |[source](Bold.regex.source.ps1) |
|[?<ANSI_Code>](Code.regex.txt) |Matches an ANSI escape code |[source](Code.regex.source.ps1) |
Expand All @@ -20,6 +20,7 @@ Note: Using these regular expressions in the terminal may result in awkward out
|[?<ANSI_Note>](Note.regex.txt) |Matches an ANSI VT520 Note |[source](Note.regex.source.ps1) |
|[?<ANSI_Reset>](Reset.regex.txt) |Matches an ANSI Reset (this clears formatting) |[source](Reset.regex.source.ps1) |
|[?<ANSI_Strikethrough>](Strikethrough.regex.txt)|Matches ANSI Strikethrough Start or End |[source](Strikethrough.regex.source.ps1)|
|[?<ANSI_Style>](Style.regex.txt) |Matches an ANSI style (color or text option) |[source](Style.regex.source.ps1) |
|[?<ANSI_Underline>](Underline.regex.txt) |Matches ANSI Underline/DoubleUnderline Start or Underline End|[source](Underline.regex.source.ps1) |


2 changes: 1 addition & 1 deletion RegEx/ANSI/Style.regex.source.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ New-RegEx -Description "Matches an ANSI style (color or text option)" |
New-Regex -Pattern '?<ANSI_Italic>'
New-Regex -Pattern '?<ANSI_Invert>'
New-Regex -Pattern '?<ANSI_Hide>'
New-Regex -Pattern '?<ANSI_Strikethru>'
New-Regex -Pattern '?<ANSI_Strikethrough>'
New-Regex -Pattern '?<ANSI_Underline>'
New-RegEx -Pattern '?<ANSI_24BitColor>'
New-RegEx -Pattern '?<ANSI_8BitColor>'
Expand Down
Loading

0 comments on commit 41c3c12

Please sign in to comment.