Skip to content

Commit

Permalink
Add LoadingLog and Out- functions
Browse files Browse the repository at this point in the history
  • Loading branch information
steve02081504 committed Dec 6, 2023
1 parent b01d284 commit 7456980
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 19 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
运行`opt/install`以快速开始(你甚至不需要clone这个项目):

```powershell
{ (Invoke-WebRequest https://bit.ly/EshInstall).Content | Invoke-Expression }.Invoke()
& { (Invoke-WebRequest https://bit.ly/EshInstall).Content | Invoke-Expression }
```

Expand Down Expand Up @@ -127,7 +127,7 @@ rm -rf superhavyrock
或者如同安装时一样运行`opt/uninstall`

```powershell
{ (Invoke-WebRequest https://bit.ly/EshUnInstall).Content | Invoke-Expression }.Invoke()
& { (Invoke-WebRequest https://bit.ly/EshUnInstall).Content | Invoke-Expression }
```

Expand Down
17 changes: 12 additions & 5 deletions src/commands/copilot.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,27 @@ function global:Install-Copilot {
if (Test-Command winget) {
try {
winget install GitHub.cli
gh extension install github/gh-copilot
}
catch {
#install failed
Write-Error 'Error: Install github cli failed.'
Out-Error 'Install github cli failed.'
throw
}
}
else {
#winget not found
Write-Error 'Please install github cli first.'
throw
try {
Import-Module Appx -UseWindowsPowerShell
Add-AppxPackage -RegisterByFamilyName -MainPackage Microsoft.DesktopAppInstaller_8wekyb3d8bbwe
}
catch {
Out-Error 'Install github cli -> Install winget failed.'
throw
}
}
}
if (-not(gh extension list | Select-String 'github/gh-copilot')) {
gh extension install github/gh-copilot
}
}

