Why does PowerShell display “DarkYellow” text improperly?

这一生的挚爱 提交于 2019-12-18 19:08:35

问题


I'm using the Write-Host cmdlet to change the color of my prompt and I noticed that the DarkYellow color was not displaying as any sort of yellow, but as a gray! Here's a test of all the colors

PS> 0..15 | %{ Write-Host "Hello, world!" -ForegroundColor $_ }

I got a list of all the enum values by using a bad value

PS> Write-Host "Hello, World!" -ForegroundColor foo

Black, DarkBlue, DarkGreen, DarkCyan, DarkRed, DarkMagenta, DarkYellow, Gray, DarkGray, Blue, Green, Cyan, Red, Magenta, Yellow, White

And I realized that this list of "console colors" somehow corresponds to the settings for this console window/shortcut, right? So, DarkYellow would be this 7th one in and it sure looks gray.

What's the connection between ForegroundColor, System.ConsoleColor, and these console shortcut settings? Why is dark yellow displaying as gray "out of the box"? Should I change it? Will this change every console on my system or just this shortcut?

Why are consoles so finnicky on Windows?!


I'm on Windows 7 Pro SP1 64-bit and I'm launching the "Windows PowerShell" shortcut from the Start Menu.


回答1:


Lee Holmes' blog post, PowerShell's Noble Blue, talks about this a bit, but doesn't actually explain why DarkYellow was changed. Most likely the default DarkYellow was unreadable with the blue background.

As you've probably figured out - the System.ConsoleColor is slightly misnamed as it doesn't actually specify a color, it specifies an index into a color table which can be modified by shortcuts. This is unfortunately a limitation of the console subsystem.

If you change the shortcut PowerShell.lnk, you'll be changing the colors only for yourself and only when you start PowerShell from that link. If you use Start | Run, or say run cmd, then run PowerShell from cmd, you'll get different settings.

Lee's blog post should give you enough hints to get the behavior you prefer.




回答2:


I don't have the exact answers, but I have something that kind of works! I did some research (thanks, Jason Shirk) and found that System.ConsoleColor is just an index into the color table, which must be represented by this

The documentation does mention that DarkYellow is supposed to be "ochre" (RGB: 204, 119, 34).

| DarkYellow  | The color dark yellow (ochre). |

Choose "Defaults" from the application's top-left window menu and you'll see the generic "Console Windows Properties" (like above), which will modify any ConsoleWindowClass type window.

Pick the "Screen Text" option and you'll notice it's set to use the value in DarkYellow's spot. I don't know why they didn't use Gray or DarkGray right next to it! Click on the 7th box (the index for DarkYellow) and change it to ochre's RGB value. Then, set the "Screen Text" to the Gray or DarkGray (unless you really want ochre foreground text by default).

All of your Command Prompt, PowerShell, and Git Bash (mingw) consoles will be changed (mine were).


I did have a little trouble with the PowerShell prompt from the "run" dialog. It took the colors, but not some of my other settings (physical and buffer size, etc). I ended up using scoop to install concfg

PS> scoop install concfg
PS> scoop install sudo

and exported my good console settings

PS> concfg export > ~\.consolerc

and imported them into my misbehaving prompt with prejudice (you can use sudo, also from scoop, or just launch an admin prompt)

PS> sudo concfg import ~\.consolerc

And, actually, concfg seemed to export everything, the color table values and the selections! Here's a partial dump of my configuration (the color parts)

{
    "popup_colors":  "cyan,white",
    "dark_gray":  "#808080",
    "screen_colors":  "gray,dark_magenta",
    "dark_green":  "#008000",
    "blue":  "#0000ff",
    "dark_yellow":  "#cc7722",
    "red":  "#ff0000",
    "magenta":  "#ff00ff",
    "dark_red":  "#800000",
    "yellow":  "#ffff00",
    "dark_magenta":  "#012456",
    "cyan":  "#00ffff",
    "green":  "#00ff00",
    "dark_blue":  "#000080",
    "gray":  "#c0c0c0",
    "white":  "#ffffff",
    "black":  "#000000",
    "dark_cyan":  "#008080"
}


来源:https://stackoverflow.com/questions/21941606/why-does-powershell-display-darkyellow-text-improperly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!