网上没找到太多有用资料,因此自己分享出来一点解决方案。
安装 oh my posh
这里使用 oh-my-posh
美化 powershell。使用如下命令安装。
winget install JanDeDobbeleer.OhMyPosh -s winget
修改配置文件
并且将字段 oh-my-posh init pwsh --config 'the path to theme you like' | Invoke-Expression
添加到 powershell 配置文件中。当然刚开始是没有选择主题的,你可以先不添加 --config 'the path to theme you like'
,之后选择好主题再添加。使用命令 notepad $PROFILE
编辑 powershell 配置文件。
如果没有的话使用 New-Item -Type File -Path $PROFILE -Force
先创建一个 powershell 配置文件,再添加上述的字段。
输入 . $PROFILE
,应用配置,接下来安装一个富文本字体,使用命令 oh-my-posh font install
,选择一个自己喜欢的字体。
添加美化主题
之后可以到路径 'C:\Users\username\AppData\Local\Programs\oh-my-posh\themes
下找到自己喜欢的主题,可以到这个网址预览一下自己喜欢的主题,之后将其完整路径补全到 $PROFILE
中的 oh-my-posh init pwsh --config 'the path to theme you like' | Invoke-Expression
里。应用配置或者重启终端后应该就可以看到效果了。
至此都还挺好的,但是有一个缺点,就是无法显示 anaconda 虚拟环境,我是用如下的配置(可以直接修改你选择的主题 json),其中 'type'='python'
的块是关键,你可以把它添加到你喜欢的主题 json 文件中,就会自动显示 anaconda 虚拟环境名以及 python 版本,相当 nice。如果你还有其他想要自行配置的,可以到 oh-my-posh 官网学习一下配置语法。
配置文件
{
"$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
"blocks": [
{
"alignment": "left",
"newline": true,
"segments": [
{
"foreground": "#E5C07B",
"properties": {
"time_format": "15:04"
},
"style": "plain",
"template": "[{{ .CurrentDate | date .Format }}]",
"type": "time"
},
{
"type": "shell",
"style": "plain",
"foreground": "#E06C75",
"properties": {
"mapped_shell_names": {
"pwsh": "Shell",
"powershell": "Shell",
"cmd": "Cmd",
"bash": "Bash"
}
},
"template": " \ue795 {{ .Name }}"
},
{
"type": "python",
"style": "plain",
"properties": {
"display_mode": "environment",
"fetch_virtual_env": true,
"home_enabled": true
},
"powerline_symbol": "",
"foreground": "#906cff",
"template": " {{ if .Error }}{{ .Error }}{{ else }}{{ if .Venv }}{{ .Venv }} {{ end }}{{ .Full }}{{ end }} "
},
{
"foreground": "#F3C267",
"foreground_templates": [
"{{ if or (.Working.Changed) (.Staging.Changed) }}#FF9248{{ end }}",
"{{ if and (gt .Ahead 0) (gt .Behind 0) }}#ff4500{{ end }}",
"{{ if gt .Ahead 0 }}#B388FF{{ end }}",
"{{ if gt .Behind 0 }}#B388FF{{ end }}"
],
"properties": {
"branch_max_length": 25,
"fetch_stash_count": true,
"fetch_status": true,
"fetch_upstream_icon": true
},
"style": "plain",
"template": " {{ .UpstreamIcon }}{{ .HEAD }}{{if .BranchStatus }} {{ .BranchStatus }}{{ end }}{{ if .Working.Changed }} \uf044 {{ .Working.String }}{{ end }}{{ if and (.Working.Changed) (.Staging.Changed) }} |{{ end }}{{ if .Staging.Changed }} \uf046 {{ .Staging.String }}{{ end }}{{ if gt .StashCount 0 }} \ueb4b {{ .StashCount }}{{ end }} ",
"type": "git"
}
],
"type": "prompt"
},
{
"alignment": "right",
"segments": [
{
"type": "status",
"style": "plain",
"foreground": "#b8ff75",
"foreground_templates": [
"{{ if gt .Code 0 }}#E06C75{{ end }}"
],
"template": " x{{ reason .Code }}"
},
{
"foreground": "#b8ff75",
"foreground_templates": [
"{{ if gt .Code 0 }}#E06C75{{ end }}"
],
"properties": {
"style": "roundrock",
"always_enabled": true
},
"style": "diamond",
"template": " {{ .FormattedMs }} ",
"type": "executiontime"
}
],
"type": "prompt"
},
{
"alignment": "left",
"newline": true,
"segments": [
{
"foreground": "#61AFEF",
"properties": {
"style": "full"
},
"style": "plain",
"template": "\ue5ff {{ .Path }}",
"type": "path"
}
],
"type": "prompt"
},
{
"alignment": "left",
"newline": true,
"segments": [
{
"foreground": "#E06C75",
"style": "plain",
"template": "\u0021",
"type": "root"
},
{
"foreground": "#E06C75",
"style": "plain",
"template": "\u276f",
"type": "text"
}
],
"type": "prompt"
}
],
"final_space": true,
"version": 2
}