From ccf8cebe0f1849c3f40edae1084ed2eb8661f3bc Mon Sep 17 00:00:00 2001 From: steve02081504 Date: Sat, 25 Nov 2023 10:15:30 +0800 Subject: [PATCH] Update --- esh/src/commands/esh_base.ps1 | 38 +++++++++++-- esh/src/main.ps1 | 5 +- esh/src/scripts/ValueEx.ps1 | 2 +- esh/src/scripts/VirtualTerminal.ps1 | 86 ++++++++++++++--------------- esh/src/scripts/minmax.ps1 | 14 ----- esh/src/system/UI/icon.ps1 | 2 + esh/src/system/UI/prompt/main.ps1 | 8 +-- 7 files changed, 86 insertions(+), 69 deletions(-) delete mode 100644 esh/src/scripts/minmax.ps1 diff --git a/esh/src/commands/esh_base.ps1 b/esh/src/commands/esh_base.ps1 index 3ec3c43..33d901b 100644 --- a/esh/src/commands/esh_base.ps1 +++ b/esh/src/commands/esh_base.ps1 @@ -4,7 +4,20 @@ #设定别名esh Set-Alias esh EShell -Scope global +# TheSudoShadow函数用于将管理员窗口的输出保存到文件中以便在非管理员窗口中显示 +function global:TheSudoShadow { + param( + $Command, + $UUID=$(New-Guid).Guid + ) + $SudoShadowFile = "$env:Temp/sudo_shadows/$UUID.txt" + Start-Transcript -Path $SudoShadowFile -UseMinimalHeader | Out-Null + Invoke-Expression $Command + Stop-Transcript | Out-Null + Write-Host "Sudo shadow file was saved to $SudoShadowFile" +} . "$($EshellUI.Sources.Path)/src/scripts/shell_args_convert.ps1" +[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") function global:sudo { param( [Parameter(ValueFromRemainingArguments = $true)] @@ -25,18 +38,35 @@ function global:sudo { } else { if ($EshellUI.Im.Sudo) { Invoke-Expression "$RemainingArguments" + return } # Otherwise, run the command as an admin - elseif (Test-Command wt.exe) { - $Arguments = @('pwsh','-Command',$(pwsh_args_convert $RemainingArguments)) + $UUID=$(New-Guid).Guid + $ShadowCommand = "TheSudoShadow -UUID '$UUID' -Command '$(pwsh_args_convert $RemainingArguments)'" + if (Test-Command wt.exe) { + $Arguments = @('pwsh','-Command', $ShadowCommand) $Arguments = cmd_args_convert $Arguments Start-Process -Wait -FilePath 'wt.exe' -ArgumentList $Arguments.Replace('"','\"') -Verb runas } else { - $Arguments = @('-Command',$(pwsh_args_convert $RemainingArguments)) + $Arguments = @('-Command', $ShadowCommand) $Arguments = cmd_args_convert $Arguments Start-Process -Wait -FilePath 'pwsh.exe' -ArgumentList "$pwshArguments $Arguments" -Verb runas } + $shadow=Get-Content "$env:Temp/sudo_shadows/$UUID.txt" + Remove-Item "$env:Temp/sudo_shadows/$UUID.txt" + $shadow = $shadow | Select-Object -Skip 4 -SkipLast 4 + #由于Start-Transcript会将宽字符重复写入,所以对于每一个字符在$shadow中进行渲染以获取其宽度,去除多余的字符 + $shadow = $shadow -join "`n" + $Font = New-Object System.Drawing.Font('cascadia mono', 128) + $Width = 0 + ($shadow.ToCharArray() | ForEach-Object { + if($Width -eq 0) { + $Width = [Math]::Max([Math]::Floor([System.Windows.Forms.TextRenderer]::MeasureText($_, $Font).Width/128)-1,0) + $_ + } + else { $Width-- } + }) -join '' | Write-Host } } function global:mklink { @@ -76,7 +106,7 @@ function global:shutdown { shutdown.exe $RemainingArguments } } -Set-Alias global:poweroff shutdown +Set-Alias poweroff shutdown -Scope global function global:poweron { Write-Host 'This computer is already powered on.' diff --git a/esh/src/main.ps1 b/esh/src/main.ps1 index 775b436..2da6d19 100644 --- a/esh/src/main.ps1 +++ b/esh/src/main.ps1 @@ -115,6 +115,7 @@ $EshellUI = ValueEx @{ $EventList = Get-EventSubscriber -Force $this.OtherData.ExitingEvent = $EventList[$EventList.Count-2] $this.OtherData.IdleEvent = $EventList[$EventList.Count-1] + Remove-Variable EventList . $PSScriptRoot/system/base.ps1 @@ -170,8 +171,8 @@ $EshellUI = ValueEx @{ } $this.SaveVariables() $function:prompt = $this.OtherData.BeforeEshLoaded.promptBackup - Unregister-Event -SubscriptionId $this.OtherData.ExitingEvent.SubscriptionId - Unregister-Event -SubscriptionId $this.OtherData.IdleEvent.SubscriptionId + Unregister-Event -SubscriptionId $this.OtherData.ExitingEvent.SubscriptionId -Force + Unregister-Event -SubscriptionId $this.OtherData.IdleEvent.SubscriptionId -Force $this.ProvidedFunctions() | ForEach-Object { Remove-Item function:\$($_.Name) } $this.ProvidedVariables() | ForEach-Object { Remove-Item variable:\$($_.Name) } $this.ProvidedAliases() | ForEach-Object { Remove-Item alias:\$($_.Name) } diff --git a/esh/src/scripts/ValueEx.ps1 b/esh/src/scripts/ValueEx.ps1 index 80c0aa6..607780b 100644 --- a/esh/src/scripts/ValueEx.ps1 +++ b/esh/src/scripts/ValueEx.ps1 @@ -25,7 +25,7 @@ } return ,$ValueExed } -function IndexEx ($Value,$Index,[switch]$Set = $false,$ValueToSet) { +function global:IndexEx ($Value,$Index,[switch]$Set = $false,$ValueToSet) { if (-not $Index) { return ,$Value } if ($Index.Contains('.')) { while ($Index.Contains('.')) { diff --git a/esh/src/scripts/VirtualTerminal.ps1 b/esh/src/scripts/VirtualTerminal.ps1 index c14797b..75c8dba 100644 --- a/esh/src/scripts/VirtualTerminal.ps1 +++ b/esh/src/scripts/VirtualTerminal.ps1 @@ -1,60 +1,60 @@ if ($Host.UI.SupportsVirtualTerminal) { - $Escape = [char]27 + '[' + function Escape { [char]27 + '[' + $args } $global:VirtualTerminal = @{ - Escape = $Escape + 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' + 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' + 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' + ResetAll = Escape '0m' + ResetColors = Escape '39m' + ResetStyles = Escape '23m' #保存当前光标位置 - SaveCursor = $Escape + 's' + SaveCursor = Escape 's' #恢复光标位置 - RestoreCursor = $Escape + 'u' + RestoreCursor = Escape 'u' #清除从光标到行尾的内容 - ClearLine = $Escape + 'K' + ClearLine = Escape 'K' #清除从光标到行首的内容 - ClearLineLeft = $Escape + '1K' + ClearLineLeft = Escape '1K' #清除整行 - ClearLineAll = $Escape + '2K' + ClearLineAll = Escape '2K' #清除从光标到屏幕底部的内容 - ClearScreenDown = $Escape + 'J' + ClearScreenDown = Escape 'J' #清除从屏幕顶部到光标的内容 - ClearScreenUp = $Escape + '1J' + ClearScreenUp = Escape '1J' #清除整屏 - ClearScreenAll = $Escape + '2J' + ClearScreenAll = Escape '2J' } - Remove-Variable Escape + Remove-Item function:Escape } diff --git a/esh/src/scripts/minmax.ps1 b/esh/src/scripts/minmax.ps1 deleted file mode 100644 index 1cc7920..0000000 --- a/esh/src/scripts/minmax.ps1 +++ /dev/null @@ -1,14 +0,0 @@ -function global:Max { - param( - [Parameter(ValueFromRemainingArguments = $true)] - [int[]]$RemainingArguments - ) - $RemainingArguments | Measure-Object -Maximum | Select-Object -ExpandProperty Maximum -} -function global:Min { - param( - [Parameter(ValueFromRemainingArguments = $true)] - [int[]]$RemainingArguments - ) - $RemainingArguments | Measure-Object -Minimum | Select-Object -ExpandProperty Minimum -} diff --git a/esh/src/system/UI/icon.ps1 b/esh/src/system/UI/icon.ps1 index b224677..3c1518f 100644 --- a/esh/src/system/UI/icon.ps1 +++ b/esh/src/system/UI/icon.ps1 @@ -1,2 +1,4 @@ . "$($EshellUI.Sources.Path)/src/scripts/Console.ps1" Set-ConsoleIcon "$($EshellUI.Sources.Path)/img/cmd.ico" +Remove-Item function:Set-ConsoleIcon +Remove-Item function:Set-WindowIcon diff --git a/esh/src/system/UI/prompt/main.ps1 b/esh/src/system/UI/prompt/main.ps1 index 279105b..9717eea 100644 --- a/esh/src/system/UI/prompt/main.ps1 +++ b/esh/src/system/UI/prompt/main.ps1 @@ -1,6 +1,4 @@ -. "$($EshellUI.Sources.Path)/src/scripts/minmax.ps1" - -$EshellUI.Prompt = ValueEx @{ +$EshellUI.Prompt = ValueEx @{ Parent = $EshellUI Builders = @{} BuildMethods = ValueEx @{ @@ -9,7 +7,7 @@ $EshellUI.Prompt = ValueEx @{ [Parameter(Mandatory = $true)] [string]$prompt_str ) - $LastLineIndex = Max $prompt_str.LastIndexOf('`n') 0 + $LastLineIndex = [Math]::Max($prompt_str.LastIndexOf('`n'),0) $LastLine = $prompt_str.Substring($LastLineIndex) #如果$prompt_str最后一行长度大于$Host.UI.RawUI.WindowSize.Width/2则换行 if ($LastLine.Length -gt ($Host.UI.RawUI.WindowSize.Width / 2)) { @@ -19,7 +17,7 @@ $EshellUI.Prompt = ValueEx @{ } 'method:AddBlock' = { param($prompt_str,$block_str) - $LastLineIndex = Max $prompt_str.LastIndexOf('`n') 0 + $LastLineIndex = [Math]::Max($prompt_str.LastIndexOf('`n'),0) $LastLine = $prompt_str.Substring($LastLineIndex) #如果$LastLine + $block_str长度大于$Host.UI.RawUI.WindowSize.Width则换行 if (($LastLine + $block_str).Length -gt $Host.UI.RawUI.WindowSize.Width) {