Terminal
16 May 2024
4 min read
tags: MacOSWarpStarshipterminalproductivity

客製化 command line - Warp + Starship

此篇文章是參考 How to Configure Starship to Look Exactly Like P10K 來設定樣式,因原文章有段時間了,所以這次跑安裝流程也有踩到一些坑,順手紀錄一下如何使用 Starship 來客製化 Warp 的 command line。

warp

warp

上面兩張圖分別是 Before(預設) 和 After(Starship)~

安裝 Warp

$brew install --cask warp

設定字體

為了順利顯示各種 icons,字體需要更改成 Nerd Font:

Settings -> Appearance -> Text -> Terminal font 改成 MesloGS NF

筆者是使用 MesloGS Nerd Font,可自行選擇下載的版本。

設定字體

💡 Nerd Font 相關下載點:

安裝 Starship

筆者環境是使用 Homebrew (套件管理器)+ Zsh (Shell),如果是使用其他的,可以參考 Starship 的安裝設定。

1. 安裝

$brew install starship

2. 初始化腳本 (script)

$vi ~/.zshrc

先輸入 i 進入 insert 模式,將以下內容放在最底部:

eval "$(starship init zsh)"

esc 結束編輯,輸入 wq,存檔離開。

⚠️ 注意

如果之前電腦設定過 iTerms + powerlevel10k,這邊會踩到一個坑,zsh 會一直套用 powerlevel10k,沒辦法看到 Starship prompt。

解決辦法是針對 Warp Terminal,去執行對應的動作

$vi ~/.zshrc

將原本這段

ZSH_THEME="powerlevel10k/powerlevel10k"

....

eval "$(starship init zsh)"

改成

if [[ $TERM_PROGRAM == "WarpTerminal" ]]; then
  ZSH_THEME="robbyrussell"
else
  ZSH_THEME="powerlevel10k/powerlevel10k"
fi

...

if [[ $TERM_PROGRAM == "WarpTerminal" ]]; then
 eval "$(starship init zsh)"
fi

存檔後,再重新執行 zsh 設定檔即可

source ~/.zshrc

3. 更改 Prompt 選項

Settings -> Appearance -> 點擊 Prompt -> 選擇 Shell prompt(PS1)

Edit prompt

設定 Starship prompt config

1. 新增 starship.toml

$mkdir -p ~/.config && touch ~/.config/starship.toml
$vi ~/.config/starship.toml

先輸入 i 進入 insert 模式,將以下內容寫入後按 esc 結束編輯,輸入 wq,存檔離開。

這邊是 starship 相關設定,更多可以參考官方的 Document

format = """\
[](bg:#030B16 fg:#7DF9AA)\
[ ](bg:#7DF9AA fg:#090c0c)\
[](fg:#7DF9AA bg:#1C3A5E)\
$time\
[](fg:#1C3A5E bg:#6699cc)\
$directory\
[](fg:#6699cc bg:#ffcc66)\
$git_branch\
$git_status\
$git_metrics\
[](fg:#ffcc66 bg:#030B16)\
$character\
"""

[directory]
format = "[  $path ]($style)"
style = "fg:#000000 bg:#6699cc"

[git_branch]
format = '[ $symbol$branch(:$remote_branch) ]($style)'
symbol = "  "
style = "fg:#000000 bg:#ffcc66"

[git_status]
ahead = "⇡${count} "
diverged = "⇡${ahead_count}⇣${behind_count} "
behind = "⇣${count} "
modified = "!${count} "
untracked = "?${count} "
staged = "+${count} "
format = '[$all_status]($style)'
style = "fg:#000000 bg:#ffcc66"

[hg_branch]
format = "[ $symbol$branch ]($style)"
symbol = " "

[cmd_duration]
format = "[ ◷ $duration ]($style)"
style = "fg:bright-white bg:18"

[character]
success_symbol = '[ ➜](bold green) '
error_symbol = '[ ✗](#E84D44) '

[time]
disabled = false
time_format = "%R" # Hour:Minute Format
style = "bg:#1d2230"
format = '[[ 󱑍 $time ](bg:#1C3A5E fg:#8DFBD2)]($style)'

接著就會看到設定好的 prompt 了 🍺 ~

image

⚠️ 注意

如果遇到色塊有跑版,可以至 Setting > Appearance 中的 Text,調整 Line Height,筆者的設定是 Font size: 14Line height: 1.2

image

設定 Warp theme

1. 新增 themes 資料夾

$mkdir -p ~/.warp/themes/ && cd ~/.warp/themes/

2. 新增 theme yaml

$mkdir -p 'coolnight' && cd coolnight
$vi coolnight.yaml

將下方內容貼上後,記得儲存後離開,這邊是參考 theRubberDuckiee 原設定,更多設定可以參考 Warp Document

accent: "#38ff9c"
background: "#010b17"
foreground: "#ebddf4"
details: "darker"
terminal_colors:
  normal:
    black: "#0b3b61"
    red: "#ff3a3a"
    green: "#52ffcf"
    yellow: "#fff383"
    blue: "#1376f8"
    magenta: "#c792ea"
    cyan: "#ff5dd4"
    white: "#15fca2"
  bright:
    black: "#62686c"
    red: "#ff54b0"
    green: "#73ffd8"
    yellow: "#fcf4ad"
    blue: "#378dfe"
    magenta: "#ae81ff"
    cyan: "#ff69d7"
    white: "#5ffbbe"

3. 選擇自定義 Theme

Settings > Appearance 中的 Themes,點選 Current theme,接著在左邊找出剛剛新增的主題,點選後就會直接套用了。

image

結語

沒有特別追求樣式的話,其實 Warp 預設已經輾壓原生的 Terminal 太多太多了~

但如果你是每次打開 Terminal,看到親手裝飾的樣式,心情都會被療癒一下,這篇可以當作是入門 Starship 的第一步~

因為筆者在 P10K 和 Starship 選擇不出來,所以公司和私人電腦的 Warp 各搭配一種 😆
有興趣 Warp 搭配 P10K,可以參考筆者另一篇文章 客製化 Terminal - Warp + Powerlevel10k

參考資料

MacOSWarpterminalproductivityStarship
Published on 16 May 2024
Updated on 16 May 2024