有没有办法在 PowerShell 中居中文本 [英] Is there a way to center text in PowerShell

查看:60
本文介绍了有没有办法在 PowerShell 中居中文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何在 PowerShell 中居中文本?WindowWidth 显然不存在,那么有没有办法让文本居中?

How can we center text in PowerShell? WindowWidth doesn't exist apparently, so is there a way somehow to keep the text centered?

我们想要这个输出:

    *
   ***
  *****
 *******

所以我写了

for ($i=1; $i -le 7; $i+=2)
{
  write-host("*" * $i)
}

但是我们得到了

*
***
*****
*******

推荐答案

function Write-HostCenter { param($Message) Write-Host ("{0}{1}" -f (' ' * (([Math]::Max(0, $Host.UI.RawUI.BufferSize.Width / 2) - [Math]::Floor($Message.Length / 2)))), $Message) }

Write-HostCenter '*'
Write-HostCenter '***'
Write-HostCenter '*****'

这篇关于有没有办法在 PowerShell 中居中文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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