Nu 的配色和主题

Nushell 界面的许多部分都可以定制它们的颜色,所有这些都可以在config.nu配置文件中设置。如果你在配置文件中看到 hash/hashtag/pound 符号 #,这意味着它后面的文字被注释掉了。

  1. 表的边框
  2. 原始值
  3. 形状(这是命令行的语法)
  4. 提示
  5. LS_COLORS

表格边框


表的边框由config.nu中的table_mode设置来控制。下面是一个例子:

> $env.config = {
    table_mode: rounded
}

下面是目前table_mode的可能选项:

  • rounded # 当然了,这个是最好的 😃
  • basic
  • compact
  • compact_double
  • light
  • thin
  • with_love
  • reinforced
  • heavy
  • none
  • other

颜色符号


  • r - 标准颜色红色的缩写
  • rb - 标准颜色红色的缩写,带有粗体属性
  • red - 标准颜色红色
  • red_bold - 带有粗体属性的标准颜色红色
  • "#ff0000" - "#hex" 格式的颜色:前景色为红色(需要引号)
  • { fg: "#ff0000" bg: "#0000ff" attr: b } - 完整 "#hex" 格式:前景为红色,背景为蓝色的 "#hex" 格式,属性为粗体缩写。

属性


编码含义
l闪烁
b加粗
d暗淡
h隐藏
i斜体
r反转
s删除
u下划线
n
默认为无

"标准颜色"和缩写

代码名称
ggreen
gbgreen_bold
gugreen_underline
gigreen_italic
gdgreen_dimmed
grgreen_reverse
gblgreen_blink
gstgreen_strike
lglight_green
lgblight_green_bold
lgulight_green_underline
lgilight_green_italic
lgdlight_green_dimmed
lgrlight_green_reverse
lgbllight_green_blink
lgstlight_green_strike
rred
rbred_bold
rured_underline
rired_italic
rdred_dimmed
rrred_reverse
rblred_blink
rstred_strike
lrlight_red
lrblight_red_bold
lrulight_red_underline
lrilight_red_italic
lrdlight_red_dimmed
lrrlight_red_reverse
lrbllight_red_blink
lrstlight_red_strike
ublue
ubblue_bold
uublue_underline
uiblue_italic
udblue_dimmed
urblue_reverse
ublblue_blink
ustblue_strike
lulight_blue
lublight_blue_bold
luulight_blue_underline
luilight_blue_italic
ludlight_blue_dimmed
lurlight_blue_reverse
lubllight_blue_blink
lustlight_blue_strike
bblack
bbblack_bold
bublack_underline
biblack_italic
bdblack_dimmed
brblack_reverse
bblblack_blink
bstblack_strike
ligrlight_gray
ligrblight_gray_bold
ligrulight_gray_underline
ligrilight_gray_italic
ligrdlight_gray_dimmed
ligrrlight_gray_reverse
ligrbllight_gray_blink
ligrstlight_gray_strike
yyellow
ybyellow_bold
yuyellow_underline
yiyellow_italic
ydyellow_dimmed
yryellow_reverse
yblyellow_blink
ystyellow_strike
lylight_yellow
lyblight_yellow_bold
lyulight_yellow_underline
lyilight_yellow_italic
lydlight_yellow_dimmed
lyrlight_yellow_reverse
lybllight_yellow_blink
lystlight_yellow_strike
ppurple
pbpurple_bold
pupurple_underline
pipurple_italic
pdpurple_dimmed
prpurple_reverse
pblpurple_blink
pstpurple_strike
lplight_purple
lpblight_purple_bold
lpulight_purple_underline
lpilight_purple_italic
lpdlight_purple_dimmed
lprlight_purple_reverse
lpbllight_purple_blink
lpstlight_purple_strike
ccyan
cbcyan_bold
cucyan_underline
cicyan_italic
cdcyan_dimmed
crcyan_reverse
cblcyan_blink
cstcyan_strike
lclight_cyan
lcblight_cyan_bold
lculight_cyan_underline
lcilight_cyan_italic
lcdlight_cyan_dimmed
lcrlight_cyan_reverse
lcbllight_cyan_blink
lcstlight_cyan_strike
wwhite
wbwhite_bold
wuwhite_underline
wiwhite_italic
wdwhite_dimmed
wrwhite_reverse
wblwhite_blink
wstwhite_strike
dgrdark_gray
dgrbdark_gray_bold
dgrudark_gray_underline
dgridark_gray_italic
dgrddark_gray_dimmed
dgrrdark_gray_reverse
dgrbldark_gray_blink
dgrstdark_gray_strike

