Format-Table 根据输出缓冲区宽度设置列宽 [英] Format-Table set column width depending on the output buffer width

查看:71
本文介绍了Format-Table 根据输出缓冲区宽度设置列宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 cmdlet,它使用 Format-Table 来输出可能很长的字符串(例如注册表路径).我想将每列宽度设置为输出缓冲区宽度除以列数.

示例:

function Write-Something {[CmdletBinding()] param()$o = [pscustomobject]@{ a = 'A' * 100;b = 'B' * 100 }$columnWidth = [int]( $PSCmdlet.Host.UI.RawUI.BufferSize.Width/2 )$o |格式表@{ e = 'a';宽度 = $columnWidth }, @{ e = 'b';宽度 = $columnWidth } -wrap}

这适用于控制台输出,它产生如下输出:

<前>乙- -AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA BBBBBBBBBBBB

问题

当我指定不同的输出缓冲区宽度时,使用 Out-StringOut-FileWidth 参数,格式将不会't 更改,它仍将基于控制台缓冲区宽度.

Write-Something |外文件 test.txt -Width 200

这会产生与上面相同的输出,而预期的输出应该是宽度为 100 的列,不会发生换行.

如何从我的 cmdlet 中获取由 Out-StringOut-FileWidth 参数设置的实际输出缓冲区宽度?

解决方案

问题是您已经在 Write-Something cmdlet 中固定了宽度.执行此操作的 PowerShell 方法是让您的 cmdlet 输出未格式化的数据对象,并将 Out-File 替换为您自己的控制输出宽度的 cmdlet.

function Write-Something {[CmdletBinding()] param()$o = [pscustomobject]@{ a = 'A' * 100;b = 'B' * 100 }写输出 $o}函数出东西{[CmdletBinding()] 参数([参数(ValueFromPipeline=$true)][psobject]$InputObject,[参数(位置=1)][字符串]$文件路径,[参数(位置=2)][int]$宽度)$columnWidth = [int]( $Width/2 )$输入对象 |格式表@{ e = 'a';宽度 = $columnWidth }, @{ e = 'b';width = $columnWidth } -Wrap |`外文件 -FilePath $FilePath -Width $Width}写东西 |Out-Something test.txt -Width 200

I have a cmdlet that uses Format-Table to output potentially long strings (such as registry paths). I would like to set each column width to the output buffer width divided by the number of columns.

Example:

function Write-Something {
    [CmdletBinding()] param()

    $o = [pscustomobject]@{ a = 'A' * 100; b = 'B' * 100 }
    
    $columnWidth = [int]( $PSCmdlet.Host.UI.RawUI.BufferSize.Width / 2 )
    $o | Format-Table @{ e = 'a'; width = $columnWidth }, @{ e = 'b'; width = $columnWidth } -wrap
}

This works nicely for console output, where it produces output like this:

a                                            b
-                                            -
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
AAAAAAAAAAAA                                 BBBBBBBBBBBB

Problem

When I specify a different output buffer width, using the Width argument of Out-String or Out-File, the formatting won't change, it will still be based on the console buffer width.

Write-Something | Out-File test.txt -Width 200

This produces the same output as above whereas the expected output should be columns of width 100, with no wrapping occuring.

How can I get the actual output buffer width set by the Width argument of Out-String or Out-File from within my cmdlet?

解决方案

The problem is that you've already fixed the width in your Write-Something cmdlet. The PowerShell way to do this would be for your cmdlet to output your unformatted data objects and for you to replace Out-File with your own cmdlet which controls the output width.

function Write-Something {
    [CmdletBinding()] param()

    $o = [pscustomobject]@{ a = 'A' * 100; b = 'B' * 100 }
    Write-Output $o
}

function Out-Something {
    [CmdletBinding()] param(
        [Parameter(ValueFromPipeline=$true)]
        [psobject]$InputObject,
        [Parameter(Position=1)]
        [String]$FilePath,
        [Parameter(Position=2)]
        [int]$Width
    )

    $columnWidth = [int]( $Width / 2 )
    $InputObject | Format-Table @{ e = 'a'; width = $columnWidth }, @{ e = 'b'; width = $columnWidth } -Wrap | ` 
        Out-File -FilePath $FilePath -Width $Width
}

Write-Something | Out-Something test.txt -Width 200

这篇关于Format-Table 根据输出缓冲区宽度设置列宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