如何在powershell中测量窗口高度(行数)?
例如,如下图所示,我的环境中的最大行数是47。 我可以通过编程方式测量此值吗如何在powershell中测量窗口高度(行数)?,powershell,powershell-ise,windows-console,Powershell,Powershell Ise,Windows Console,例如,如下图所示,我的环境中的最大行数是47。 我可以通过编程方式测量此值吗 您可以尝试以下方法 $(Get-Host).UI.RawUI.WindowSize.Height 但是请注意以下几点: 根据脚本的运行位置,使用RawUI可能不是特别的“可移植”例如,在“ISE”中,该属性不返回任何内容,而在控制台上它将工作 要了解控制台和ISE之间的更多差异,请运行$(Get Host).UI.RawUI,并比较输出 在其他PowerShell主机中,除了控制台或ISE之外,它可能仍然不同 还有
您可以尝试以下方法
$(Get-Host).UI.RawUI.WindowSize.Height
但是请注意以下几点:
- 根据脚本的运行位置,使用
可能不是特别的“可移植”例如,在“ISE”中,该属性不返回任何内容,而在控制台上它将工作 要了解控制台和ISE之间的更多差异,请运行RawUI
,并比较输出 在其他PowerShell主机中,除了控制台或ISE之外,它可能仍然不同$(Get Host).UI.RawUI
- 还有一个
,它可以大于WindowSize。后者是用户当前“可查看”的唯一零件BufferSize
- 获取主机对象的一种更简单、更高效的方法是使用
而不是$host
cmdletGet host
- 仅保证您能够访问PowerShell控制台主机中的窗口大小信息,即当在控制台(终端)窗口中运行时
- 是否公开这些信息由特定主机自行决定,而且,正如Christian所说,PowerShell ISE不公开这些信息,即使考虑到它内置了一个控制台子系统,可以说它应该公开这些信息
- 要测试代码是否在控制台(也称为终端)中运行,请使用
-$Host.UI.supportsVirtualAlterMinal
表示主机是控制台$True
如果在PowerShell控制台主机中运行: 您可以访问控制台的属性:
- 通过
(如Christian的回答所示),这是一个类型为$Host.UI.RawUI
的对象,仅在PowerShell控制台主机中包装.NET[System.Management.Automation.Internal.Host.InternalHostRawUserInterface]
类(见下文)[console]
- 通过;e、 例如,要通过这种方式获得窗口高度(行数),请使用:
[控制台]:窗口高度
$Host.UI.RawUI
输出:
> $Host.UI.RawUI
ForegroundColor : DarkYellow
BackgroundColor : DarkMagenta
CursorPosition : 0,58
WindowPosition : 0,0
CursorSize : 25
BufferSize : 160,9999
WindowSize : 160,75
MaxWindowSize : 160,94
MaxPhysicalWindowSize : 303,94
KeyAvailable : True
WindowTitle : Windows PowerShell
如果在PowerShell ISE中运行: 从PowerShell版本5.1开始编写 未填充
$Host.UI.RawUI
中的大多数属性,它们将返回其数据类型的默认值:
> $Host.UI.RawUI # in ISE
ForegroundColor : -1
BackgroundColor : -1
CursorPosition : 0,0
WindowPosition :
CursorSize :
BufferSize : 166,0
WindowSize :
MaxWindowSize :
MaxPhysicalWindowSize :
KeyAvailable :
WindowTitle : Windows PowerShell ISE
可用的唯一与大小相关的信息是缓冲区宽度(166
)
请注意,如果您试图直接从ISE使用[Console]
,则会出现以下异常:
我真正需要的功能是powershell ise中的
git status
,git diff
,具有突出显示和输出分页功能。
目前版本的powershell_ise似乎无法解决问题本身
虽然不完全令人满意,但我设计了一个实用的解决方案并创建了以下函数
源代码
截屏
感谢您的良好回复。当我测试它时,powershell中没有任何值。当我需要在powershell ise中拆分页面时,我想创建一个与“更多”对应的函数。现在我知道了powershell.exe中的最大行数,我想如果我能很好地应用它,它会对我有所帮助。好的,请注意,即使是内置的
more
在ISE中也不起作用。可能是因为那里没有必要的信息。谢谢,“[Console]::WindowHeight”它在powershell ise中工作得很好。@HyundongHwang:我很高兴听到它,但这与我的答案相矛盾,所以让我们试着找出原因:(a)当你粘贴[Console]::WindowHeight
在ISE的控制台窗格中,您真的返回了一个整数值(对我来说:上述异常)?(b) (获取命令powershell.Version.ToString()
报告了什么(对于我:10.0.15063.0
)?(c) $PSVersionTable.PSVersion.ToString()
报告了什么(对我来说:5.1.15063.413
)?是的,它与您的预期不同。我打印a、b和c值以进行调试,并捕获屏幕@HyundongHwang:谢谢-这太令人震惊了-我不能在美国英语系统上生成它-无论是64位还是32位。如果其他人有解释,请告诉我们。
> [Console]::WindowHeight
The handle is invalid. # EXCEPTION
...
function hhd-git-diff
{
[CmdletBinding()]
param
(
)
$res = git diff
hhd-git-colored-output -INPUT_OBJECT $res
}
function hhd-git-status
{
[CmdletBinding()]
param
(
)
$res = git status
hhd-git-colored-output -INPUT_OBJECT $res
}
function hhd-git-colored-output
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelinebyPropertyName=$true)]
[System.Object]
$INPUT_OBJECT
)
$lineNum = 1
$INPUT_OBJECT |
Out-String |
foreach {
$_ -split "\n" |
foreach {
if($lineNum % [Console]::WindowHeight -eq 0)
{
Read-Host "continue? press any key..."
}
$lineNum++
if($_ -like "diff --git *")
{
Write-Host ""
Write-Host ""
Write-Host ""
Write-Host ("#"*80) -ForegroundColor Cyan
Write-Host $_ -ForegroundColor Cyan
Write-Host ("#"*80) -ForegroundColor Cyan
Read-Host "continue? press any key..."
$lineNum = 1
}
elseif($_ -like "--- *")
{
}
elseif($_ -like "+++ *")
{
}
elseif($_ -like "-*")
{
Write-Host $_ -ForegroundColor Red
}
elseif($_ -like "+*")
{
Write-Host $_ -ForegroundColor Green
}
elseif($_ -like "On branch *")
{
Write-Host ("#"*80) -ForegroundColor Cyan
Write-Host $_ -ForegroundColor Cyan
}
elseif($_ -like "Your branch is*")
{
Write-Host $_ -ForegroundColor Cyan
Write-Host ("#"*80) -ForegroundColor Cyan
}
elseif($_ -like "Changes to be committed:*")
{
Write-Host ("-"*80) -ForegroundColor Green
Write-Host $_ -ForegroundColor Green
}
elseif($_ -like "Changes not staged for commit:*")
{
Write-Host ("-"*80) -ForegroundColor Red
Write-Host $_ -ForegroundColor Red
}
elseif($_ -like "Untracked files:*")
{
Write-Host ("-"*80) -ForegroundColor Black
Write-Host $_ -ForegroundColor Black
}
elseif($_ -like "*modified:*")
{
Write-Host $_ -ForegroundColor Yellow
}
elseif($_ -like "*deleted:*")
{
Write-Host $_ -ForegroundColor Red
}
else
{
Write-Host $_ -ForegroundColor Gray
}
}
}
}