"#hex" 格式


"#hex" 格式是你通常看到的一种表示颜色的方式。它由简单的#字符以及后面的 6 个字符组成。前两个代表 红色,接下来两个代表 绿色,最后两个代表 蓝色。重要的是,这个字符串必须用引号包围,否则 Nushell 会认为它是一个被注释掉的字符串。

例子:红色的主要颜色是 "#ff0000" 或 "#FF0000"。字母的大写和小写没有区别。

这种 "#hex"格式允许我们为 Nushell 的不同部分指定 24 位真彩色调。

完整 "#hex" 格式


完整 "#hex"格式是对 "#hex" 格式的一种改进,但允许人们在一行中指定前景色、背景色和属性。

例如:{ fg: "#ff0000" bg: "#0000ff" attr: b }

  • 前景色红色为 "#hex" 格式
  • 背景色蓝色为 "#hex" 格式
  • 属性为加粗的缩写形式

原始值


原始值是像intstring这样的值。原始值和形状可以用上面看到的各种颜色符号来设置。

这是当前的原始值列表。并非所有这些都是可配置的。可配置的被标记为 *

原始值默认颜色可配置
any
binaryColor::White.normal()*
blockColor::White.normal()*
boolColor::White.normal()*
cellpathColor::White.normal()*
condition
custom
dateColor::White.normal()*
durationColor::White.normal()*
expression
filesizeColor::White.normal()*
floatColor::White.normal()*
glob
import
intColor::White.normal()*
listColor::White.normal()*
nothingColor::White.normal()*
number
operator
path
rangeColor::White.normal()*
recordColor::White.normal()*
signature
stringColor::White.normal()*
table
var
vardecl
variable

特殊的 "原始值"(不是真正的原始值,它们的存在仅仅是为了着色)

原始值默认颜色可配置
leading_trailing_space_bgColor::Rgb(128, 128, 128))*
headerColor::Green.bold()*
emptyColor::Blue.normal()*
row_indexColor::Green.bold()*
hintsColor::DarkGray.normal()*

下面是一个改变其中一些数值的小例子。

> let config = {
    color_config: {
        separator: purple
        leading_trailing_space_bg: "#ffffff"
        header: gb
        date: wd
        filesize: c
        row_index: cb
        bool: red
        int: green
        duration: blue_bold
        range: purple
        float: red
        string: white
        nothing: red
        binary: red
        cellpath: cyan
        hints: dark_gray
    }
}

下面是另一个使用多种颜色语法的小例子,其中有一些注释:

> let config = {
    color_config: {
        separator: "#88b719" # this sets only the foreground color like PR #486
        leading_trailing_space_bg: white # this sets only the foreground color in the original style
        header: { # this is like PR #489
            fg: "#B01455", # note, quotes are required on the values with hex colors
            bg: "#ffb900",# note, commas are not required, it could also be all on one line
            attr: bli # note, there are no quotes around this value. it works with or without quotes
        }
        date: "#75507B"
        filesize: "#729fcf"
        row_index: {
            # note that this is another way to set only the foreground, no need to specify bg and attr
            fg: "#e50914"
        }
    }
}

Shape

如上所述,"形状" 是一个用来表示语法着色的术语。

下面是当前支持的平面形状列表:

