Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
steve02081504 committed Nov 25, 2023
1 parent 1a9b1e8 commit ccf8ceb
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 69 deletions.
38 changes: 34 additions & 4 deletions esh/src/commands/esh_base.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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 {
Expand Down Expand Up @@ -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.'
Expand Down
5 changes: 3 additions & 2 deletions esh/src/main.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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) }
Expand Down
2 changes: 1 addition & 1 deletion esh/src/scripts/ValueEx.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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('.')) {
Expand Down
86 changes: 43 additions & 43 deletions esh/src/scripts/VirtualTerminal.ps1
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 0 additions & 14 deletions esh/src/scripts/minmax.ps1

This file was deleted.

2 changes: 2 additions & 0 deletions esh/src/system/UI/icon.ps1
Original file line number Diff line number Diff line change
@@ -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
8 changes: 3 additions & 5 deletions esh/src/system/UI/prompt/main.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
. "$($EshellUI.Sources.Path)/src/scripts/minmax.ps1"

$EshellUI.Prompt = ValueEx @{
$EshellUI.Prompt = ValueEx @{
Parent = $EshellUI
Builders = @{}
BuildMethods = ValueEx @{
Expand All @@ -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)) {
Expand All @@ -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) {
Expand Down

0 comments on commit ccf8ceb

Please sign in to comment.