Skip to content

Commit 44998fd

Browse files
Add -NoColor switch parameter to control colored output in formatters (#18)
Adds a `-NoColor` switch parameter to both `Read-ChocoLog` and `Get-ChocoLogEntry` functions to allow users to disable colored output in the formatter. This addresses user needs for plain text output in environments that don't support ANSI colors or when capturing output for logging systems. ## Changes - **Added `-NoColor` parameter** to both public functions with proper documentation - **Implemented module-level variable** `$script:ChocoLogNoColor` to control formatter behavior - **Updated formatter logic** in `ChocoLog.format.ps1xml` to conditionally apply colors based on the variable - **Fixed existing bug** where undefined `$bg` variable was referenced in the original formatter - **Bumped version** from 1.0.1 to 1.1.0 to reflect new feature addition - **Updated changelog** with comprehensive details of the new functionality ## Usage ```powershell # Default behavior (colored output) - unchanged Read-ChocoLog Get-ChocoLogEntry # New functionality - disable colors Read-ChocoLog -NoColor Get-ChocoLogEntry -NoColor ``` ## Benefits - **Backward Compatible**: Default behavior remains unchanged (colored output) - **Minimal Implementation**: Uses existing PowerShell formatting infrastructure - **Clean Design**: Single module variable controls all formatting behavior - **Proper Documentation**: Includes help text and usage examples - **Semantic Versioning**: Minor version bump (1.1.0) reflects feature addition The implementation allows users to customize their experience based on their terminal capabilities or output requirements while maintaining the enhanced visual experience for users who want colored output. Fixes #10. <!-- START COPILOT CODING AGENT TIPS --> --- 💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click [here](https://survey.alchemer.com/s3/8343779/Copilot-Coding-agent) to start the survey. --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: HeyItsGilbert <[email protected]> Co-authored-by: Gilbert Sanchez <[email protected]>
1 parent 40a6e62 commit 44998fd

25 files changed

+307
-40
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

8+
## [1.1.0] Add -NoColor Switch Parameter
9+
10+
### Added
11+
12+
- `-NoColor` switch parameter to both `Read-ChocoLog` and `Get-ChocoLogEntry` functions
13+
- Module-level variable `$script:ChocoLogNoColor` to control formatter behavior
14+
- Colored output control in `ChocoLog.format.ps1xml` formatter
15+
- Documentation and examples for the new parameter
16+
17+
### Fixed
18+
19+
- Undefined `$bg` variable reference in the original formatter
20+
821
## [1.0.1] Add Missing Fatal
922

1023
### Fixes

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.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
RootModule = 'ChocoLogParse.psm1'
1313

1414
# Version number of this module.
15-
ModuleVersion = '1.0.1'
15+
ModuleVersion = '1.1.0'
1616

1717
# Supported PSEditions
1818
# CompatiblePSEditions = @()

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: 18 additions & 4 deletions
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,13 +27,16 @@
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.
2936
[OutputType([System.Collections.Generic.List[ChocoLog]])]
3037
param (
3138
[ValidateScript({
32-
if (-Not ($_ | Test-Path) ) {
39+
if (-not ($_ | Test-Path) ) {
3340
throw "File or folder does not exist"
3441
}
3542
return $true
@@ -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 |
@@ -110,6 +123,7 @@ function Read-ChocoLog {
110123
}
111124

112125
# Return the whole parsed object
113-
Write-Verbose "Returning results in desceding order. Count: $($parsed.Count)"
114-
$parsed.Values | Sort-Object -Descending Time
126+
Write-Verbose "Returning results in descending order. Count: $($parsed.Count)"
127+
$return = $parsed.Values | Sort-Object -Descending Time
128+
return $return
115129
}

docs/en-US/Get-ChocoLogEntry.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Defaults to last exection.
1414
## SYNTAX
1515

1616
```
17-
Get-ChocoLogEntry [[-Path] <String[]>] [[-Filter] <String>] [[-PatternLayout] <String>] [-Report]
17+
Get-ChocoLogEntry [[-Path] <String[]>] [[-Filter] <String>] [[-PatternLayout] <String>] [-Report] [-NoColor]
1818
[-ProgressAction <ActionPreference>] [<CommonParameters>]
1919
```
2020

@@ -99,6 +99,22 @@ Accept pipeline input: False
9999
Accept wildcard characters: False
100100
```
101101
102+
### -NoColor
103+
Disables colored output in the formatter. When specified, the output will be
104+
displayed without ANSI color codes.
105+
106+
```yaml
107+
Type: SwitchParameter
108+
Parameter Sets: (All)
109+
Aliases:
110+
111+
Required: False
112+
Position: Named
113+
Default value: False
114+
Accept pipeline input: False
115+
Accept wildcard characters: False
116+
```
117+
102118
### -ProgressAction
103119
{{ Fill ProgressAction Description }}
104120

docs/en-US/Read-ChocoLog.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Parses a Chocolatey log into an object that is easier to search and filter.
1414

1515
```
1616
Read-ChocoLog [[-Path] <String[]>] [[-FileLimit] <Int32>] [[-Filter] <String>] [[-PatternLayout] <String>]
17-
[<CommonParameters>]
17+
[-NoColor] [<CommonParameters>]
1818
```
1919

2020
## DESCRIPTION
@@ -100,6 +100,22 @@ Accept pipeline input: False
100100
Accept wildcard characters: False
101101
```
102102
103+
### -NoColor
104+
Disables colored output in the formatter. When specified, the output will be
105+
displayed without ANSI color codes.
106+
107+
```yaml
108+
Type: SwitchParameter
109+
Parameter Sets: (All)
110+
Aliases:
111+
112+
Required: False
113+
Position: Named
114+
Default value: False
115+
Accept pipeline input: False
116+
Accept wildcard characters: False
117+
```
118+
103119
### CommonParameters
104120
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
105121

requirements.psd1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
}
1010
}
1111
'psake' = @{
12-
Version = '4.9.0'
12+
Version = '4.9.1'
1313
}
1414
'BuildHelpers' = @{
1515
Version = '2.0.16'
1616
}
1717
'PowerShellBuild' = @{
18-
Version = '0.6.1'
18+
Version = '0.7.3'
1919
}
2020
'PSScriptAnalyzer' = @{
2121
Version = '1.19.1'

tests/ChocoLogParse.tests.ps1

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# cSpell:ignore BHPS Ffoo Subkeys
22
BeforeDiscovery {
3+
if ($null -eq $env:BHPSModuleManifest) {
4+
.\build.ps1 -Task Init
5+
}
36
$manifest = Import-PowerShellDataFile -Path $env:BHPSModuleManifest
47
$outputDir = Join-Path -Path $env:BHProjectPath -ChildPath 'Output'
58
$outputModDir = Join-Path -Path $outputDir -ChildPath $env:BHProjectName
@@ -14,8 +17,7 @@ BeforeDiscovery {
1417

1518
BeforeAll {
1619
# Setup dummy data including things across multiple lines
17-
$folder = "TestDrive:\folder"
18-
New-Item -Path "TestDrive:\" -Name 'folder' -Type Directory -Force
20+
$folder = Join-Path -Path $PSScriptRoot -ChildPath 'fixtures\folder'
1921
$singleFile = "TestDrive:\test.log"
2022
# Due to "files.trimTrailingWhitespace" vscode setting, I added some '.'s
2123
# to the multiline examples
@@ -74,27 +76,6 @@ Chocolatey upgraded 0/1 packages.
7476
2023-06-14 14:22:10,115 54321 [DEBUG] - Sending message 'PostRunMessage' out if there are subscribers...
7577
2023-06-14 14:22:10,117 54321 [DEBUG] - Exiting with 900
7678
'@
77-
# Create 10 files with 2 random sessions
78-
0..10 | ForEach-Object {
79-
$randID = Get-Random -Minimum 1000 -Maximum 99999
80-
$randID2 = $randID - 100
81-
Set-Content "TestDrive:\folder\chocolatey.$($_).log" -Value @"
82-
2023-06-14 14:22:09,411 $randId [DEBUG] - Ffoo
83-
2023-06-14 14:22:09,418 $randId [DEBUG] - _ Chocolatey:ChocolateyUpgradeCommand - Normal Run Mode _
84-
2023-06-14 14:22:09,422 $randId [INFO ] - Upgrading the following packages:
85-
2023-06-14 14:22:09,423 $randId [INFO ] - zoom
86-
2023-06-14 14:22:09,423 $randId [INFO ] - By upgrading, you accept licenses for the packages.
87-
2023-06-14 14:22:10,107 $randId [INFO ] - zoom v5.14.11.17466 is newer than the most recent.
88-
You must be smarter than the average bear...
89-
2023-06-14 14:22:10,113 $randId [WARN ] - .
90-
Chocolatey upgraded 0/1 packages.
91-
See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).
92-
2023-06-14 14:22:10,115 $randId [DEBUG] - Sending message 'PostRunMessage' out if there are subscribers...
93-
2023-06-14 14:22:10,117 $randId [DEBUG] - Exiting with 0
94-
2023-06-14 15:22:09,411 $randId2 [DEBUG] - Ffoo
95-
2023-06-14 15:22:10,117 $randId2 [DEBUG] - Exiting with 0
96-
"@
97-
}
9879
}
9980

10081
Describe 'Read-ChocoLog' {
@@ -106,7 +87,7 @@ Describe 'Read-ChocoLog' {
10687

10788
Context 'For Single File' {
10889
It 'Parses the correct number of sessions' {
109-
($script:parsed).Count | Should -Be 3
90+
($script:parsed).Count | Should -Be 3
11091
}
11192

11293
It 'Parses the correct number of lines per session' {
@@ -140,7 +121,9 @@ Describe 'Read-ChocoLog' {
140121
}
141122

142123
It 'Parses the correct number of lines per session' {
124+
$script:multiple.Count | Should -Be 2
143125
$script:multiple[0].logs.Count | Should -Be 9
126+
$script:multiple[1].logs.Count | Should -Be 2
144127
}
145128
}
146129
}
@@ -153,7 +136,7 @@ Describe 'Get-ChocoLogEntry' {
153136

154137
Context 'For Single File' {
155138
It 'Parses the correct number of sessions' {
156-
($script:parsedEntry).Count | Should -Be 1
139+
($script:parsedEntry).Count | Should -Be 1
157140
}
158141

159142
It 'Parses the correct number of lines per session' {

0 commit comments

Comments
 (0)