自动在PowerShell中对齐两个点

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

Automatically Align Two Points in Powershell

问题

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

示例函数

  1. Write-Output "dirs`t: helloworld `
  2. optional`t: helloworld2"

不符合预期的输出

  1. dirs : helloworld
  2. optional : helloworld2

预期的输出

  1. dirs : helloworld
  2. 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

  1. Write-Output "dirs`t: helloworld `
  2. optional`t: helloworld2"

UnExpectedOutput

  1. dirs : helloworld
  2. optional : helloworld2

ExpectedOutput

  1. dirs : helloworld
  2. 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 一起,以获取所需的输出:

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

输出:

  1. dirs : helloworld
  2. optional : helloworld2
英文:

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

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

Output :

  1. dirs : helloworld
  2. optional : helloworld2

答案2

得分: 1

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

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

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

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

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

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

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

  1. $date = Get-Date
  2. "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.

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

自动在PowerShell中对齐两个点

Here are some other examples of formatted strings:

  1. $name = "Alice"
  2. $age = 25
  3. "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."

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

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

  1. $date = Get-Date
  2. "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的答案非常相似,但首先获取所需的最大填充量。

  1. $obj = [PSCustomObject]@{ dirs = 'helloworld'; optional = 'helloworld2' }
  2. # 获取对象的属性
  3. $properties = $obj.PSObject.Properties
  4. # 获取属性名称的最大长度并添加+1
  5. $padding = [System.Linq.Enumerable]::Max(
  6. [string[]] $properties.Name,
  7. [System.Func[string, int]] { $args[0].Length }) + 1
  8. # 现在只需一个简单的循环,将最大填充量添加到每个属性名称
  9. # 并连接分号+属性值
  10. foreach ($property in $properties) {
  11. $property.Name.PadRight($padding) + ': ' + $property.Value
  12. }

输出:

  1. dirs : helloworld
  2. optional : helloworld2

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

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

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.

  1. $obj = [PSCustomObject]@{ dirs = 'helloworld'; optional = 'helloworld2' }
  2. # Get the properties of the object
  3. $properties = $obj.PSObject.Properties
  4. # Get the Max Length of the property Names and add + 1
  5. $padding = [System.Linq.Enumerable]::Max(
  6. [string[]] $properties.Name,
  7. [System.Func[string, int]] { $args[0].Length }) + 1
  8. # Now its a simple loop adding the max padding to each property Name
  9. # and concatenating the semi-colon + property Value
  10. foreach ($property in $properties) {
  11. $property.Name.PadRight($padding) + ': ' + $property.Value
  12. }

Outputs:

  1. dirs : helloworld
  2. optional : helloworld2

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

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

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:

确定