简体   繁体   English

如何更改 PowerShell 5 中的字体属性?

[英]How to change font properties in PowerShell 5?

I want to make the font as bold for the path that I'm printing using Write-Host .我想将字体设置为使用Write-Host打印的路径的粗体。 I'm flexible to using other methods like echo or something else.我可以灵活地使用其他方法,例如 echo 或其他方法。

I've tried other methods like Write-Debug , etc, also checked the module WindowsConsoleFonts .我尝试了其他方法,例如Write-Debug等,还检查了模块WindowsConsoleFonts But none of them supports font properties like making them bold or italic while printing them.但是它们都不支持字体属性,例如在打印时将它们设为粗体或斜体。

$pathString = "[" + (Get-Location) + "]"
Write-Host $pathString -ForegroundColor Cyan

I'm using PowerShell 5.1 which doesn't support MarkDown rendering, else I would have done it using Markdown.我正在使用不支持 MarkDown 渲染的 PowerShell 5.1,否则我会使用 Markdown 来完成它。

You can achieve bold text via VT (Virtual Terminal) escape sequences .您可以通过VT(虚拟终端)转义序列实现粗体文本

However, regular Windows console windows ( conhost.exe ) do not support italics , and neither does their upcoming successor, Windows Terminal (at least as of this writing).但是,常规的 Windows 控制台 windows ( conhost.exe )支持斜体他们即将推出的继任者Windows 终端也不支持斜体(至少在撰写本文时)。 [1] [1]

In recent versions of Windows 10, support for VT sequences is enabled by default in both Windows PowerShell and PowerShell Core .在 Windows 10 的最新版本中,默认情况下在Windows PowerShellZ3D265B4E003EEEF0DDFZ8817B81FA中都启用了对 VT 序列的支持。

However, Write-Host has no support for them, so you must embed the escape sequences directly into the strings you pass to Write-Host (or strings you send to the success output stream, if it goes to the console):但是, Write-Host不支持它们,因此您必须将转义序列直接嵌入到传递给Write-Host的字符串中(或发送到成功 output stream 的字符串,如果它进入控制台):

Note:笔记:

  • I'm omitting Write-Host from the examples below, because it isn't strictly necessary, but colored text generally should indeed be written to the display (host), not to the success output stream.我在下面的示例中省略了Write-Host ,因为它不是绝对必要的,但通常确实应该将彩色文本写入显示器(主机),而不是成功 output stream。

  • While it is better to consistently use VT sequences for all formatting needs - including colors - it is possible to combine them with Write-Host -ForegroundColor / -BackgroundColor`.虽然最好始终使用 VT 序列来满足所有格式化需求 - 包括 colors - 可以将它们与Write-Host -ForegroundColor /结合使用。

PowerShell Core : PowerShell核心

PowerShell Core supports embedding escape sequences directly in "..." (double-quoted strings), via the `e escape sequence, which expands to a literal ESC character, which is the character that initiates a VT escape sequence. PowerShell Core 支持通过`e转义序列直接"..." (双引号字符串)中嵌入转义序列,该转义序列扩展为文字 ESC 字符,它是启动VT 转义序列的字符。

"To `e[1mboldly`e[m go ..., in `e[36mcyan`e[m."

Windows PowerShell : Windows PowerShell

There's no support for `e ;不支持`e ; the easiest solution is to use \e as placeholders and use -replace to substitute actual ESC characters (Unicode code point 0x1b ) for them:最简单的解决方案是使用\e作为占位符并使用-replace替换实际的 ESC 字符(Unicode 代码点0x1b ):

"To \e[1mboldly\e[m go ..., in \e[36mcyan\e[m." -replace '\\e', [char] 0x1b

[1] From PowerShell Core , you can run the following test command to see if the word italics prints in italics: [1] 从 PowerShell Core中,您可以运行以下测试命令来查看斜体字是否以italics打印:
"`e[3mitalics`e[m after"
Note that italics in the terminal are supported on macOS and at least in some Linux distros;请注意, macOS和至少某些Linux发行版支持终端的斜体; eg, Ubuntu 18.04例如,Ubuntu 18.04

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM