370 lines
9.3 KiB
Plaintext
370 lines
9.3 KiB
Plaintext
---
|
||
title: 主題
|
||
description: 選擇內置主題或定義您自己的主題。
|
||
---
|
||
|
||
使用 opencode,您可以從多個內置主題之一中進行選擇,使用適合您的terminal主題的主題,或者定義您自己的自定義主題。
|
||
|
||
默認情況下,opencode 使用我們自己的 `opencode` 主題。
|
||
|
||
---
|
||
|
||
## terminal要求
|
||
|
||
為了使主題能夠正確顯示完整的調色板,您的terminal必須支持**真彩色**(24 位顏色)。大多數現代terminal默認支持此功能,但您可能需要啟用它:
|
||
|
||
- **檢查支持**:運行`echo $COLORTERM` - 它應該輸出`truecolor`或`24bit`
|
||
- **啟用真彩色**:在 shell 配置文件中設置環境變量 `COLORTERM=truecolor`
|
||
- **terminal兼容性**:確保您的terminal模擬器支持 24 位顏色(大多數現代terminal,如 iTerm2、Alacritty、Kitty、Windows terminal和最新版本的 GNOME terminal都支持)
|
||
|
||
如果沒有真彩色支持,主題的顏色精度可能會降低或回落到最接近的 256 色近似值。
|
||
|
||
---
|
||
|
||
## 內置主題
|
||
|
||
opencode 附帶了幾個內置主題。
|
||
|
||
| 名稱 | 描述 |
|
||
| ---------------------- | ----------------------------------------------------------------- |
|
||
| `system` | 適應您terminal的背景顏色 |
|
||
| `tokyonight` | 基於[tokyonight](https://github.com/folke/tokyonight.nvim)主題 |
|
||
| `everforest` | 基於[everforest](https://github.com/sainnhe/everforest)主題 |
|
||
| `ayu` | 基於[ayu](https://github.com/ayu-theme)深色主題 |
|
||
| `catppuccin` | 基於[catppuccin](https://github.com/catppuccin)主題 |
|
||
| `catppuccin-macchiato` | 基於[catppuccin](https://github.com/catppuccin)主題 |
|
||
| `gruvbox` | 基於[gruvbox](https://github.com/morhetz/gruvbox)主題 |
|
||
| `kanagawa` | 基於[kanagawa](https://github.com/rebelot/kanagawa.nvim)主題 |
|
||
| `nord` | 基於[Nord](https://github.com/nordtheme/nord)主題 |
|
||
| `matrix` | 黑客風格黑底綠主題 |
|
||
| `one-dark` | 基於[One Dark](https://github.com/Th3Whit3Wolf/one-nvim) 深色主題 |
|
||
|
||
此外,我們還在不斷添加新主題。
|
||
|
||
---
|
||
|
||
## 系統主題
|
||
|
||
`system` 主題旨在自動適應您terminal的配色方案。與使用固定顏色的傳統主題不同,_system_ 主題:
|
||
|
||
- **生成灰度**:根據terminal的背景顏色創建自定義灰度,確保最佳對比度。
|
||
- **使用 ANSI 顏色**:利用標準 ANSI 顏色 (0-15) 進行語法突出顯示和 UI 元素,尊重terminal的調色板。
|
||
- **保留terminal默認設置**:使用 `none` 作為文本和背景顏色,以保持terminal的本機外觀。
|
||
|
||
系統主題適合以下用戶:
|
||
|
||
- 希望 opencode 與其terminal的外觀相匹配
|
||
- 使用自定義terminal配色方案
|
||
- 希望所有terminal應用程式具有一致的外觀
|
||
|
||
---
|
||
|
||
## 使用主題
|
||
|
||
您可以通過使用 `/theme` 命令調出主題選擇來選擇主題。或者您可以在[配置](/docs/config) 中指定它。
|
||
|
||
```json title="opencode.json" {3}
|
||
{
|
||
"$schema": "https://opencode.ai/config.json",
|
||
"theme": "tokyonight"
|
||
}
|
||
```
|
||
|
||
---
|
||
|
||
## 自定義主題
|
||
|
||
opencode 支持靈活的基於 JSON 的主題系統,允許用戶輕鬆創建和自定義主題。
|
||
|
||
---
|
||
|
||
### 等級制度
|
||
|
||
主題按以下順序從多個目錄加載,其中後面的目錄覆蓋前面的目錄:
|
||
|
||
1. **內置主題** - 這些主題嵌入在二進製文件中
|
||
2. **用戶配置目錄** - 在`~/.config/opencode/themes/*.json` 或`$XDG_CONFIG_HOME/opencode/themes/*.json` 中定義
|
||
3. **項目根目錄** - 定義在`<project-root>/.opencode/themes/*.json`
|
||
4. **當前工作目錄** - 在`./.opencode/themes/*.json`中定義
|
||
|
||
如果多個目錄包含同名主題,則將使用優先級較高的目錄中的主題。
|
||
|
||
---
|
||
|
||
### 創建主題
|
||
|
||
要創建自定義主題,請在主題目錄之一中創建一個 JSON 文件。
|
||
|
||
對於用戶範圍的主題:
|
||
|
||
```bash no-frame
|
||
mkdir -p ~/.config/opencode/themes
|
||
vim ~/.config/opencode/themes/my-theme.json
|
||
```
|
||
|
||
以及針對特定項目的主題。
|
||
|
||
```bash no-frame
|
||
mkdir -p .opencode/themes
|
||
vim .opencode/themes/my-theme.json
|
||
```
|
||
|
||
---
|
||
|
||
### JSON格式
|
||
|
||
主題使用靈活的 JSON 格式,支持:
|
||
|
||
- **十六進制顏色**:`"#ffffff"`
|
||
- **ANSI 顏色**:`3` (0-255)
|
||
- **顏色參考**:`"primary"` 或自定義定義
|
||
- **深色/淺色版本**:`{"dark": "#000", "light": "#fff"}`
|
||
- **無顏色**:`"none"` - 使用terminal的默認顏色或透明
|
||
|
||
---
|
||
|
||
### 顏色定義
|
||
|
||
`defs` 部分是可選的,它允許您定義可在主題中引用的可重用顏色。
|
||
|
||
---
|
||
|
||
### terminal默認值
|
||
|
||
特殊值`"none"`可用於任何顏色以繼承terminal的默認顏色。這對於創建與terminal配色方案無縫融合的主題特別有用:
|
||
|
||
- `"text": "none"` - 使用terminal的默認前景色
|
||
- `"background": "none"` - 使用terminal的默認背景顏色
|
||
|
||
---
|
||
|
||
### 例子
|
||
|
||
以下是自定義主題的示例:
|
||
|
||
```json title="my-theme.json"
|
||
{
|
||
"$schema": "https://opencode.ai/theme.json",
|
||
"defs": {
|
||
"nord0": "#2E3440",
|
||
"nord1": "#3B4252",
|
||
"nord2": "#434C5E",
|
||
"nord3": "#4C566A",
|
||
"nord4": "#D8DEE9",
|
||
"nord5": "#E5E9F0",
|
||
"nord6": "#ECEFF4",
|
||
"nord7": "#8FBCBB",
|
||
"nord8": "#88C0D0",
|
||
"nord9": "#81A1C1",
|
||
"nord10": "#5E81AC",
|
||
"nord11": "#BF616A",
|
||
"nord12": "#D08770",
|
||
"nord13": "#EBCB8B",
|
||
"nord14": "#A3BE8C",
|
||
"nord15": "#B48EAD"
|
||
},
|
||
"theme": {
|
||
"primary": {
|
||
"dark": "nord8",
|
||
"light": "nord10"
|
||
},
|
||
"secondary": {
|
||
"dark": "nord9",
|
||
"light": "nord9"
|
||
},
|
||
"accent": {
|
||
"dark": "nord7",
|
||
"light": "nord7"
|
||
},
|
||
"error": {
|
||
"dark": "nord11",
|
||
"light": "nord11"
|
||
},
|
||
"warning": {
|
||
"dark": "nord12",
|
||
"light": "nord12"
|
||
},
|
||
"success": {
|
||
"dark": "nord14",
|
||
"light": "nord14"
|
||
},
|
||
"info": {
|
||
"dark": "nord8",
|
||
"light": "nord10"
|
||
},
|
||
"text": {
|
||
"dark": "nord4",
|
||
"light": "nord0"
|
||
},
|
||
"textMuted": {
|
||
"dark": "nord3",
|
||
"light": "nord1"
|
||
},
|
||
"background": {
|
||
"dark": "nord0",
|
||
"light": "nord6"
|
||
},
|
||
"backgroundPanel": {
|
||
"dark": "nord1",
|
||
"light": "nord5"
|
||
},
|
||
"backgroundElement": {
|
||
"dark": "nord1",
|
||
"light": "nord4"
|
||
},
|
||
"border": {
|
||
"dark": "nord2",
|
||
"light": "nord3"
|
||
},
|
||
"borderActive": {
|
||
"dark": "nord3",
|
||
"light": "nord2"
|
||
},
|
||
"borderSubtle": {
|
||
"dark": "nord2",
|
||
"light": "nord3"
|
||
},
|
||
"diffAdded": {
|
||
"dark": "nord14",
|
||
"light": "nord14"
|
||
},
|
||
"diffRemoved": {
|
||
"dark": "nord11",
|
||
"light": "nord11"
|
||
},
|
||
"diffContext": {
|
||
"dark": "nord3",
|
||
"light": "nord3"
|
||
},
|
||
"diffHunkHeader": {
|
||
"dark": "nord3",
|
||
"light": "nord3"
|
||
},
|
||
"diffHighlightAdded": {
|
||
"dark": "nord14",
|
||
"light": "nord14"
|
||
},
|
||
"diffHighlightRemoved": {
|
||
"dark": "nord11",
|
||
"light": "nord11"
|
||
},
|
||
"diffAddedBg": {
|
||
"dark": "#3B4252",
|
||
"light": "#E5E9F0"
|
||
},
|
||
"diffRemovedBg": {
|
||
"dark": "#3B4252",
|
||
"light": "#E5E9F0"
|
||
},
|
||
"diffContextBg": {
|
||
"dark": "nord1",
|
||
"light": "nord5"
|
||
},
|
||
"diffLineNumber": {
|
||
"dark": "nord2",
|
||
"light": "nord4"
|
||
},
|
||
"diffAddedLineNumberBg": {
|
||
"dark": "#3B4252",
|
||
"light": "#E5E9F0"
|
||
},
|
||
"diffRemovedLineNumberBg": {
|
||
"dark": "#3B4252",
|
||
"light": "#E5E9F0"
|
||
},
|
||
"markdownText": {
|
||
"dark": "nord4",
|
||
"light": "nord0"
|
||
},
|
||
"markdownHeading": {
|
||
"dark": "nord8",
|
||
"light": "nord10"
|
||
},
|
||
"markdownLink": {
|
||
"dark": "nord9",
|
||
"light": "nord9"
|
||
},
|
||
"markdownLinkText": {
|
||
"dark": "nord7",
|
||
"light": "nord7"
|
||
},
|
||
"markdownCode": {
|
||
"dark": "nord14",
|
||
"light": "nord14"
|
||
},
|
||
"markdownBlockQuote": {
|
||
"dark": "nord3",
|
||
"light": "nord3"
|
||
},
|
||
"markdownEmph": {
|
||
"dark": "nord12",
|
||
"light": "nord12"
|
||
},
|
||
"markdownStrong": {
|
||
"dark": "nord13",
|
||
"light": "nord13"
|
||
},
|
||
"markdownHorizontalRule": {
|
||
"dark": "nord3",
|
||
"light": "nord3"
|
||
},
|
||
"markdownListItem": {
|
||
"dark": "nord8",
|
||
"light": "nord10"
|
||
},
|
||
"markdownListEnumeration": {
|
||
"dark": "nord7",
|
||
"light": "nord7"
|
||
},
|
||
"markdownImage": {
|
||
"dark": "nord9",
|
||
"light": "nord9"
|
||
},
|
||
"markdownImageText": {
|
||
"dark": "nord7",
|
||
"light": "nord7"
|
||
},
|
||
"markdownCodeBlock": {
|
||
"dark": "nord4",
|
||
"light": "nord0"
|
||
},
|
||
"syntaxComment": {
|
||
"dark": "nord3",
|
||
"light": "nord3"
|
||
},
|
||
"syntaxKeyword": {
|
||
"dark": "nord9",
|
||
"light": "nord9"
|
||
},
|
||
"syntaxFunction": {
|
||
"dark": "nord8",
|
||
"light": "nord8"
|
||
},
|
||
"syntaxVariable": {
|
||
"dark": "nord7",
|
||
"light": "nord7"
|
||
},
|
||
"syntaxString": {
|
||
"dark": "nord14",
|
||
"light": "nord14"
|
||
},
|
||
"syntaxNumber": {
|
||
"dark": "nord15",
|
||
"light": "nord15"
|
||
},
|
||
"syntaxType": {
|
||
"dark": "nord7",
|
||
"light": "nord7"
|
||
},
|
||
"syntaxOperator": {
|
||
"dark": "nord9",
|
||
"light": "nord9"
|
||
},
|
||
"syntaxPunctuation": {
|
||
"dark": "nord4",
|
||
"light": "nord0"
|
||
}
|
||
}
|
||
}
|
||
```
|