From 75cd921a7c5d7bfbc04285bb30bd6a376aa50523 Mon Sep 17 00:00:00 2001 From: steve02081504 Date: Mon, 4 Mar 2024 22:45:00 +0800 Subject: [PATCH] colors --- src/HelpShower.ps1 | 41 ++++++++++++++++++++++++++- src/VirtualTerminal.ps1 | 61 +++++++++++++++++++++++++++++++++++++++++ src/locale/en-UK.ps1 | 28 +++++++++---------- src/locale/es-ES.ps1 | 28 +++++++++---------- src/locale/zh-CN.ps1 | 28 +++++++++---------- 5 files changed, 143 insertions(+), 43 deletions(-) create mode 100644 src/VirtualTerminal.ps1 diff --git a/src/HelpShower.ps1 b/src/HelpShower.ps1 index fc3f4db6..f0e66516 100644 --- a/src/HelpShower.ps1 +++ b/src/HelpShower.ps1 @@ -1,6 +1,13 @@ param ( [hashtable]$HelpData ) +. $PSScriptRoot/VirtualTerminal.ps1 +function Showuseage($Usage) { + $Usage -replace '\-(\w+)', "$($VirtualTerminal.Colors.BrightYellow)-`$1$($VirtualTerminal.Colors.Reset)"` + -replace "'([^']+)'", "$($VirtualTerminal.Colors.BrightMagenta)'`$1'$($VirtualTerminal.Colors.Reset)"` + -replace '(\w+)=', "$($VirtualTerminal.Colors.BrightGreen)`$1$($VirtualTerminal.Colors.Reset)="` + -replace '\[([a-zA-Z]+)', "[$($VirtualTerminal.Colors.BrightGreen)`$1$($VirtualTerminal.Colors.Reset)"` +} function ShowParamsHelp($ParamsHelpData) { # 对于所有的键 $MaxKeyLength = $ParamsHelpData.Keys.Length | Measure-Object -Maximum | Select-Object -ExpandProperty Maximum @@ -9,9 +16,41 @@ function ShowParamsHelp($ParamsHelpData) { $Key = $_ $Value = $ParamsHelpData[$Key] + # 在Vlaue中寻找``包裹的内容,对其进行色彩化 + while ($Value -match '`(?[^\`]+)`') { + $str = $Matches['coloringstr'] + $newstr = $str + $color = $VirtualTerminal.Colors.BrightBlue + if(($str.StartsWith('"') -and $str.EndsWith('"')) -or ($str.StartsWith("'") -and $str.EndsWith("'"))) { + # 字符串,淡紫色渲染 + $color = $VirtualTerminal.Colors.BrightMagenta + } + elseif ($str.IndexOf('::') -ge 0) { + $newstr = $str.Replace('::', "$($VirtualTerminal.ResetAll)::$($VirtualTerminal.Colors.BrightYellow)") + } + elseif ($str.StartsWith('-') -or $ParamsHelpData.Keys -ccontains $str) { + # 选项,淡黄色渲染 + $color = $VirtualTerminal.Colors.BrightYellow + } + elseif ($str.IndexOf('://') -ge 0) { + # URL,淡蓝色渲染+下划线 + $color += $VirtualTerminal.Styles.Underline + } + elseif($str -match '^%\w+%$') { + # 环境变量,绿色渲染 + $color = $VirtualTerminal.Colors.BrightGreen + } + elseif(Get-Command $str -ErrorAction Ignore) { + # 命令,黄色渲染 + $color = $VirtualTerminal.Colors.BrightYellow + } + $Value = $Value.Replace("``$str``", "$color$newstr$($VirtualTerminal.ResetAll)") + } + $Spaces = ' ' * ($MaxKeyLength - $Key.Length) - "$Key$Spaces : $Value" + "$($VirtualTerminal.Colors.BrightYellow)$Key$Spaces$($VirtualTerminal.Colors.Reset) : $Value" } } $HelpData.title +Showuseage $HelpData.Usage ShowParamsHelp $HelpData.PrarmsData diff --git a/src/VirtualTerminal.ps1 b/src/VirtualTerminal.ps1 new file mode 100644 index 00000000..f2e36ea6 --- /dev/null +++ b/src/VirtualTerminal.ps1 @@ -0,0 +1,61 @@ +# 代码截取并修改自esh,所有权和许可证由esh项目保留 +if ($Host.UI.SupportsVirtualTerminal) { + function Escape { [char]27 + '[' + $args } + $VirtualTerminal = @{ + Escape = Escape + Colors = @{ + Black = Escape '30m' + Red = Escape '31m' + Green = Escape '32m' + Yellow = Escape '33m' + Blue = Escape '34m' + Magenta = Escape '35m' + Cyan = Escape '36m' + White = Escape '37m' + Default = Escape '39m' + BrightBlack = Escape '90m' + BrightRed = Escape '91m' + BrightGreen = Escape '92m' + BrightYellow = Escape '93m' + BrightBlue = Escape '94m' + BrightMagenta = Escape '95m' + BrightCyan = Escape '96m' + BrightWhite = Escape '97m' + Reset = Escape '39m' + } + Styles = @{ + Italic = Escape '3m' + Underline = Escape '4m' + Blink = Escape '5m' + Reverse = Escape '7m' + Hide = Escape '8m' + NoItalic = Escape '23m' + NoUnderline = Escape '24m' + NoBlink = Escape '25m' + NoReverse = Escape '27m' + NoHide = Escape '28m' + Reset = Escape '23m' + } + ResetAll = Escape '0m' + ResetColors = Escape '39m' + ResetStyles = Escape '23m' + + #保存当前光标位置 + SaveCursor = Escape 's' + #恢复光标位置 + RestoreCursor = Escape 'u' + #清除从光标到行尾的内容 + ClearLine = Escape 'K' + #清除从光标到行首的内容 + ClearLineLeft = Escape '1K' + #清除整行 + ClearLineAll = Escape '2K' + #清除从光标到屏幕底部的内容 + ClearScreenDown = Escape 'J' + #清除从屏幕顶部到光标的内容 + ClearScreenUp = Escape '1J' + #清除整屏 + ClearScreenAll = Escape '2J' + } + Remove-Item function:Escape +} diff --git a/src/locale/en-UK.ps1 b/src/locale/en-UK.ps1 index d64daefd..616cfe74 100644 --- a/src/locale/en-UK.ps1 +++ b/src/locale/en-UK.ps1 @@ -12,8 +12,8 @@ CfgFileLabelHead = "Configuration file, my good sir:" # Console GUIHelpData = @{ - title = "Usage: -ps12exeGUI [[-ConfingFile] ''] [-Localize ''] [-UIMode 'Dark'|'Light'|'Auto'] [-help]" + title = "Usage:" + Usage = "ps12exeGUI [[-ConfingFile] ''] [-Localize ''] [-UIMode 'Dark'|'Light'|'Auto'] [-help]" PrarmsData = [ordered]@{ ConfingFile = "The configuration file to load." Localize = "The language code to use." @@ -22,8 +22,8 @@ ps12exeGUI [[-ConfingFile] ''] [-Localize ''] [-UIMode ' } } ConsoleHelpData = @{ - title = "Usage: -[input |] ps12exe [[-inputFile] '' | -Content '