此页面介绍了如何通过颜色和主题来自定义fzf的外观。它涵盖了颜色系统的架构、内置主题、配置选项,并提供了自定义fzf视觉外观的实际示例。
有关预览窗口自定义的信息,请参阅预览窗口。有关键绑定自定义的信息,请参阅键绑定自定义。
fzf中的颜色系统建立在几个核心组件之上,这些组件协同工作以定义应用程序中每个UI元素的可视外观。
颜色 - 表示为int32,具有特殊值
colDefault (-1):默认终端颜色colBlack 到 colWhite (0-7):基本 ANSI 颜色属性 (Attr) - 文本属性,如粗体、下划线、斜体等。
颜色属性 (ColorAttr) - 结合了颜色和文本属性(属性 (Attr))。
颜色对 (ColorPair) - 结合了前景色、背景色和属性。
颜色主题 (ColorTheme) - 定义了所有 UI 元素(如提示符、边框、选择项等)的颜色。
来源:src/tui/tui.go98-107 src/tui/tui.go202-234
fzf 支持两种不同的渲染器,每种渲染器处理颜色的方式不同。
来源:src/tui/light.go99-116 src/tui/tcell.go41-109 src/tui/tui.go1025-1077
LightRenderer 使用 ANSI 转义序列直接控制颜色。
ColorPair值生成 ANSI 颜色代码。TcellRenderer 使用 tcell 库进行终端操作。
fzf 附带三个内置主题。
Default16 - 使用 16 种 ANSI 颜色的基本主题。
Dark256 - 针对暗背景和 256 色支持优化的主题。
Light256 - 针对浅背景和 256 色支持优化的主题。
这些主题定义了数十个 UI 元素的默认颜色,然后可以由用户配置覆盖。
来源:src/tui/tui.go821-940 src/tui/tcell.go127-136
--color 选项自定义颜色的主要方式是通过--color选项。
--color=[BASE_SCHEME][,COLOR_NAME[:ANSI_COLOR][:ANSI_ATTRIBUTES]]...
dark - 深色 256 色终端的颜色方案(256 色终端的默认值)。light - 浅色 256 色终端的颜色方案。16 - 16 色终端的颜色方案(16 色终端的默认值)。bw - 无颜色(等同于--no-color)。fzf 有许多 UI 元素可以单独着色。
| 类别 | 颜色名称 | 描述 |
|---|---|---|
| 文本 | fg | 默认文本 |
list-fg | 列表区文本 | |
selected-fg | 选定行文本 | |
preview-fg | 预览窗口文本 | |
| 背景 | bg | 背景 |
list-bg | 列表区背景 | |
selected-bg | 选定行背景 | |
preview-bg | 预览窗口背景 | |
| 高亮 | hl | 高亮显示的子字符串匹配项。 |
selected-hl | 选定行中高亮显示的子字符串。 | |
| 当前行 | current-fg (或 fg+) | 当前行文本 |
current-bg (或 bg+) | 当前行背景 | |
current-hl (或 hl+) | 当前行中高亮显示的部分。 | |
| UI元素 | prompt | 命令提示符。 |
pointer | 指向当前行的指针。 | |
marker | 多选标记。 | |
border | 窗口边框。 | |
info | 信息行(匹配计数器)。 | |
spinner | 流式输入指示器。 | |
header | 标题 | |
scrollbar | 滚动条。 |
颜色可以指定为:
-1 - 默认终端颜色(在代码中表示为 colDefault)。0-15 - 16 种基本颜色(按数字或名称)。black, red, green, yellow, blue, magenta, cyan, whitebright-black (或 gray), bright-red, bright-green 等。16-255 - ANSI 256 色。#rrggbb - 24 位 RGB 颜色(真彩色)。在代码中,颜色表示为 tui.Color 类型(一个 int32)。
colBlack 到 colWhite)。// Color conversion for 24-bit RGB colors
func HexToColor(rrggbb string) Color {
r, _ := strconv.ParseInt(rrggbb[1:3], 16, 0)
g, _ := strconv.ParseInt(rrggbb[3:5], 16, 0)
b, _ := strconv.ParseInt(rrggbb[5:7], 16, 0)
return Color((1 << 24) + (r << 16) + (g << 8) + b)
}
来源:src/tui/tui.go202-270 src/options.go1183-1371
文本属性应用于前景色。
regular - 清除先前设置的属性。boldunderlinereversedimitalicstrikethrough--style 选项应用风格预设。
--style=PRESET
可用预设:
default - 标准 fzf 布局。minimal - 极简风格(无边框,紧凑的用户界面)。full[:BORDER_样式] - 全屏风格,可选择边框样式。--border[=STYLE]
边框样式
rounded - 圆角边框(默认)。sharp - 尖角边框。bold - 粗边框。double - 双线边框。block - 使用块元素边框。thinblock - 使用旧版计算符号的边框。horizontal - 上下方的水平线。vertical - 每侧的垂直线。top, bottom, left, right - Only one border sidenone - No border来源: man/man1/fzf.1465-494 src/tui/tui.go412-464
The border system includes different shapes and styles
The BorderStyle struct contains the Unicode characters used for drawing borders of different styles. The MakeBorderStyle function creates these border styles with appropriate characters.
来源: src/tui/tui.go414-464 src/tui/tui.go483-590
fzf's color system uses inheritance to simplify configuration
The color resolution process follows these steps
Base Theme Selection: One of the built-in themes is selected based on terminal capabilities
User Theme Parsing: User-specified colors are parsed and merged into the theme
Theme Initialization: The InitTheme() function merges base and user themes
Palette Initialization: The initPalette() function creates global ColorPair variables
来源: src/tui/tui.go962-1097 src/options.go1183-1371
fzf --color=dark --border=bold
fzf --color=light,hl:33,hl+:37,pointer:1,marker:2
fzf --color=fg:252,bg:234,hl:67,fg+:252,bg+:235,hl+:81,info:144,prompt:161,pointer:161,marker:118,spinner:135,header:254
# Seoul256 theme with 8-bit colors
fzf --color='bg:237,bg+:236,info:143,border:240,spinner:108' \
--color='hl:65,fg:252,header:65,fg+:252' \
--color='pointer:161,marker:168,prompt:110,hl+:108'
# Seoul256 theme with 24-bit colors
fzf --color='bg:#4B4B4B,bg+:#3F3F3F,info:#BDBB72,border:#6B6B6B,spinner:#98BC99' \
--color='hl:#719872,fg:#D9D9D9,header:#719872,fg+:#D9D9D9' \
--color='pointer:#E12672,marker:#E17899,prompt:#98BEDE,hl+:#98BC99'
来源: man/man1/fzf.1324-334 src/options.go218-222
fzf includes a sophisticated ANSI escape sequence processing system that handles color codes in input text. This system is crucial for preserving and interpreting colored text from various sources.
The ANSI processing system works as follows
ANSI Sequence Detection: The nextAnsiEscapeSequence() function identifies ANSI escape sequences in the input text.
Color Extraction: The extractColor() function processes the text, extracting ANSI color codes and building a list of color offsets.
Code Interpretation: The interpretCode() function parses ANSI codes and updates the ansiState with the appropriate colors and attributes.
State Tracking: The ansiState struct maintains the current foreground color, background color, and text attributes.
This system enables fzf to
来源: src/ansi.go22-486 src/ansi_test.go190-337
If you prefer not to use colors, you can disable them with the --no-color option
fzf --no-color
This is equivalent to setting the base scheme to bw (black and white). fzf will also respect the NO_COLOR environment variable, which is checked during theme initialization
来源: man/man1/fzf.1336-337 src/options.go651-657 src/tui/tui.go786-830
fzf's color and theming system provides extensive customization options to match your terminal theme and preferences. By understanding the color names, ANSI colors, and attributes, you can create a personalized fzf experience that's both functional and visually pleasing.