param([Parameter(Mandatory=$true)][string]$OutFile, [Parameter(Mandatory=$true)][string]$Work, [int]$HoldSeconds = 5) $lines = New-Object System.Collections.Generic.List[string] function L($s) { $lines.Add($s); [System.IO.File]::WriteAllLines($OutFile, $lines, (New-Object System.Text.UTF8Encoding($false))) } L ("PSVersion: " + $PSVersionTable.PSVersion + " CLR " + [System.Environment]::Version + " Redirected=" + [Console]::IsOutputRedirected) function Asm([string]$n) { return [AppDomain]::CurrentDomain.GetAssemblies() | Where-Object { $_.GetName().Name -eq $n } | Select-Object -First 1 } function T([string]$n, [string]$a) { $x = Asm $a; if ($x) { return $x.GetType($n) } else { return $null } } $isDesktop = ($PSVersionTable.PSEdition -eq 'Desktop') $fx = if ($isDesktop) { 'net461' } else { 'netstandard2.0' } L "" L "############ A) Spectre.Console 0.49.1 with the COMPLETE System.Memory closure side-loaded ############" $scOk = $false try { $order = @( (Join-Path $Work "system.runtime.compilerservices.unsafe-4.5.3\lib\net461\System.Runtime.CompilerServices.Unsafe.dll"), (Join-Path $Work "system.buffers-4.5.1\lib\net461\System.Buffers.dll"), (Join-Path $Work "system.numerics.vectors-4.5.0\lib\net46\System.Numerics.Vectors.dll"), (Join-Path $Work "system.memory-4.5.5\lib\$fx\System.Memory.dll"), (Join-Path $Work "sc049\lib\netstandard2.0\Spectre.Console.dll") ) L (" host=$($PSVersionTable.PSEdition) using System.Memory lib/$fx") foreach ($p in $order) { if (-not (Test-Path $p)) { throw "missing file: $p" } Add-Type -Path $p -ErrorAction Stop L (" loaded $([System.IO.Path]::GetFileName($p)) v$(([System.Reflection.AssemblyName]::GetAssemblyName($p)).Version) [$([System.IO.Path]::GetFileName([System.IO.Path]::GetDirectoryName($p)))]") } L " ALL dependencies loaded." $ansiT = T 'Spectre.Console.AnsiConsole' 'Spectre.Console' $tableT = T 'Spectre.Console.Table' 'Spectre.Console' $colT = T 'Spectre.Console.TableColumn' 'Spectre.Console' $extT = T 'Spectre.Console.TableExtensions' 'Spectre.Console' $t = [Activator]::CreateInstance($tableT) L " new Spectre.Console.Table() -> OK" $t.AddColumn([Activator]::CreateInstance($colT, [object[]]@('项目 / Item'))) $t.AddColumn([Activator]::CreateInstance($colT, [object[]]@('大小 / Size'))) $t.AddColumn([Activator]::CreateInstance($colT, [object[]]@('状态 / Status'))) $m = $extT.GetMethod('AddRow', [Type[]]@($tableT, [string[]])) $m.Invoke($null, [object[]]@($t, [string[]]@('中文备份归档 alpha', '1.2 GB', 'OK'))) | Out-Null $m.Invoke($null, [object[]]@($t, [string[]]@('beta', '340 MB', 'WARN'))) | Out-Null $m.Invoke($null, [object[]]@($t, [string[]]@('gamma-ascii', '2.0 GB', 'OK'))) | Out-Null Write-Host "" Write-Host (" ===== Spectre.Console 0.49.1 (netstandard2.0) on PowerShell " + $PSVersionTable.PSVersion + " =====") -ForegroundColor Cyan $ansiT::Write($t) Write-Host "" $ansiT::Markup('[red]red[/] [green]green[/] [yellow]yellow[/] [bold]bold[/] [blue on white]reverse[/]') $ansiT::Markup('[grey]grey[/] ' + [char]0x2713 + ' check ' + [char]0x26A0 + ' warn') L " AnsiConsole.Write(Table with CJK) + Markup colors + unicode symbols -> INVOKED, no exception" L (" Table rendered width (SafeBorder default): rows=" + ($t.Rows.Count) + " cols=" + ($t.Columns.Count)) $scOk = $true L "RESULT-A: SpectreConsole=WORKS" } catch { L (" FAILED: " + $_.Exception.GetType().Name + ': ' + $_.Exception.Message) $ie = $_.Exception.InnerException; while ($ie) { L (" inner: " + $ie.GetType().Name + ': ' + ($ie.Message -split "`n")[0]); $ie = $ie.InnerException } L "RESULT-A: SpectreConsole=FAILED" } L "" L "############ B) Terminal.Gui 1.15.0 - can the driver be hand-wired from PowerShell at all? ############" try { Add-Type -Path (Join-Path $Work 'nstack111\lib\netstandard2.0\NStack.dll') -ErrorAction Stop Add-Type -Path (Join-Path $Work 'tg115\lib\net472\Terminal.Gui.dll') -ErrorAction Stop $tgAsm = Asm 'Terminal.Gui' $appT = $tgAsm.GetType('Terminal.Gui.Application') $drvT = $tgAsm.GetType('Terminal.Gui.NetDriver') $mlT = $tgAsm.GetType('Terminal.Gui.IMainLoopDriver') $loopT = $tgAsm.GetType('Terminal.Gui.NetMainLoop') L (" Application=" + [bool]$appT + " NetDriver=" + [bool]$drvT + " NetMainLoop=" + [bool]$loopT) L (" Is NetMainLoop public? " + $loopT.IsPublic + " (a PowerShell user cannot 'new' a non-public type without reflection)") foreach ($c in $loopT.GetConstructors([System.Reflection.BindingFlags]'Public,NonPublic,Instance')) { L (" ctor " + $(if ($c.IsPublic) { 'PUBLIC' } else { 'internal' }) + ": NetMainLoop(" + (($c.GetParameters() | ForEach-Object { $_.ParameterType.Name + ' ' + $_.Name }) -join ', ') + ")") } $initM = $appT.GetMethod('Init', [Type[]]@($drvT, $mlT)) $drv = [Activator]::CreateInstance($drvT) # build NetMainLoop via reflection on the internal ctor $ctor = $loopT.GetConstructors([System.Reflection.BindingFlags]'Public,NonPublic,Instance') | Select-Object -First 1 $args = @() foreach ($p in $ctor.GetParameters()) { if ($p.ParameterType -eq $drvT) { $args += , $drv } elseif ($p.ParameterType -eq [bool]) { $args += , $true } else { $args += , $null } } $loop = $ctor.Invoke($args) L (" NetMainLoop constructed via reflection: " + [bool]$loop) $initM.Invoke($null, @($drv, $loop)) | Out-Null L " Application.Init(NetDriver, NetMainLoop) -> SUCCEEDED *** Terminal.Gui IS live on PowerShell " + $PSVersionTable.PSVersion + " ***" $winT = $tgAsm.GetType('Terminal.Gui.Window'); $lblT = $tgAsm.GetType('Terminal.Gui.Label'); $btnT = $tgAsm.GetType('Terminal.Gui.Button') $w = [Activator]::CreateInstance($winT, [object[]]@('BakNRet - Terminal.Gui on PS ' + $PSVersionTable.PSVersion)) $l1 = [Activator]::CreateInstance($lblT, [object[]]@('Terminal.Gui 1.15.0 (net472)')) $l2 = [Activator]::CreateInstance($lblT, [object[]]@('CJK: 2 Chinese chars = 4 columns')) $b1 = [Activator]::CreateInstance($btnT, [object[]]@('OK')) $w.Add($l1); $w.Add($l2); $w.Add($b1) $w.X = 1; $w.Y = 1; $w.Width = 78; $w.Height = 9 $l1.Y = 1; $l2.Y = 3; $b1.Y = 5; $b1.X = 2 ($appT.GetProperty('Top')).SetValue($null, $w) ($appT.GetProperty('Current')).SetValue($null, $w) $w.SetFocus(); $w.Redraw($w.Bounds) $appT.GetMethod('Refresh', [Type[]]@()).Invoke($null, @()) | Out-Null L " painted Window(78x9)+2 Labels+Button, holding " + $HoldSeconds + "s" Start-Sleep -Seconds $HoldSeconds $appT.GetMethod('Shutdown', [Type[]]@()).Invoke($null, @()) | Out-Null L " Application.Shutdown() -> clean" L "RESULT-B: TerminalGui=WORKS" } catch { L (" FAILED: " + $_.Exception.GetType().Name + ': ' + $_.Exception.Message) $ie = $_.Exception.InnerException; while ($ie) { L (" inner: " + $ie.GetType().Name + ': ' + ($ie.Message -split "`n")[0]); $ie = $ie.InnerException } L "RESULT-B: TerminalGui=FAILED" } if (-not [Console]::IsOutputRedirected) { Write-Host ""; Write-Host ("[done PS " + $PSVersionTable.PSVersion + "] closing in $HoldSeconds s") -ForegroundColor Yellow; Start-Sleep -Seconds $HoldSeconds }