I have a program writing text outputs to STDOUT. I would like to filter and color these outputs. In PowerShell, I wrote a CmdLet, which parses text lines and emits them to the console and colors certain parts if needed. Example
In PowerShell I have such a function:
function Write-ColoredProgLine
{ [CmdletBinding()]
param(
[Parameter(ValueFromPipeline=$true)]
$InputObject,
[Parameter(Position=1)]
[switch]$SuppressWarnings = $false,
[Parameter(Position=2)]
[string]$Indent = ""
)
begin
{ $ErrorRecordFound = $false }
process
{ if (-not $InputObject)
{ Write-Host "Empty pipeline!" }
elseif ($InputObject -is [string])
{ if ($InputObject.StartsWith("vlib "))
{ Write-Host "${Indent}$InputObject" -ForegroundColor Gray } }
elseif ($InputObject.StartsWith("** Warning:") -and -not $SuppressWarnings)
{ Write-Host "${Indent}WARNING: " -NoNewline -ForegroundColor Yellow
Write-Host $InputObject.Substring(12)
}
elseif ($InputObject.StartsWith("** Error:") -and -not $SuppressWarnings)
{ $ErrorRecordFound += 1
Write-Host "${Indent}WARNING: " -NoNewline -ForegroundColor Yellow
Write-Host $InputObject.Substring(12)
}
}
else
{ Write-Host "Unsupported object in pipeline stream" }
}
end
{ $ErrorRecordFound }
}
Usage:
$expr = "prog.exe -flag -param foo"
$errors = Invoke-Expression $expr | Write-ColoredProgLine $false " "
How can I process such operations in Bash?
I need some kind of inner state in the filter script, so tools like GRC are not powerful enough.