如何将字符串的颜色保存到变量中

发布于 2025-01-23 00:23:49 字数 672 浏览 0 评论 0 原文

我正在使用PowerShell 5.1,我想将一些有色文本存储到变量中。我知道您可以使用写入式将一些彩色字符串打印到控制台中,如下所示:

“在此处输入图像描述”

但是,我似乎无法保存它们进入变量:

“在这里输入图像描述”

我尝试了各种解决方案,例如给出的解决方案此处” ,但似乎没有什么可用。

I'm using PowerShell 5.1, and I want to store some colored text into a variable. I'm aware that you can use Write-Host to print out some colored strings into the console, as shown below:

enter image description here

However, I can't seem to save them into a variable:

enter image description here

I've tried various solutions such as the ones given here but nothing seems to work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我不吻晚风 2025-01-30 00:23:49

您可以在变量中存储 witr-host 所需的参数参数:

$hi = @{ Object = 'hi!'; ForegroundColor = 'Red' }

# ... later
Write-Host @hi

@在变量路径/名称的前面称为 splatting 操作员,您可以在大约_splatting 帮助主题

You can store the parameter arguments you need for Write-Host in a variable:

$hi = @{ Object = 'hi!'; ForegroundColor = 'Red' }

# ... later
Write-Host @hi

The @ in front of the variable path/name is known as the splatting operator, you can read more in the about_Splatting help topic

凤舞天涯 2025-01-30 00:23:49

试图将文本输出分配到变量的原因, $ hi = write-host ... ,是因为 write> write-host not return return /em>它打印的文本 - 它将带有ANSI颜色代码的文本发送到控制台。

您可以找出一个方便的方案,用于在要打印的文本中包含颜色信息,并编写代码,该代码在将文本打印到屏幕上时使用该颜色信息,如其他已发布的答案所建议。

例如,

$text = @'
  { 
    "text": [["The quick ", "red"], ["brown fox ", "blue"], 
             ["jumped over ", "yellow"], ["the lazy ", "cyan"], 
             ["dog", "white"]]
  }
'@
($text | ConvertFrom-Json).text |
         % { Write-Host $_[0] -ForegroundColor $_[1] -NoNewLine }

这可能很乏味。也许检查 psgallery 查看是否有任何包装包的包装文本可以为您的应用程序提供便利的命令。

The reason the attempt to assign the text output to a variable, $hi = Write-Host ..., is because Write-Host doesn't return the text it prints - it sends the text with ANSI color codes to the console.

You could figure out a convenient scheme for including color information in the text you want to print, and write code that uses that color information as it prints the text to the screen, as suggested by the other posted answer.

For instance,

$text = @'
  { 
    "text": [["The quick ", "red"], ["brown fox ", "blue"], 
             ["jumped over ", "yellow"], ["the lazy ", "cyan"], 
             ["dog", "white"]]
  }
'@
($text | ConvertFrom-Json).text |
         % { Write-Host $_[0] -ForegroundColor $_[1] -NoNewLine }

This could be pretty tedious though. Maybe check PSGallery to see if there are any packages dealing with colorized text that provide commands convenient for your application.

笑着哭最痛 2025-01-30 00:23:49

使用格式使用PowerShell ANSI逃脱序列:

$([char] 0x1b)[#m

其中代表一个数字。 ANSI逃生序列的更多信息)

(请参阅在此处,有关 看起来像这样:

“在此处输入图像描述”

Use PowerShell ANSI escape sequences with the format:

$([char]0x1b)[#m

where # represents a number. (see here for more info about ANSI escape sequences)

In the case of the question above, it would look like this:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文