形状默认样式可配置
shape_blockfg(Color::Blue).bold()*
shape_boolfg(Color::LightCyan)*
shape_custombold()*
shape_externalfg(Color::Cyan)*
shape_externalargfg(Color::Green).bold()*
shape_filepathfg(Color::Cyan)*
shape_flagfg(Color::Blue).bold()*
shape_floatfg(Color::Purple).bold()*
shape_garbagefg(Color::White).on(Color::Red).bold()*
shape_globpatternfg(Color::Cyan).bold()*
shape_intfg(Color::Purple).bold()*
shape_internalcallfg(Color::Cyan).bold()*
shape_listfg(Color::Cyan).bold()*
shape_literalfg(Color::Blue)*
shape_nothingfg(Color::LightCyan)*
shape_operatorfg(Color::Yellow)*
shape_rangefg(Color::Yellow).bold()*
shape_recordfg(Color::Cyan).bold()*
shape_signaturefg(Color::Green).bold()*
shape_stringfg(Color::Green)*
shape_string_interpolationfg(Color::Cyan).bold()*
shape_tablefg(Color::Blue).bold()*
shape_variablefg(Color::Purple)*

这里有一个小例子,说明如何对这些项目应用颜色。任何没有显示指定的都会被设置为默认的颜色。

> $env.config = {
    color_config: {
        shape_garbage: { fg: "#FFFFFF" bg: "#FF0000" attr: b}
        shape_bool: green
        shape_int: { fg: "#0000ff" attr: b}
    }
}

"提示"的配置和着色

Nushell 的提示符可以通过这些环境变量进行配置:

  • PROMPT_COMMAND: 为设置提示而执行的代码(块)
  • PROMPT_COMMAND_RIGHT: 为设置 RIGHT 提示而执行的代码(块)(参考 nu_scripts 里的 oh-my.nu)
  • PROMPT_INDICATOR = "〉": 提示后打印的标识(默认为">"类 Unicode 符号)
  • PROMPT_INDICATOR_VI_INSERT = ": "
  • PROMPT_INDICATOR_VI_NORMAL = "v "
  • PROMPT_MULTILINE_INDICATOR = "::: "

例如:对于一个简单的提示,我们可以这样做。注意PROMPT_COMMAND需要一个block而其他的需要一个string

> $env.PROMPT_COMMAND = { build-string (date now | format date '%m/%d/%Y %I:%M:%S%.3f') ': ' (pwd | path basename) }

如果你不喜欢默认的PROMPT_INDICATOR,你可以这样改变它:

> $env.PROMPT_INDICATOR = "> "

提示的颜色由 PROMPT_COMMAND 中的 block 控制,在这里你可以编写自己的自定义提示。我们写了一个稍微花哨点的,有 git 状态的,位于 nu_scripts 仓库open in new window

ls 命令的配色:LS_COLORS

Nushell 将尊重并使用 Mac、Linux 和 Windows 上的 LS_COLORS 环境变量设置。这个设置允许你在做ls时定义文件类型的颜色。例如,你可以让目录变成一种颜色,_.md markdown 文件一种颜色,_.toml 文件变成另一种颜色,等等。有很多方法可以给你的文件类型着色。

有一个详尽的清单可以在 这里open in new window 看到,不过它可能太多了,但可以让你初步了解如何创建一个ls_colors文件,而dircolors可以把它变成LS_COLORS环境变量。

open in new window 是对LS_COLORS的一个相当好的介绍。我相信你可以在网上找到更多相关教程。

我喜欢vivid应用程序,目前在我的config.nu中是这样配置的。你可以在 这里open in new window 找到vivid

$env.LS_COLORS = (vivid generate molokai | str trim)

如果没有设置 LS_COLORS,Nushell 将默认使用内置的 LS_COLORS 设置,基于 8 位(扩展)ANSI 颜色。

主题

主题设计结合了上述所有的着色。这里有一个快速的例子,我们把它放在一起,以证明主题定制的能力。这是对我们在网络上看到的 base16 主题的一种转换。

使主题生效的关键是确保你在声明 let config =之前,在config.nu文件中指定你要使用的所有主题和颜色:

# let's define some colors

