3

I am using PowerShell 7 as terminal in PyCharm and the default colors for the commands were not really clear.

I created a profile.ps1 where I set them in this way:

# Set colours
Set-PSReadLineOption -Colors @{
    Command = "`e[30;107m"
    Comment = "`e[32;107m"
    ContinuationPrompt = "`e[30;47m"
    Emphasis = "`e[30;107m"
    Error = "`e[91;47m"
    InlinePrediction = "`e[90;47m"
    Keyword = "`e[92;47m"
    ListPrediction = "`e[33;47m"
    ListPredictionSelected = "`e[48;5;238m"
    Member = "`e[97;47m"
    Number = "`e[30;47m"
    Operator ="`e[90;47m"
    Parameter = "`e[90;47m"
    Selection = "`e[30;107m"
    String = "`e[36;47m"
    Type = "`e[30;47m"
    Variable =  "`e[92;47m"
}

but I couldn't find how to modify the Get-ChildItem result for the directory. As I am using a light theme, the result is not really clear.

Looking around it seems there are some solutions using a third-party command or complicated scripts, but I am wondering if in PowerShell 7 there is an easy way to do it with a command as Set-PSReadLineOption.

Any suggestions?

cicciodevoto
  • 133
  • 3

1 Answers1

3

Colour support for the output of Get-ChildItem was included in PR#14403 and released to v7.2.0.

It is configurable via the $PSStyle preference variable.

You should put any changes into your $PROFILE— as you've been doing— but you'll need to modify $PSStyle, not the PSReadLine options.

From the documentation:

$PSStyle.FileInfo is a nested object to control the coloring of FileInfo objects.

  • Directory - Built-in member to specify color for directories
  • SymbolicLink - Built-in member to specify color for symbolic links
  • Executable - Built-in member to specify color for executables.
  • Extension - Use this member to define colors for different file extensions. The Extension member pre-includes extensions for archive and PowerShell files. Note

NOTE $PSStyle.FileInfo is only available when the PSAnsiRenderingFileInfo experimental feature is enabled. For more information, see about_Experimental_Features and Using experimental features.

In PowerShell 7.3, the PSAnsiRenderingFileInfo feature became mainstream and available by default.

So to change file and directory colours, set the appropriate property in the $PSStyle.FileInfo object to the ANSI escape sequence representing the colour of your choice.

For example, to change the colour of directories to bright yellow:

# As separate Yellow and Bold attributes
$PSStyle.FileInfo.Directory = "`e[33;1m" # Note the double quotes!

Sequences 90-97 and 100-107 are also supported

$PSStyle.FileInfo.Directory = "`e[93m" # Note the double quotes!

The double quotes are essential, since otherwise ​`e will be interpreted literally, and you'll get a SetValueIncovationException error.

Whilst the documentation claims that 24-bit colour is also supported via the FromRgb method, that method is only available on the foreground and background colour styles (I'm not allowed to link to both classes' SDK documentation directly, since I'm at the hyperlink limit, so here's the SDK doc for System.Management.Automation.PSStyle).