首页 > 解决方案 > Powershell在启动时不运行颜色更改

问题描述

我一直在工作,但我刚刚更新了 windows,现在我的 powershell 配置文件没有正确更新颜色。

以前工作的配置文件代码:

Set-PSReadLineOption -Colors @{
"ContinuationPrompt" =     [ConsoleColor]:: DarkGray
"Emphasis" =     [ConsoleColor]:: DarkGray    
"Error" =     [ConsoleColor]:: DarkGray
"Selection" =     [ConsoleColor]:: DarkGray
"Default" =     [ConsoleColor]:: DarkGray
"Comment" =     [ConsoleColor]:: DarkGray
"Keyword" =     [ConsoleColor]:: DarkGray
"String" =     [ConsoleColor]:: DarkGray
"Operator" =     [ConsoleColor]:: DarkGray
"Variable" =     [ConsoleColor]:: DarkGray
"Command" =     [ConsoleColor]:: DarkGray
"Parameter" =     [ConsoleColor]:: DarkGray
"Type" =     [ConsoleColor]:: DarkGray
"Number" =     [ConsoleColor]:: DarkGray
"Member" =     [ConsoleColor]:: DarkGray
}

当我将 echo 添加到顶部时,它会在打开 powershell 时正确打印,但颜色不会改变。

echo "hello"
Set-PSReadLineOption -Colors @{
"ContinuationPrompt" =     [ConsoleColor]:: DarkGray
"Emphasis" =     [ConsoleColor]:: DarkGray    
"Error" =     [ConsoleColor]:: DarkGray
"Selection" =     [ConsoleColor]:: DarkGray
"Default" =     [ConsoleColor]:: DarkGray
"Comment" =     [ConsoleColor]:: DarkGray
"Keyword" =     [ConsoleColor]:: DarkGray
"String" =     [ConsoleColor]:: DarkGray
"Operator" =     [ConsoleColor]:: DarkGray
"Variable" =     [ConsoleColor]:: DarkGray
"Command" =     [ConsoleColor]:: DarkGray
"Parameter" =     [ConsoleColor]:: DarkGray
"Type" =     [ConsoleColor]:: DarkGray
"Number" =     [ConsoleColor]:: DarkGray
"Member" =     [ConsoleColor]:: DarkGray
}

标签: powershell

解决方案


实际上,它对我有用,你可以只说“深灰色”作为颜色。唯一需要注意的是,您不能在 powershell 5 中使用“`e”。请注意,Windows 终端在其方案中重新定义了颜色,就像任何控制台程序一样。

在转义序列方面,DarkGray 变为“亮黑色”或“esc[90m” https://en.wikipedia.org/wiki/ANSI_escape_code#Colors_ 控制台颜色和 ansi 颜色有一些不同的形容词。

psreadline中的一些常用代码。其中一些在 windows 终端的日晒方案中变得不可见,因为亮白色或亮黑色被设置为与背景相同的颜色。

Command            "$([char]0x1b)[31m" red # 93 bright yellow
Comment            "$([char]0x1b)[32m" green
ContinuationPrompt "$([char]0x1b)[37m" white # 33 yellow
DefaultToken       "$([char]0x1b)[37m" white
Emphasis           "$([char]0x1b)[96m" bright cyan
Error              "$([char]0x1b)[91m" bright red
Keyword            "$([char]0x1b)[92m" bright green
Member             "$([char]0x1b)[97m" bright white
Number             "$([char]0x1b)[97m" bright white
Operator           "$([char]0x1b)[90m" bright black
Parameter          "$([char]0x1b)[90m" bright black
Selection          "$([char]0x1b)[30;47m" black on white # 35;43 magenta;yellow
String             "$([char]0x1b)[36m" cyan
Type               "$([char]0x1b)[37m" white
Variable           "$([char]0x1b)[92m" bright green

推荐阅读