自定義powershell主題和字體

主要仍是爲了方便以及好看吧,也借鑑了一些網上的資源,而後本身折騰了一段時間搞這個powershell的配色主題,提升工做效率
下面是記錄方法,也歡迎提問討論;
配置步驟:git

1)首先安裝Chocolatey,這個很重要,若是裏以爲能夠本身去git或別途徑安裝軟件或字體,請跳過這一條,安裝scoop也能夠;
安裝可去官網下載,也能夠代碼聯網安裝,打開powershell,推薦使用管理員打開;
而後輸入如下代碼安裝:github

Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-ObjectSystem.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

而後引入安裝的包並添加path使命令行可調用shell

@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"

若有不懂自行百度,此處很少講;函數

2)安裝好以後
choco -v可顯示最新的版本號oop

這裏我就不添加圖片了
這裏列出幾個經常使用的choco命令post

choco list -lo 列出全部的安裝包
choco list --local-only 列出choco已安裝的包字體

choco search software 查找包
choco upgrade software 升級包
choco uninstall software 卸載包ui

別的可以使用 choco --help 查看須要使用的命令spa

圖片描述

這裏咱們經過安裝 colortool 來改變默認的配色命令行

choco install colortool

命令爲
colortool -s 查看默認配色主題

colortool 主題名稱 能夠更變主題
圖片描述

後面其實設置好以後右鍵設置而後直接保存就好

3)接下來是字體的安裝

字體能夠去github上下載

這裏我推薦
https://github.com/powerline/...
的powerline的字體

字體安裝好以後可能不能在屬性後直接設置,須要命令行,,這個下面會一塊兒說

4)下載完字體後,接下來我這邊是用的oh-my-posh模塊

Install-Module posh-git
Install-Module oh-my-posh

安裝好以後

5)引入模塊並加載

Import-Module posh-git
Import-Module oh-my-posh

這個時候能夠設置
set-theme 改變主題了

默認有好幾個主題,
圖片描述
搭配上面提到的colortool能夠搭配出很好看的主題

若是發現亂碼等,,請再檢查你的字體是否設置

若是發現沒有安裝的字體

接下來貼代碼

這個是在c:/program files/powershell/Microsoft.PowerShell_profile.ps1

Install-Module -Name PSReadLine -Force -SkipPublisherCheck
if (!(Test-Path -Path $PROFILE )) { New-Item -Type File -Path $PROFILE -Force }
@"
#requires -Version 2 -Modules posh-git
 
function Write-Theme {
    param(
        [bool]
        `$lastCommandFailed,
        [string]
        `$with
    )
 
    `$lastColor = `$sl.Colors.PromptBackgroundColor
    `$prompt = Write-Prompt -Object `$sl.PromptSymbols.StartSymbol -ForegroundColor `$sl.Colors.PromptForegroundColor -BackgroundColor `$sl.Colors.SessionInfoBackgroundColor
 
    #check the last command state and indicate if failed
    If (`$lastCommandFailed) {
        `$prompt += Write-Prompt -Object "`$(`$sl.PromptSymbols.FailedCommandSymbol) " -ForegroundColor `$sl.Colors.CommandFailedIconForegroundColor -BackgroundColor `$sl.Colors.SessionInfoBackgroundColor
    }
 
    #check for elevated prompt
    If (Test-Administrator) {
        `$prompt += Write-Prompt -Object "`$(`$sl.PromptSymbols.ElevatedSymbol) " -ForegroundColor `$sl.Colors.AdminIconForegroundColor -BackgroundColor `$sl.Colors.SessionInfoBackgroundColor
    }
 
    `$user = [System.Environment]::UserName
    `$computer = [System.Environment]::MachineName
    `$path = Get-FullPath -dir `$pwd
    if (Test-NotDefaultUser(`$user)) {
        `$prompt += Write-Prompt -Object "`$user@`$computer " -ForegroundColor `$sl.Colors.SessionInfoForegroundColor -BackgroundColor `$sl.Colors.SessionInfoBackgroundColor
    }
 
    if (Test-VirtualEnv) {
        `$prompt += Write-Prompt -Object "`$(`$sl.PromptSymbols.SegmentForwardSymbol) " -ForegroundColor `$sl.Colors.SessionInfoBackgroundColor -BackgroundColor `$sl.Colors.VirtualEnvBackgroundColor
        `$prompt += Write-Prompt -Object "`$(`$sl.PromptSymbols.VirtualEnvSymbol) `$(Get-VirtualEnvName) " -ForegroundColor `$sl.Colors.VirtualEnvForegroundColor -BackgroundColor `$sl.Colors.VirtualEnvBackgroundColor
        `$prompt += Write-Prompt -Object "`$(`$sl.PromptSymbols.SegmentForwardSymbol) " -ForegroundColor `$sl.Colors.VirtualEnvBackgroundColor -BackgroundColor `$sl.Colors.PromptBackgroundColor
    }
    else {
        `$prompt += Write-Prompt -Object "`$(`$sl.PromptSymbols.SegmentForwardSymbol) " -ForegroundColor `$sl.Colors.SessionInfoBackgroundColor -BackgroundColor `$sl.Colors.PromptBackgroundColor
    }
 
    # Writes the drive portion
    `$prompt += Write-Prompt -Object "`$path " -ForegroundColor `$sl.Colors.PromptForegroundColor -BackgroundColor `$sl.Colors.PromptBackgroundColor
 
    `$status = Get-VCSStatus
    if (`$status) {
        `$themeInfo = Get-VcsInfo -status (`$status)
        `$lastColor = `$themeInfo.BackgroundColor
        `$prompt += Write-Prompt -Object `$(`$sl.PromptSymbols.SegmentForwardSymbol) -ForegroundColor `$sl.Colors.PromptBackgroundColor -BackgroundColor `$lastColor
        `$prompt += Write-Prompt -Object " `$(`$themeInfo.VcInfo) " -BackgroundColor `$lastColor -ForegroundColor `$sl.Colors.GitForegroundColor
    }
 
    # Writes the postfix to the prompt
    `$prompt += Write-Prompt -Object `$sl.PromptSymbols.SegmentForwardSymbol -ForegroundColor `$lastColor
 
    `$timeStamp = Get-Date -UFormat %r
    `$timestamp = "[`$timeStamp]"
 
    `$prompt += Set-CursorForRightBlockWrite -textLength (`$timestamp.Length + 1)
    `$prompt += Write-Prompt `$timeStamp -ForegroundColor `$sl.Colors.PromptForegroundColor
 
    `$prompt += Set-Newline
 
    if (`$with) {
        `$prompt += Write-Prompt -Object "`$(`$with.ToUpper()) " -BackgroundColor `$sl.Colors.WithBackgroundColor -ForegroundColor `$sl.Colors.WithForegroundColor
    }
    `$prompt += Write-Prompt -Object (`$sl.PromptSymbols.PromptIndicator) -ForegroundColor `$sl.Colors.PromptBackgroundColor
    `$prompt += ' '
    `$prompt
}
 
`$sl = `$global:ThemeSettings #local settings
`$sl.PromptSymbols.StartSymbol = ''
`$sl.PromptSymbols.PromptIndicator = [char]::ConvertFromUtf32(0x276F)
`$sl.PromptSymbols.SegmentForwardSymbol = [char]::ConvertFromUtf32(0xE0B0)
`$sl.Colors.PromptForegroundColor = [ConsoleColor]::White
`$sl.Colors.PromptSymbolColor = [ConsoleColor]::White
`$sl.Colors.PromptHighlightColor = [ConsoleColor]::DarkBlue
`$sl.Colors.GitForegroundColor = [ConsoleColor]::Black
`$sl.Colors.WithForegroundColor = [ConsoleColor]::DarkRed
`$sl.Colors.WithBackgroundColor = [ConsoleColor]::Magenta
`$sl.Colors.VirtualEnvBackgroundColor = [System.ConsoleColor]::Red
`$sl.Colors.VirtualEnvForegroundColor = [System.ConsoleColor]::White
"@>$env:userprofile"\Documents\WindowsPowerShell\Modules\oh-my-posh\2.0.263\Themes\Paradox.psm1"       # 建議本身把代碼放到記事本而後替換路徑,此條路徑要改。。
@"


chcp 88888  ##這一段能夠省略,若是下面是手動建立的,建議下者
Set-PSReadlineOption -EditMode Emacs
function which(`$name) { Get-Command `$name | Select-Object Definition }
function rmrf(`$item) { Remove-Item `$item -Recurse -Force }
function mkfile(`$file) { "" | Out-File `$file -Encoding ASCII }
Import-Module posh-git
Import-Module oh-my-posh
Set-Theme Paradox
"@ > $PROFILE


chcp 65001   ##這個是c:/user/document/powershell/Microsoft.PowerShell_profile.ps1
Set-PSReadlineOption -EditMode Emacs
function which($name) { Get-Command $name | Select-Object Definition }
function rmrf($item) { Remove-Item $item -Recurse -Force }
function mkfile($file) { "" | Out-File $file -Encoding ASCII }
Import-Module posh-git
Import-Module oh-my-posh
Set-Theme Paradox

總體是把函數寫進powershell的編輯裏引入系統的字體
而後主題更新
最後就能夠設置剛剛安裝好的字體了

圖片描述

ps:默認的主題能夠經過Set-Theme 主題來更改

相關文章
相關標籤/搜索