function global:Copilot {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/special/linux_bins.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function global:cd {
$PreviousPath = Join-Path $PreviousPath $CurrentPath
$Path = $ChildPath
if (-not (Test-Path $PreviousPath)) {
$Host.UI.WriteErrorLine("bash: cd: ${PreviousPath}: No such file or directory")
Out-Error "bash: cd: ${PreviousPath}: No such file or directory"
}
#若是符号链接
if ((Get-Item $PreviousPath).Attributes -band [System.IO.FileAttributes]::ReparsePoint) {
Expand All @@ -64,7 +64,7 @@ function global:cd {
Set-Location $Path
}
else {
$Host.UI.WriteErrorLine("bash: cd: ${Path}: No such file or directory")
Out-Error "bash: cd: ${Path}: No such file or directory"
}
return
}
Expand Down
11 changes: 9 additions & 2 deletions src/fixers/VSCodeBackgroundJobsDisAbler.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
# fixer of https://github.com/PowerShell/vscode-powershell/issues/4851
if ($EshellUI.Im.VSCodeExtension) {
if ($EshellUI.Im.VSCodeExtension -and ($host.Version -le [version]'2023.8.0')) {
$EshellUI.BackgroundJobs.Add({
Unregister-Event -SubscriptionId $EshellUI.OtherData.IdleEvent.SubscriptionId -Force
$EshellUI.OtherData.Remove('IdleEvent')
}) | Out-Null
$EshellUI.LoadingLog.AddWarning(
"EshellUI's BackgroundJobs has been disabled due to a bug of PowerShell VSCode Extension.
See $(
$VirtualTerminal.Styles.Underline+$VirtualTerminal.Colors.BrightCyan
)https://github.com/PowerShell/vscode-powershell/issues/4851$(
$VirtualTerminal.Styles.NoUnderline+$VirtualTerminal.Colors[(Get-Host).PrivateData.WarningForegroundColor.ToString()]
) for more info."
)
}
33 changes: 31 additions & 2 deletions src/main.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,35 @@ $EshellUI = ValueEx @{
$this.Im.From = $this.GetMyFrom($Invocation)
}

LoadingLog = ValueEx @{
__type__ = [System.Collections.ArrayList]
ErrorLevel = &{enum ErrorLevel{
Info
Warning
Error
};[ErrorLevel]}
'method:AddLog' = {
param($What, $Level)
$this.Add([PSCustomObject]@{
What = $What
Level = $Level
}) | Out-Null
}
'method:AddInfo' = {param($What);$this.AddLog($What, $this.ErrorLevel::Info)}
'method:AddWarning' = {param($What);$this.AddLog($What, $this.ErrorLevel::Warning)}
'method:AddError' = {param($What);$this.AddLog($What, $this.ErrorLevel::Error)}
'method:Print' = {
$this | ForEach-Object {
$What = $_.What # 这个变量是必须的,$_在switch中会被更新为switch的参数
switch ($_.Level) {
$this.ErrorLevel::Info { Out-Info $What }
$this.ErrorLevel::Warning { Out-Warning $What }
$this.ErrorLevel::Error { Out-Error $What }
}
}
}
}

MSYS = @{
RootPath = 'C:\msys64'
}
Expand All @@ -68,7 +97,7 @@ $EshellUI = ValueEx @{
'method:PopAndRun' = {
$OriginalPref = $ProgressPreference # Default is 'Continue'
$ProgressPreference = 'SilentlyContinue'
$this.Pop().Invoke()
& $this.Pop()
$ProgressPreference = $OriginalPref
}
}
Expand Down Expand Up @@ -226,7 +255,7 @@ $EshellUI = ValueEx @{
)
$StartExecutionTime = Get-Date
try { Invoke-Expression $expr | Out-Default }
catch { $Host.UI.WriteErrorLine($_) }
catch { Out-Error $_ }
$EndExecutionTime = Get-Date
[PSCustomObject](@{
CommandLine = $expr
Expand Down
46 changes: 46 additions & 0 deletions src/scripts/Out.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
${global:Out-Performance} = @{
Warning = @{
Color = 'Yellow'
}
Error = @{
Color = 'Red'
}
Info = @{
Color = 'Blue'
}
}

function global:Out-Error {
param ($Value)
$Host.UI.WriteErrorLine(
$VirtualTerminal.Colors[
${global:Out-Performance}.Error.Color ??
(Get-Host).PrivateData.ErrorForegroundColor.ToString()
]+(
(($Value ?? $Input) | ForEach-Object { $_.ToString() }) -join "`r`n"
)+$VirtualTerminal.Colors.Default
)
}

function global:Out-Warning {
param ($Value)
$Host.UI.WriteWarningLine(
$VirtualTerminal.Colors[
${global:Out-Performance}.Warning.Color ??
(Get-Host).PrivateData.WarningForegroundColor.ToString()
]+(
(($Value ?? $Input) | ForEach-Object { $_.ToString() }) -join "`r`n"
)+$VirtualTerminal.Colors.Default
)
}

function global:Out-Info {
param ($Value)
$Host.UI.WriteLine(
$VirtualTerminal.Colors[
${global:Out-Performance}.Info.Color ?? 'Default'
]+(
(($Value ?? $Input) | ForEach-Object { $_.ToString() }) -join "`r`n"
)+$VirtualTerminal.Colors.Default
)
}
5 changes: 4 additions & 1 deletion src/scripts/ValueEx.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ function global:ValueEx ($ValueAndMethods) {
if ($_.Key.StartsWith('method:') -and ($_.Value -is [scriptblock])) {
Add-Member -InputObject $ValueExed -MemberType ScriptMethod -Name $_.Key.Substring(7) -Value $_.Value -Force
}
else {
elseif ($ValueExed -is [HashTable]) {
$ValueExed[$_.Key] = $_.Value
}
else {
Add-Member -InputObject $ValueExed -MemberType NoteProperty -Name $_.Key -Value $_.Value -Force
}
}
return ,$ValueExed
}
Expand Down
4 changes: 4 additions & 0 deletions src/system/UI/loaded.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ if ($error.Count -eq $EshellUI.OtherData.BeforeEshLoaded.Errors.Count) {

. $PSScriptRoot/logo.ps1
$EshellUI.Logo.Print()
if($EshellUI.LoadingLog.Count) {
$EshellUI.LoadingLog.Print()
Write-Host
}
2 changes: 1 addition & 1 deletion src/system/UI/loading.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ if ($EshellUI.Im.VSCodeExtension -and (-not $EshellUI.OtherData.ReloadSafeVariab
}
Write-Host 'E-Shell v1765.3.13'
Write-Host 'Loading...'
Write-Host ""
Write-Host
4 changes: 2 additions & 2 deletions src/system/UI/logo.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ $EshellUI.Logo = ValueEx @{
}
Write-Host -NoNewline "$($VirtualTerminal.Colors.Magenta) For $($VirtualTerminal.Styles.Italic)Windows Terminal $($VirtualTerminal.Styles.NoItalic)v$(Get-WindowsTerminalVersion)"
}#>
Write-Host ""
Write-Host
Write-Host "$($VirtualTerminal.Styles.Italic)$($VirtualTerminal.Colors.BrightMagenta)(c)$($VirtualTerminal.Colors.Reset) E-tek Corporation.$($VirtualTerminal.Styles.NoItalic) $($VirtualTerminal.Styles.Underline)All rights reserved$($VirtualTerminal.Styles.NoUnderline)."

$EshellUI.Hints.PrintRandom()

Write-Host ""
Write-Host
}
}
1 change: 1 addition & 0 deletions src/system/base.ps1
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
. "$($EshellUI.Sources.Path)/src/scripts/Out.ps1"
. "$($EshellUI.Sources.Path)/src/scripts/TestCommand.ps1"
. "$($EshellUI.Sources.Path)/src/scripts/VirtualTerminal.ps1"
4 changes: 2 additions & 2 deletions src/system/linux.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,15 @@ Set-PSReadLineKeyHandler -Key Enter -ScriptBlock {
}
#若当前行以/开头
if ($Executable.StartsWith("/") -or $Executable.StartsWith("~")) {
Write-Host ""
Write-Host
[Microsoft.PowerShell.PSConsoleReadLine]::CancelLine()
Write-Host "`b`b "
#则转换为windows路径
$Executable = LinuxPathToWindowsPath $Executable
#求值并输出
$StartExecutionTime = Get-Date
try { Invoke-Expression "$Executable $Rest *>&1" | Out-Default }
catch { $Host.UI.WriteErrorLine($_) }
catch { Out-Error $_ }
$EndExecutionTime = Get-Date
[Microsoft.PowerShell.PSConsoleReadLine]::AddToHistory($OriLine)
[PSCustomObject](@{
Expand Down

0 comments on commit 7456980

Please sign in to comment.