以下の内容はhttps://let.blog.jp/tag/WindowsTerminalより取得しました。


PowerShell でコマンドや変数の文字色を変える
ターミナルの黒画面に飽きて色を変えてみてますが コマンドの出力に色がついてるところだと 色が被ったり影響が出るのですよね
Windows Terminal だと設定の中にテーマを設定するところがあって そこで文字色や背景色を設定できます

wt-color

通常の文字色と背景色以外にも 黒や赤などの色も設定できます
背景色の上に表示してはっきりと文字が見えるように調整できます
色名は参考のものでしかないので 赤なのに実際の色は青みたいなこともできます
アプリケーション側で赤色って指定されたときに画面に出す色を設定する感じです
これのお陰で色を変えてもきれいに見れるように調整できます

これだけでもいいのですが PowerShell 側で色を付けてる部分は PowerShell の設定で変更できます
ユーザーが入力する部分で コマンドは黄色で変数は緑色みたいなのです

ps-color1

ここでどの色を出すかは PowerShell 側に設定があって Get-PSReadLineOption で取得できます

PS C:\Users\WDAGUtilityAccount> Get-PSReadLineOption

EditMode : Windows
AddToHistoryHandler :
HistoryNoDuplicates : True
HistorySavePath : C:\Users\WDAGUtilityAccount\AppData\Roaming\Microsoft\Windows\PowerShell\PSRea
dLine\ConsoleHost_history.txt
HistorySaveStyle : SaveIncrementally
HistorySearchCaseSensitive : False
HistorySearchCursorMovesToEnd : False
MaximumHistoryCount : 4096
ContinuationPrompt : >>
ExtraPromptLineCount : 0
PromptText : >
BellStyle : Audible
DingDuration : 50
DingTone : 1221
CommandsToValidateScriptBlockArguments : {ForEach-Object, %, Invoke-Command, icm...}
CommandValidationHandler :
CompletionQueryItems : 100
MaximumKillRingCount : 10
ShowToolTips : True
ViModeIndicator : None
WordDelimiters : ;:,.[]{}()/\|^&*-=+'"–—―
CommandColor : "$([char]0x1b)[93m"
CommentColor : "$([char]0x1b)[32m"
ContinuationPromptColor : "$([char]0x1b)[37m"
DefaultTokenColor : "$([char]0x1b)[37m"
EmphasisColor : "$([char]0x1b)[96m"
ErrorColor : "$([char]0x1b)[91m"
KeywordColor : "$([char]0x1b)[92m"
MemberColor : "$([char]0x1b)[97m"
NumberColor : "$([char]0x1b)[97m"
OperatorColor : "$([char]0x1b)[90m"
ParameterColor : "$([char]0x1b)[90m"
SelectionColor : "$([char]0x1b)[30;47m"
StringColor : "$([char]0x1b)[36m"
TypeColor : "$([char]0x1b)[37m"
VariableColor : "$([char]0x1b)[92m"

◯◯Color になってるところは実際の色で表示されています
コマンドの色を変更してみます

Set-PSReadLineOption -Colors @{ "Command"="$([char]0x1b)[33m" }

ps-color2

エスケープとして出力する文字が変わるものなので 過去ログは変わらず変更後の行だけ変わります
33 や 93 の数字が色に対応してます
エスケープの詳細は wikipedia 参照です
https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters

38 や 48 を使うと 8bit/24bit で色を絶対値指定できますが これを使うと色名を通さないのでユーザーが変更できないです
そうなると背景色等のカスタマイズと相性が悪いのであまり使わないほうが良さそうです



ちなみに上のは標準の PowerShell でしたが 新しい PowerShell だと少しプロパティが追加されていて エスケープの表記方法も変わっていました
「$([char]0x1b)」 → 「`e」 とシンプルになってます

PowerShell 7.4.1
PS C:\Users\WDAGUtilityAccount> Get-PSReadLineOption

EditMode : Windows
AddToHistoryHandler : System.Func`2[System.String,System.Object]
HistoryNoDuplicates : True
HistorySavePath : C:\Users\WDAGUtilityAccount\AppData\Roaming\Microsoft\Windows\PowerShell\PSRea
dLine\ConsoleHost_history.txt
HistorySaveStyle : SaveIncrementally
HistorySearchCaseSensitive : False
HistorySearchCursorMovesToEnd : False
MaximumHistoryCount : 4096
ContinuationPrompt : >>
ExtraPromptLineCount : 0
PromptText : {> }
BellStyle : Audible
DingDuration : 50
DingTone : 1221
CommandsToValidateScriptBlockArguments : {ForEach-Object, %, Invoke-Command, icm…}
CommandValidationHandler :
CompletionQueryItems : 100
MaximumKillRingCount : 10
ShowToolTips : True
ViModeIndicator : None
WordDelimiters : ;:,.[]{}()/\|!?^&*-=+'"–—―
AnsiEscapeTimeout : 100
PredictionSource : HistoryAndPlugin
PredictionViewStyle : InlineView
TerminateOrphanedConsoleApps : False
CommandColor : "`e[93m"
CommentColor : "`e[32m"
ContinuationPromptColor : "`e[37m"
DefaultTokenColor : "`e[37m"
EmphasisColor : "`e[96m"
ErrorColor : "`e[91m"
InlinePredictionColor : "`e[97;2;3m"
KeywordColor : "`e[92m"
ListPredictionColor : "`e[33m"
ListPredictionSelectedColor : "`e[48;5;238m"
ListPredictionTooltipColor : "`e[97;2;3m"
MemberColor : "`e[37m"
NumberColor : "`e[97m"
OperatorColor : "`e[90m"
ParameterColor : "`e[90m"
SelectionColor : "`e[30;47m"
StringColor : "`e[36m"
TypeColor : "`e[37m"
VariableColor : "`e[92m"

またスタイルに関連する情報が入っている変数 $PSStyle が存在します

PS C:\Users\WDAGUtilityAccount> $psstyle

