From 98ca7a1b617e260fb6d0d565fd7de7da03a982c1 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Mon, 19 Oct 2026 13:54:12 +0200 Subject: [PATCH] Color terminal item statuses with plain output fallback --- README.md | 3 ++- TASKS.md | 1 + docs/advanced_commands.md | 3 +++ src/Core.psm1 | 20 +++++++++++++++++++- tests/Core.Tests.ps1 | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 57 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b6115ec..a459862 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,8 @@ See the [application config guide](docs/desktop-app-configs.md) for details. ## Troubleshooting and advanced use Run `cc doctor` in a new terminal to check setup, or `cc status` to inspect the -last run and config drift. +last run and config drift. Status colors highlight failures (red), conflicts or +skips (yellow), completed work (green), and unchanged items (gray). - [Advanced commands](docs/advanced_commands.md): profiles, packages, conflicts, backups, schedules, and development checks. diff --git a/TASKS.md b/TASKS.md index e929a31..8d224ae 100644 --- a/TASKS.md +++ b/TASKS.md @@ -132,3 +132,4 @@ open until it is run on disposable machines with the required account setups. - [x] Avoid null temporary-file cleanup after registry deployment or config adoption; preserve original per-item errors. - [x] Report CLI exceptions concisely on stderr without throwing from the error handler; preserve exit code 2 and JSON errors. - [x] Accept signed registry DWORD/QWORD exports and validate the complete MPC-HC declaration with mocked registry writes. +- [x] Color terminal item statuses while preserving plain redirected output and supporting NO_COLOR. diff --git a/docs/advanced_commands.md b/docs/advanced_commands.md index 10657f1..24048c2 100644 --- a/docs/advanced_commands.md +++ b/docs/advanced_commands.md @@ -318,3 +318,6 @@ and repository hygiene checks; it does not pretend to execute Windows behavior. integrity checks. - Package removal and deletion of formerly managed configs are deliberately manual operations. + +Terminal results color status labels only. Redirected output and JSON remain plain. +Set `$env:NO_COLOR = "1"` to disable status colors in a terminal. diff --git a/src/Core.psm1 b/src/Core.psm1 index 350a81f..f76148a 100644 --- a/src/Core.psm1 +++ b/src/Core.psm1 @@ -184,14 +184,32 @@ function Invoke-ConfigCentral { return [pscustomobject]$result } +function Test-ResultColor { + -not [Console]::IsOutputRedirected -and $null -eq [Environment]::GetEnvironmentVariable('NO_COLOR') +} + function Write-ConfigCentralResult { param($Result) + $useColor = Test-ResultColor Write-Host "Config Central: $($Result.command)" if ($Result.revision) { Write-Host "Revision: $($Result.revision)" } foreach ($operation in @($Result.operations)) { $state = if ($operation.PSObject.Properties['action']) { $operation.action } elseif ($operation.PSObject.Properties['status']) { $operation.status } elseif ($operation.PSObject.Properties['passed']) { if ($operation.passed) { 'OK' } else { 'FAIL' } } else { '-' } $message = if ($operation.PSObject.Properties['reason']) { $operation.reason } else { $operation.message } - Write-Host ("{0,-10} {1,-24} {2}" -f $state, $operation.name, $message) + $color = switch ($state) { + { $_ -in @('Failed', 'FAIL') } { 'Red'; break } + { $_ -in @('Conflict', 'Skipped', 'Warning', 'Missing') } { 'Yellow'; break } + { $_ -in @('Succeeded', 'OK', 'Captured') } { 'Green'; break } + { $_ -in @('Unchanged', 'None') } { 'DarkGray'; break } + default { 'Cyan' } + } + if ($useColor) { + Write-Host ("{0,-10}" -f $state) -ForegroundColor $color -NoNewline + Write-Host (" {0,-24} {1}" -f $operation.name, $message) + } + else { + Write-Host ("{0,-10} {1,-24} {2}" -f $state, $operation.name, $message) + } } foreach ($warning in @($Result.warnings)) { Write-Warning $warning } } diff --git a/tests/Core.Tests.ps1 b/tests/Core.Tests.ps1 index 146bcd9..bf1b480 100644 --- a/tests/Core.Tests.ps1 +++ b/tests/Core.Tests.ps1 @@ -33,3 +33,35 @@ Describe 'Core workflow policy' { } } } + +Describe 'Result status colors' { + It 'colors statuses without coloring item names and messages' { + InModuleScope Core { + Mock Test-ResultColor { $true } + Mock Write-Host {} + $result = [pscustomobject]@{ command = 'apply'; revision = ''; warnings = @(); operations = @( + [pscustomobject]@{ name = 'one'; status = 'Failed'; message = 'Problem' }, + [pscustomobject]@{ name = 'two'; status = 'Succeeded'; message = 'Done' }, + [pscustomobject]@{ name = 'three'; status = 'Conflict'; message = 'Local edits' }, + [pscustomobject]@{ name = 'four'; status = 'Unchanged'; message = 'Current' } + ) } + Write-ConfigCentralResult $result + Should -Invoke Write-Host -Times 1 -ParameterFilter { $ForegroundColor -eq 'Red' -and $NoNewline } + Should -Invoke Write-Host -Times 1 -ParameterFilter { $ForegroundColor -eq 'Green' -and $NoNewline } + Should -Invoke Write-Host -Times 1 -ParameterFilter { $ForegroundColor -eq 'Yellow' -and $NoNewline } + Should -Invoke Write-Host -Times 1 -ParameterFilter { $ForegroundColor -eq 'DarkGray' -and $NoNewline } + } + } + It 'writes complete plain rows when color is disabled' { + InModuleScope Core { + Mock Test-ResultColor { $false } + Mock Write-Host {} + $result = [pscustomobject]@{ command = 'apply'; revision = ''; warnings = @(); operations = @( + [pscustomobject]@{ name = 'one'; status = 'Failed'; message = 'Problem' } + ) } + Write-ConfigCentralResult $result + Should -Invoke Write-Host -Times 1 -ParameterFilter { $Object -match '^Failed\s+one\s+Problem$' -and -not $NoNewline } + Should -Invoke Write-Host -Times 0 -ParameterFilter { $PSBoundParameters.ContainsKey('ForegroundColor') } + } + } +}