core/utils/text.ps1
$PSCompletions | Add-Member -MemberType ScriptMethod fn_get_content { param ([string]$path) $res = Get-Content $path -Encoding utf8 -ErrorAction SilentlyContinue | Where-Object { $_ -ne '' } if ($res) { return $res } return $null } $PSCompletions | Add-Member -MemberType ScriptMethod fn_get_raw_content { param ([string]$path, [bool]$trim = $true) $res = Get-Content $path -Raw -Encoding utf8 -ErrorAction SilentlyContinue if ($res) { if ($trim) { return $res.Trim() } return $res } return $null } $PSCompletions | Add-Member -MemberType ScriptMethod fn_replace { param ($data) $data = $data -join '' $pattern = '\{\{(.*?(\})*)(?=\}\})\}\}' $matches = [regex]::Matches($data, $pattern) foreach ($match in $matches) { $data = $data.Replace($match.Value, (Invoke-Expression $match.Groups[1].Value)) } if ($data -match $pattern) { $PSCompletions.fn_replace($data) }else { return $data } } $PSCompletions | Add-Member -MemberType ScriptMethod fn_write { param([string]$str) $color_list = @() $str = $str -replace "`n", 'n&&_n_n&&' $str_list = $str -split '(<\$[^>]+>.*?(?=<\$|$))' | Where-Object { $_ -ne '' } | ForEach-Object { if ($_ -match '<\$([\s\w]+)>(.*)') { ($matches[2] -replace 'n&&_n_n&&', "`n") -replace '^<\$>', '' $color = $matches[1] -split ' ' $color_list += @{ color = $color[0] bgcolor = $color[1] } } else { ($_ -replace 'n&&_n_n&&', "`n") -replace '^<\$>', '' $color_list += @{} } } $str_list = [array]$str_list for ($i = 0; $i -lt $str_list.Count; $i++) { $color = $color_list[$i].color $bgcolor = $color_list[$i].bgcolor if ($color) { if ($bgcolor) { Write-Host $str_list[$i] -f $color -b $bgcolor -NoNewline } else { Write-Host $str_list[$i] -f $color -NoNewline } } else { Write-Host $str_list[$i] -NoNewline } } Write-Host '' } |