自动在PowerShell中对齐两个点

huangapple go评论62阅读模式
英文:

Automatically Align Two Points in Powershell

问题

当我留出一个制表符空格时,如何确保冒号在所有行上均匀分布?(我不想添加一个额外的转义制表符字符)

示例函数

Write-Output "dirs`t: helloworld `
optional`t: helloworld2"

不符合预期的输出

dirs    : helloworld
optional        : helloworld2

预期的输出

dirs    : helloworld
optional: helloworld2

一些 PowerShell 函数可以自动执行这个操作,如 键 : 值 ...
另外,我尝试不使用任何 PowerShell 内置函数(如 format-list、convert/to/from ... 等)来完成此操作。

英文:

When I leave one tab space, how can I ensure that the colon is evenly distributed on all lines? (I dont want to adding one more escape tab character)

Sample Function

Write-Output "dirs`t: helloworld `
optional`t: helloworld2"

UnExpectedOutput

dirs    : helloworld
optional        : helloworld2

ExpectedOutput

dirs    : helloworld
optional: helloworld2

> Some powershell functions can do this automatically as a key : value....
> Also, I try to do it without using any powershell built-in functions (format-list, convert/to/from ... etc.)

答案1

得分: 5

你可以使用cmdlet Format-ListPSCustomObject 一起,以获取所需的输出:

[PSCustomObject]@{ dirs="helloworld"; optional="helloworld2" } | Format-List

输出:

dirs     : helloworld
optional : helloworld2
英文:

You can use the cmdlet Format-List with a PSCustomObject to get your desired output :

[PSCustomObject]@{ dirs="helloworld"; optional="helloworld2" } | Format-List

Output :

dirs     : helloworld
optional : helloworld2

答案2

得分: 1

使用格式化字符串。这允许您指定稍后要插入的值的占位符。

$format = "{0,-10}: {1}"
Write-Output ($format -f "dirs", "`thelloworld")
Write-Output ($format -f "optional","`thelloworld2")

以下是一些其他格式化字符串的示例:

$name = "Alice"
$age = 25
"My name is {0} and I am {1} years old." -f $name, $age

上述代码将解析为:"My name is Alice and I am 25 years old."

$pi = 3.14159
"Pi to two decimal places is {0:N2}." -f $pi

上述代码将解析为:"Pi to two decimal places is 3.14."

$date = Get-Date
"Today's date in 'MM/dd/yyyy' format is {0:MM/dd/yyyy}." -f $date

上述代码将解析为:"Today's date in 'MM/dd/yyyy' format is 2023/06/28."

英文:

Use a formatted string. This lets you specify placeholders for the values to be inserted later.

$format = "{0,-10}: {1}"

Write-Output ($format -f "dirs", "`thelloworld")
Write-Output ($format -f "optional","`thelloworld2")

自动在PowerShell中对齐两个点

Here are some other examples of formatted strings:

$name = "Alice"
$age = 25
"My name is {0} and I am {1} years old." -f $name, $age

The above code will parse to: "My name is Alice and I am 25 years old."

$pi = 3.14159
"Pi to two decimal places is {0:N2}." -f $pi

The above code will parse to: "Pi to two decimal places is 3.14."

$date = Get-Date
"Today's date in 'MM/dd/yyyy' format is {0:MM/dd/yyyy}." -f $date

The above code will parse to: "Today's date in 'MM/dd/yyyy' format is 2023/06/28."

答案3

得分: 0

Cid's answer绝对是正确的答案,但根据评论,似乎您对Format-List使用的逻辑感兴趣,以将对象的属性和值作为列表呈现出来,因此这是一种方法,基本上与shadow2020的答案非常相似,但首先获取所需的最大填充量。

$obj = [PSCustomObject]@{ dirs = 'helloworld'; optional = 'helloworld2' }
# 获取对象的属性
$properties = $obj.PSObject.Properties
# 获取属性名称的最大长度并添加+1
$padding = [System.Linq.Enumerable]::Max(
    [string[]] $properties.Name,
    [System.Func[string, int]] { $args[0].Length }) + 1

# 现在只需一个简单的循环,将最大填充量添加到每个属性名称
# 并连接分号+属性值
foreach ($property in $properties) {
    $property.Name.PadRight($padding) + ': ' + $property.Value
}

输出:

dirs     : helloworld
optional : helloworld2

如果您还想要像Format-List一样的着色,可以插入VT Escape Sequences

foreach ($property in $properties) {
    "$([char] 27)[32;1m" +              # 绿色格式强调
    $property.Name.PadRight($padding) + # 属性名称
    ":$([char] 27)[0m "  +              # 分号+重置VT
    $property.Value                     # 属性值
}
英文:

Cid's answer is absolutely the right one but looking at comments it seems you're interested in the logic being used by Format-List to lay down the object's properties and values as a List, so here is one way you can do it, essentially is very similar to shadow2020's answer but first getting the max padding needed.

$obj = [PSCustomObject]@{ dirs = 'helloworld'; optional = 'helloworld2' }
# Get the properties of the object
$properties = $obj.PSObject.Properties
# Get the Max Length of the property Names and add + 1
$padding = [System.Linq.Enumerable]::Max(
    [string[]] $properties.Name,
    [System.Func[string, int]] { $args[0].Length }) + 1

# Now its a simple loop adding the max padding to each property Name
# and concatenating the semi-colon + property Value
foreach ($property in $properties) {
    $property.Name.PadRight($padding) + ': ' + $property.Value
}

Outputs:

dirs     : helloworld
optional : helloworld2

If you also want coloring like how Format-List does it you can insert VT Escape Sequences:

foreach ($property in $properties) {
    "$([char] 27)[32;1m" +              # Green Format Accent
    $property.Name.PadRight($padding) + # Property Name
    ":$([char] 27)[0m "  +              # Semi-colon + Reset VT
    $property.Value                     # Property Value
}

huangapple
  • 本文由 发表于 2023年6月29日 04:20:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76576490.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定