Reset : `e[0m
BlinkOff : `e[25m
Blink : `e[5m
BoldOff : `e[22m
Bold : `e[1m
DimOff : `e[22m
Dim : `e[2m
Hidden : `e[8m
HiddenOff : `e[28m
Reverse : `e[7m
ReverseOff : `e[27m
ItalicOff : `e[23m
Italic : `e[3m
UnderlineOff : `e[24m
Underline : `e[4m
StrikethroughOff : `e[29m
Strikethrough : `e[9m
OutputRendering : Host
Formatting.FormatAccent : `e[32;1m
Formatting.ErrorAccent : `e[36;1m
Formatting.Error : `e[31;1m
Formatting.Warning : `e[33;1m
Formatting.Verbose : `e[33;1m
Formatting.Debug : `e[33;1m
Formatting.TableHeader : `e[32;1m
Formatting.CustomTableHeaderLabel : `e[32;1;3m
Formatting.FeedbackName : `e[33m
Formatting.FeedbackText : `e[96m
Formatting.FeedbackAction : `e[97m
Progress.Style : `e[33;1m
Progress.MaxWidth : 120
Progress.View : Minimal
Progress.UseOSCIndicator : False
FileInfo.Directory : `e[44;1m
FileInfo.SymbolicLink : `e[36;1m
FileInfo.Executable : `e[32;1m
FileInfo.Extension : .zip,.tgz,.gz,.tar,.nupkg,.cab,.7z,.ps1,.psd1,.psm1,.ps1xml
Foreground.Black : `e[30m
Foreground.BrightBlack : `e[90m
Foreground.White : `e[37m
Foreground.BrightWhite : `e[97m
Foreground.Red : `e[31m
Foreground.BrightRed : `e[91m
Foreground.Magenta : `e[35m
Foreground.BrightMagenta : `e[95m
Foreground.Blue : `e[34m
Foreground.BrightBlue : `e[94m
Foreground.Cyan : `e[36m
Foreground.BrightCyan : `e[96m
Foreground.Green : `e[32m
Foreground.BrightGreen : `e[92m
Foreground.Yellow : `e[33m
Foreground.BrightYellow : `e[93m
Background.Black : `e[40m
Background.BrightBlack : `e[100m
Background.White : `e[47m
Background.BrightWhite : `e[107m
Background.Red : `e[41m
Background.BrightRed : `e[101m
Background.Magenta : `e[45m
Background.BrightMagenta : `e[105m
Background.Blue : `e[44m
Background.BrightBlue : `e[104m
Background.Cyan : `e[46m
Background.BrightCyan : `e[106m
Background.Green : `e[42m
Background.BrightGreen : `e[102m
Background.Yellow : `e[43m
Background.BrightYellow : `e[103m

Set-PSReadLineOption で色を変更するときに色に対応する数字を覚えなくてもここから参照できて便利です

ここの変数を直接更新できないかなと試しましたが ReadOnly プロパティと言われてダメでした

PS C:\Users\WDAGUtilityAccount> $PSStyle.Background.Red = $PSStyle.Foreground.Red
InvalidOperation: 'Red' is a ReadOnly property.
PS C:\Users\WDAGUtilityAccount> $PSStyle | Get-Member background

TypeName: System.Management.Automation.PSStyle

Name MemberType Definition
---- ---------- ----------
Background Property System.Management.Automation.PSStyle+BackgroundColor Background {get;}

PS C:\Users\WDAGUtilityAccount> $PSStyle.Background | Get-Member black

TypeName: System.Management.Automation.PSStyle+BackgroundColor

Name MemberType Definition
---- ---------- ----------
Black Property string Black {get;}

getter しか用意されてないですね
Windows Sandbox で Windows Terminal を使う
未だに標準だと入ってないみたい
入れようにもサンドボックスの中ではストアが使えないのでストアを見れないしブラウザからインストールボタンを押してもストアが無いので何も起きない

仕方ないので Github の releases ページからインストーラーをダウンロードする
https://github.com/microsoft/terminal/releases

Windows10 と Windows11 版のそれぞれに msix ファイルと zip ファイルがある
zip の方は Windows10_PreinstallKit.zip というものでこれを先にインストールしないと msix のインストールに失敗する

ストアが無いせいか msix や appx ファイルはエクスプローラーから開けないので PowerShell でインストールする
zip の中は arm/arm64/x86/x64 とアーキテクチャごとの Microsoft.VCLibs の appx があって自動でやってくれなそうなので環境にあったのを選んでインストール
完了したら Windows Terminal 本体の msixbundle をインストール

Add-AppxPackage (zip の中の appx)
Add-AppxPackage (Windows Terminal の msixbundle)

新しい方の PowerShell は Add-AppxPackage コマンドレット自体はあるけどサポートしてないエラーが出るので古い方でやる
Windows Terminal でショートカットキーが優先される
WSL で nano を使って画面を下に移動するために Ctrl-V を押すと Windows Terminal 側のショートカットキーが優先されて クリップボードのテキストが貼り付けられる
ターミナルのショートカットは中で実行中のプログラムがハンドルしなかった場合のみでいいんだけど判定が難しいのかな
Windows Terminal の WSLtty で微妙にフォントが違う
WSLtty のウィンドウが多すぎてやっぱりタブが欲しくなってくる
Windows Terminal は稀に落ちてる問題があって 未だに PowerShell で一部使う程度
稀すぎてまだ起きてないのか修正されたのかここしばらくは落ちてない
落ちてたら落ちてたで再接続すればいいかなというのは Windows Terminal に移そうと思って設定を少しいじってて気づいた

文字の見た目がなんか微妙に違う

一応同じフォントでフォントサイズを選んでる
Windows Terminal は文字が詰まってるように見える
スクショを撮ってピクセル単位で比較してみると 文字の高さは一緒
幅は私の環境だと Windows Terminal のほうが 1 文字あたり 1px 狭い
行間も Windows Terminal のほうが狭い
ピクセル的にはほんのわずかな違いだけどすごく違って見える
1 文字あたりのサイズは 10px 前後だし 1 割も違えば十分?
特に文字幅は横に圧縮してるせいか Windows Terminal は少し滲んで見えて WSLtty のほうがくっきり見えてる
WSLtty の表示に揃えたいけどどうにか方法はないものか

文字はレンダリング方法の違いで不可能?
行間は設定できてもいいと思うけど Windows Terminal のリポジトリに issue が数年前からあって要望は多いのに未対応状態らしいしあまり期待できない
フォントの方を Windows Terminal できれいに見れるようにしたほうがいいのかも

あと Windows Terminal は背景の半透明がないのも不便
アクリルというのがあったけど ぼかした感じになるから後ろ側のウィンドウの文字が見えない
ウィンドウを重ねた状態で後ろのウィンドウの中身を見ながらコンソール操作ができない



以上の内容はhttps://let.blog.jp/tag/WindowsTerminalより取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14