Color terminal item statuses with plain output fallback
Config Central CI Config Central passed all CI integrity checks.
Beffroi/config-central/pipeline/head This commit looks good Details

This commit is contained in:
Dandaedre 2026-10-19 13:54:12 +02:00
parent f4448c5662
commit 98ca7a1b61
5 changed files with 57 additions and 2 deletions

View File

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

View File

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

View File

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

View File

@ -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 }
}

View File

@ -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') }
}
}
}