1. 程式人生 > >Powershell 主題美化學習過程

Powershell 主題美化學習過程

前前言

如果只需要美化,但對其中原理以及學習過程不是跟感興趣,而且網路環境很好(可以科學上網)的同學可以直接繞道知乎:超酷的 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 "`[email protected]`$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

參考連結(不分先後,如果缺了麻煩告訴我):

  • https://zhuanlan.zhihu.com/p/51901035
  • https://github.com/chocolatey/choco/wiki/Installation
  • https://github.com/microexs/PowerAdmin/blob/master/PCInit.md
  • https://docs.microsoft.com/en-us/powershell/module/powershellget/install-module?view=powershell-6
  • https://chocolatey.org/install