Powershell 主題美化學習過程


前前言

如果只需要美化,但對其中原理以及學習過程不是跟感興趣,而且網絡環境很好(可以科學上網)的同學可以直接繞道知乎:超酷的 PowerShell 美化指南,本文也根據該分享折騰一番后的心得以及補充。
被吐槽標題有濃濃的90年代電子風了哎,而且第一張圖太惡心。貼上一張PD虛擬機里的Powershell吧。但標題真不知道怎么改,東西很雜,只能說是圍繞這個主題了。

前言

相信不少擼友都有過下面這樣一個不堪入目很不友好的界面,我們本着對美好食物的向往以及對更好生活的憧憬,勢必要對其進行一番改造,所以有了本文的學習過程。

無法直視

那么我們來搞事情

0. 准備工作

其實對於下面1/2/3三個步驟,很多擼友基本上是不需要的,只要檢查一下即可。
特別是步驟2的Chocolatey,它不是必備的,Powershell自帶Install-Module也是可以是,這里只是順帶提個知識點。

1. Powershell腳本簽名

參考鏈接:https://www.pstips.net/powershell-scripts-signature.html

配置windows允許執行沒有簽名(遠程)的Powershell腳本

BTW,這一步是為后面的執行遠程powershell腳本安裝Chocolatey做准備,如果下載這個腳本下來,在腳本的最后面是有提供簽名的其實。

2. Windows的包管理工具Chocolatey

參考鏈接:https://chocolatey.org/
當然的了,並不是要什么node.js開發,或者是前端開發者,又或者VS開發才要用到包管理工具,windows下(powershell開發/cmd開發)也可以一樣有。
用管理員模式啟動Powershell,然后執行以下命令。(官網上也有非管理員模式的安裝方法,想了解更多的可以查一下)

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

執行完后出現下面這個一般就是表示安裝成功,然后最好重啟下powershell,畢竟人家提示了嘛。

重啟好了后驗證是否安裝成功,敲入choco -v,出現下面的表示成功了(抱歉,這屏幕截圖有點怪異,忍着看吧)

3. git

這個東西現在已經是必不可少的工具了,如果你還沒用到,請反思為何。

choco install git

git安裝就不詳細說明了。

3. 字體庫

推薦字體庫:https://github.com/powerline/fonts
當然,用nerd-fontsy也未必不可,但這里不展開了,太多東西了。
字體庫只是為了在目錄下多一些舒服點的“圖標”而已(其實是字體代替)。
試試跑一下下面的命令

Write-Host "$([char] 0xE0A0; [char] 0xE0A1; [char] 0xE0A2; [char] 0xE0A3; [char] 0xE0B0; [char] 0xE0B1; [char] 0xE0B2; [char] 0xE0B3; [char] 0xE0B8; [char] 0xE0B9; [char] 0xE0BA; [char] 0xE0BB; [char] 0xE0BC; [char] 0xE0BD; [char] 0xE0BE; [char] 0xE0BF;)" 

如果出現類似這個,或者干脆方格問號甚至空字符的,那么基本上就需要安裝字體支持。

powerline/fonts提供了不少字體,我這里只試了一個,也就是推薦的“大丈夫老師摸摸”“逮蝦戶”字體,好吧,名字你們說怎么叫就怎么叫。
注意:並不需要下載整個字體庫,指需要下載需要用到的DejaVuSansMono就可以了。參考的分享里是把整個字體庫都下了下來,但我們並不需要怎么多,反正豐儉由人嘛。
下好后全選ttf文件,右鍵安裝便是。

我選的是安裝到所有賬戶,雖然也只有我一個人在用,反正。。。
好啦,就不說你們下下來為何30幾k50幾k而且安裝失敗了,冇眼睇~(下面幾個鏈接混了,沒排對,反正都下載都裝了就對了,給你鏈接已經很不錯了)

裝好了別急,還改不了字體。不信你試試。

4. powershell的git集成方案 posh-git

資源鏈接:https://github.com/dahlbyk/posh-git
本來想用chcoc裝的,不知道為何找不到包,那用回Install-Module

Install-Module posh-git


這里提示我的NuGet版本不夠新,順便更新下吧。能Yes to all就all啦

5. 基於posh-git的美化主題oh-my-posh

資源鏈接:https://github.com/JanDeDobbeleer/oh-my-posh
同上繼續安裝。

Install-Module oh-my-posh

6. 硬貨在這里

剛剛前面做的全都是事前准備而已,這里才是真正影響Powershell主題的主要關鍵。直接復制到Powershell跑一下吧。

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.230\Themes\Paradox.psm1"
@"
chcp 65001
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
Set-PSReadlineOption -EditMode Emacs
Import-Module posh-git
Import-Module oh-my-posh
Set-Theme Paradox

跑完后會發現,終於可以設置字體了。

設置好后再跑一下前面檢查的的那一段,會有下面這效果,那么就是ok的了。這幾個字符能檢查安裝的字體符合不符合要求。

最終結果

當然,顏色那里還要痛苦一番,網上找點舒服點的配色吧,顏色設置這個地方,越熟悉Photoshop的人會越痛苦,要是你沒碰過ps,那么恭喜你,你嘗不到這個痛苦,哈哈哈,慢慢品嘗吧。

對了,最后面Set-Theme Paradox這里的主題詳見主題頁面,兩個地方記得都要改,每次打開他會跑一次這個的。

總結

馬桶的工作量嚴重不飽和!
嗯,我承認,不然也不會瞎搞這些。
其實說實話,上面這些東西早在十年前,就碰到過,但那個時候連什么叫“包管理工具”是什么都不懂,就算有個人告訴我:“這就是用來管理包的呀”,“哦好的”,然后就沒然后了。
關於學習,這個東西還是要多想一步,多求甚解,對自己有好處的。

顏色設置跟字體設置我為何要用界面?(都是UI給害的!)

就是為了吐槽,如果Windows沒有這些設置界面,那將會是非常專業的軟件!233

參考鏈接(不分先后,如果缺了麻煩告訴我):


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM