菜单

颜色与主题

相关源文件

此页面介绍了如何通过颜色和主题来自定义fzf的外观。它涵盖了颜色系统的架构、内置主题、配置选项,并提供了自定义fzf视觉外观的实际示例。

有关预览窗口自定义的信息,请参阅预览窗口。有关键绑定自定义的信息,请参阅键绑定自定义

颜色系统架构

fzf中的颜色系统建立在几个核心组件之上,这些组件协同工作以定义应用程序中每个UI元素的可视外观。

来源:src/tui/tui.go202-365

核心组件

  1. 颜色 - 表示为int32,具有特殊值

    • colDefault (-1):默认终端颜色
    • colBlackcolWhite (0-7):基本 ANSI 颜色
    • 值 8-15:亮色 ANSI 颜色
    • 值 16-255:扩展的 256 色调色板
    • 设置了第 24 位的值:24 位 RGB 颜色
  2. 属性 (Attr) - 文本属性,如粗体、下划线、斜体等。

  3. 颜色属性 (ColorAttr) - 结合了颜色和文本属性(属性 (Attr))。

  4. 颜色对 (ColorPair) - 结合了前景色、背景色和属性。

  5. 颜色主题 (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

LightRenderer 使用 ANSI 转义序列直接控制颜色。

  • 根据ColorPair值生成 ANSI 颜色代码。
  • 与文本内容一起输出这些代码。

来源:src/tui/light.go1084-1088

TcellRenderer

TcellRenderer 使用 tcell 库进行终端操作。

  • 将 fzf 颜色映射到 tcell 颜色。
  • 将样式应用于终端网格中的单元格。

来源:src/tui/tcell.go34-36

内置主题

fzf 附带三个内置主题。

  1. Default16 - 使用 16 种 ANSI 颜色的基本主题。

    • 用于颜色支持有限的终端。
  2. Dark256 - 针对暗背景和 256 色支持优化的主题。

    • 使用扩展调色板中更细微的颜色。
    • 256 色终端的默认主题。
  3. 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)。

来源:man/man1/fzf.1241-247

颜色名称与 UI 元素

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滚动条。

来源:man/man1/fzf.1250-288

ANSI 颜色

颜色可以指定为:

  • -1 - 默认终端颜色(在代码中表示为 colDefault)。
  • 0-15 - 16 种基本颜色(按数字或名称)。
    • black, red, green, yellow, blue, magenta, cyan, white
    • bright-black (或 gray), bright-red, bright-green 等。
  • 16-255 - ANSI 256 色。
  • #rrggbb - 24 位 RGB 颜色(真彩色)。

在代码中,颜色表示为 tui.Color 类型(一个 int32)。

  • 值 0-7:基本 ANSI 颜色(colBlackcolWhite)。
  • 值 8-15:亮色 ANSI 颜色
  • 值 16-255:扩展的 256 色调色板
  • 设置了第 24 位的值(1<<24):24 位 RGB 颜色,存储为 (1<<24) | (r<<16) | (g<<8) | b。
// 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

ANSI 属性

文本属性应用于前景色。

  • regular - 清除先前设置的属性。
  • bold
  • underline
  • reverse
  • dim
  • italic
  • strikethrough

来源:man/man1/fzf.1313-320

其他主题化选项

--style 选项

应用风格预设。

--style=PRESET

可用预设:

  • default - 标准 fzf 布局。
  • minimal - 极简风格(无边框,紧凑的用户界面)。
  • full[:BORDER_样式] - 全屏风格,可选择边框样式。

来源:man/man1/fzf.1233-235

边框自定义

--border[=STYLE]

边框样式

  • rounded - 圆角边框(默认)。
  • sharp - 尖角边框。
  • bold - 粗边框。
  • double - 双线边框。
  • block - 使用块元素边框。
  • thinblock - 使用旧版计算符号的边框。
  • horizontal - 上下方的水平线。
  • vertical - 每侧的垂直线。
  • top, bottom, left, right - Only one border side
  • none - No border

来源: man/man1/fzf.1465-494 src/tui/tui.go412-464

Border System Architecture

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

Color Inheritance and Resolution

fzf's color system uses inheritance to simplify configuration

The color resolution process follows these steps

  1. Base Theme Selection: One of the built-in themes is selected based on terminal capabilities

  2. User Theme Parsing: User-specified colors are parsed and merged into the theme

  3. Theme Initialization: The InitTheme() function merges base and user themes

  4. Palette Initialization: The initPalette() function creates global ColorPair variables

来源: src/tui/tui.go962-1097 src/options.go1183-1371

示例

Basic Dark Theme with Bold Borders

fzf --color=dark --border=bold

Light Theme with Custom Highlighting

fzf --color=light,hl:33,hl+:37,pointer:1,marker:2

Customizing Specific UI Elements

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 (Example from Manual)

# 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'

Using 24-bit True Colors

# 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'

Setting Default Colors via Environment Variable

来源: man/man1/fzf.1324-334 src/options.go218-222

ANSI Processing System

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

  1. ANSI Sequence Detection: The nextAnsiEscapeSequence() function identifies ANSI escape sequences in the input text.

  2. Color Extraction: The extractColor() function processes the text, extracting ANSI color codes and building a list of color offsets.

  3. Code Interpretation: The interpretCode() function parses ANSI codes and updates the ansiState with the appropriate colors and attributes.

  4. State Tracking: The ansiState struct maintains the current foreground color, background color, and text attributes.

This system enables fzf to

  • Preserve colored output from commands
  • Apply consistent styling to matched items
  • Handle hyperlinks and other advanced terminal features

来源: src/ansi.go22-486 src/ansi_test.go190-337

Disabling Colors

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.

来源: man/man1/fzf.1236-337 src/tui/tui.go733-940