Skip to content

Commit e10fdb4

Browse files
Add -NoColor switch parameter to control colored output
Co-authored-by: HeyItsGilbert <[email protected]>
1 parent 40a6e62 commit e10fdb4

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

ChocoLogParse/ChocoLog.format.ps1xml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,16 @@
3636
</TableColumnItem>
3737
<TableColumnItem>
3838
<ScriptBlock>
39-
$style = if ($_.exitCode -eq 0) {
40-
$PSStyle.Foreground.White
39+
if ($script:ChocoLogNoColor) {
40+
[String]::Format("{0}", $_.exitCode)
4141
} else {
42-
$PSStyle.Foreground.Yellow
42+
$style = if ($_.exitCode -eq 0) {
43+
$PSStyle.Foreground.White
44+
} else {
45+
$PSStyle.Foreground.Yellow
46+
}
47+
[String]::Format("{0}{1}{2}", $style, $_.exitCode, $PSStyle.Reset)
4348
}
44-
[String]::Format("{0}{1}{2}", $bg, $_.exitCode, $PSStyle.Reset)
4549
</ScriptBlock>
4650
</TableColumnItem>
4751
<TableColumnItem>

ChocoLogParse/ChocoLogParse.psm1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ foreach ($import in @($enums + $classes + $public )) {
1010
}
1111
}
1212

13+
# Initialize module variable for color control (default to colored output)
14+
$script:ChocoLogNoColor = $false
15+
1316
# Add our custom formatter that needed classes first
1417
$format = Join-Path -Path $PSScriptRoot -ChildPath 'ChocoLog.format.ps1xml'
1518
Update-FormatData -PrependPath $format

ChocoLogParse/Public/Get-ChocoLogEntry.ps1

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
Get-ChocoLogEntry
1010
1111
Grabs the laste entry from the latest log
12+
.EXAMPLE
13+
Get-ChocoLogEntry -NoColor
14+
15+
Grabs the latest entry from the latest log without colored output
1216
.PARAMETER Report
1317
This changes the output to be more friendly for reporting
1418
.PARAMETER Path
@@ -20,6 +24,9 @@
2024
The log4net pattern layout used to parse the log. It is very unlikely that you
2125
need to supply this. The code expects pattern names: time, session, level, and
2226
message.
27+
.PARAMETER NoColor
28+
Disables colored output in the formatter. When specified, the output will be
29+
displayed without ANSI color codes.
2330
#>
2431
function Get-ChocoLogEntry {
2532
[CmdletBinding()]
@@ -36,15 +43,16 @@ function Get-ChocoLogEntry {
3643
$Filter = 'chocolatey*.log',
3744
[string]
3845
$PatternLayout = '%date %thread [%-5level] - %message',
39-
[switch]$Report
46+
[switch]$Report,
47+
[switch]$NoColor
4048
)
4149
# ToDo:
4250
# - Support searching for a cli entry
4351
# - sub command type (e.g. search, list, upgrade)
4452
# - Exit code
4553
# - Filter to recent time to make sure we get the right one
4654

47-
$entry = Read-ChocoLog -FileLimit 1 -Path $Path -Filter $Filter -PatternLayout $PatternLayout | Select-Object -Last 1
55+
$entry = Read-ChocoLog -FileLimit 1 -Path $Path -Filter $Filter -PatternLayout $PatternLayout -NoColor:$NoColor | Select-Object -Last 1
4856
if ($report) {
4957
# Print out in a format that's useful for Chef logging
5058
Write-Host ('Command: {0}' -F $entry.cli)

ChocoLogParse/Public/Read-ChocoLog.ps1

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
Read-ChocoLog
1313
1414
This will read the latest Chocolatey.log on the machine.
15+
.EXAMPLE
16+
Read-ChocoLog -NoColor
17+
18+
This will read the latest Chocolatey.log on the machine without colored output.
1519
.PARAMETER Path
1620
The log path you want to parse. This will default to the latest local log.
1721
This can be a directory of logs.
@@ -23,6 +27,9 @@
2327
The log4net pattern layout used to parse the log. It is very unlikely that you
2428
need to supply this. The code expects pattern names: time, session, level, and
2529
message.
30+
.PARAMETER NoColor
31+
Disables colored output in the formatter. When specified, the output will be
32+
displayed without ANSI color codes.
2633
#>
2734
function Read-ChocoLog {
2835
# This makes PlatyPS sad.
@@ -41,8 +48,14 @@ function Read-ChocoLog {
4148
[String]
4249
$Filter = 'chocolatey*.log',
4350
[string]
44-
$PatternLayout = '%date %thread [%-5level] - %message'
51+
$PatternLayout = '%date %thread [%-5level] - %message',
52+
[switch]
53+
$NoColor
4554
)
55+
56+
# Set module-level variable to control coloring in formatter
57+
$script:ChocoLogNoColor = $NoColor.IsPresent
58+
4659
$files = Get-Item -Path $Path
4760
if ($files.PSIsContainer) {
4861
$files = Get-ChildItem -Path $Path -Filter $Filter |

0 commit comments

Comments
 (0)