let base00 = "#181818" # Default Background
let base01 = "#282828" # Lighter Background (Used for status bars, line number and folding marks)
let base02 = "#383838" # Selection Background
let base03 = "#585858" # Comments, Invisibles, Line Highlighting
let base04 = "#b8b8b8" # Dark Foreground (Used for status bars)
let base05 = "#d8d8d8" # Default Foreground, Caret, Delimiters, Operators
let base06 = "#e8e8e8" # Light Foreground (Not often used)
let base07 = "#f8f8f8" # Light Background (Not often used)
let base08 = "#ab4642" # Variables, XML Tags, Markup Link Text, Markup Lists, Diff Deleted
let base09 = "#dc9656" # Integers, Boolean, Constants, XML Attributes, Markup Link Url
let base0a = "#f7ca88" # Classes, Markup Bold, Search Text Background
let base0b = "#a1b56c" # Strings, Inherited Class, Markup Code, Diff Inserted
let base0c = "#86c1b9" # Support, Regular Expressions, Escape Characters, Markup Quotes
let base0d = "#7cafc2" # Functions, Methods, Attribute IDs, Headings
let base0e = "#ba8baf" # Keywords, Storage, Selector, Markup Italic, Diff Changed
let base0f = "#a16946" # Deprecated, Opening/Closing Embedded Language Tags, e.g. <?php ?>

# we're creating a theme here that uses the colors we defined above.

let base16_theme = {
    separator: $base03
    leading_trailing_space_bg: $base04
    header: $base0b
    date: $base0e
    filesize: $base0d
    row_index: $base0c
    bool: $base08
    int: $base0b
    duration: $base08
    range: $base08
    float: $base08
    string: $base04
    nothing: $base08
    binary: $base08
    cellpath: $base08
    hints: dark_gray

    # shape_garbage: { fg: $base07 bg: $base08 attr: b} # base16 white on red
    # but i like the regular white on red for parse errors
    shape_garbage: { fg: "#FFFFFF" bg: "#FF0000" attr: b}
    shape_bool: $base0d
    shape_int: { fg: $base0e attr: b}
    shape_float: { fg: $base0e attr: b}
    shape_range: { fg: $base0a attr: b}
    shape_internalcall: { fg: $base0c attr: b}
    shape_external: $base0c
    shape_externalarg: { fg: $base0b attr: b}
    shape_literal: $base0d
    shape_operator: $base0a
    shape_signature: { fg: $base0b attr: b}
    shape_string: $base0b
    shape_filepath: $base0d
    shape_globpattern: { fg: $base0d attr: b}
    shape_variable: $base0e
    shape_flag: { fg: $base0d attr: b}
    shape_custom: {attr: b}
}

# now let's apply our regular config settings but also apply the "color_config:" theme that we specified above.

let config = {
  filesize_metric: true
  table_mode: rounded # basic, compact, compact_double, light, thin, with_love, rounded, reinforced, heavy, none, other
  use_ls_colors: true
  color_config: $base16_theme # <-- this is the theme
  use_grid_icons: true
  footer_mode: always #always, never, number_of_rows, auto
  animate_prompt: false
  float_precision: 2
  use_ansi_coloring: true
  filesize_format: "b" # b, kb, kib, mb, mib, gb, gib, tb, tib, pb, pib, eb, eib, auto
  edit_mode: emacs # vi
  max_history_size: 10000
  log_level: error
}

如果你想在主题设计上火力全开,你需要把我在一开始提到的所有项目作为主题,包括LS_COLORS和提示。祝您好运!

在终端上使用浅色背景

Nushell 的默认配置文件包含一个浅色主题定义,如果你在浅色背景的终端上工作,你可以很容易地应用浅色主题:

# in $nu.config-file
$env.config = {
  ...
  color_config: $dark_theme   # 如果你需要浅色主题, 可以将 `$dark_theme` 替换为 `$light_theme`
  ...
}

你只需要将 $dark_theme 替换为 $light_theme 就可以切换到浅色主题了:

# in $nu.config-file
$env.config = {
  ...
  color_config: $light_theme   # if you want a light theme, replace `$dark_theme` to `$light_theme`
  ...
}