手动更改 xfce4 终端的配色方案

手动更改 xfce4 终端的配色方案

我想绑定一个快捷方式来更改我的日晒配色方案(深色和浅色)。为此,我需要知道一个用于更改终端配色方案的命令,但我还没有找到这样的命令,我不得不一次又一次地进入首选项菜单并切换预设方案。有没有办法制作这种终端配色方案切换器?

答案1

XFCE4 终端将用户首选项存储在$XDG_CONFIG_HOME/xfce4/terminal/terminalrc(通常,~/.config/xfce4/terminal/terminalrc如果您未更改任何首选项,则可能不存在)。我发现 XFCE4 终端会监视此文件并在其更改时重新加载设置,因此您可以写入此文件来更改设置。

但是,没有可以编辑的单个条目来更改颜色。各种调色板实际上是一组设置。例如:

$ cat /usr/share/xfce4/terminal/colorschemes/solarized-dark.theme 
[Scheme]
Name=Solarized (dark)
Name[bg]=Златисто (тъмно)
Name[fr]=Solarisé (foncé)
Name[nl]=Overbelicht (donker)
Name[th]=ในแสงแดด (มืด)
Name[uk]=Золотистий (темний)
ColorForeground=#839496
ColorBackground=#002b36
ColorCursor=#93a1a1
TabActivityColor=#dc322f
ColorPalette=#073642;#dc322f;#859900;#b58900;#268bd2;#d33682;#2aa198;#eee8d5;#002b36;#cb4b16;#586e75;#657b83;#839496;#6c71c4;#93a1a1;#fdf6e3
ColorBold=#93a1a1
ColorBoldUseDefault=FALSE

看起来terminalrc像这样:

$ cat .config/xfce4/terminal/terminalrc                           
[Configuration]
ColorForeground=#839496
FontName=Ubuntu Mono 12
ColorBackground=#002b36
ColorCursor=#93a1a1
ColorBold=#93a1a1
ColorBoldUseDefault=FALSE
ColorPalette=#073642;#dc322f;#859900;#b58900;#268bd2;#d33682;#2aa198;#eee8d5;#002b36;#cb4b16;#586e75;#657b83;#839496;#6c71c4;#93a1a1;#fdf6e3
TabActivityColor=#dc322f
TabActivityColor=#dc322f

正如所见,没有办法轻易识别这些颜色来自哪个调色板。

编写脚本似乎并不安全,但这是一个低效的尝试:

#! /bin/bash
if ! [[ -f /usr/share/xfce4/terminal/colorschemes/$1.theme ]]
then
    echo "No such colorscheme: $1"
    exit 1
fi
cd ~/.config/xfce4/terminal
# strip settings from any themes
grep -Fxvf <(cat /usr/share/xfce4/terminal/colorschemes/*.theme) terminalrc > .terminalrc.tmp
grep -v -e Name -e Scheme "/usr/share/xfce4/terminal/colorschemes/$1.theme" >> .terminalrc.tmp
cp terminalrc terminalrc.bak
mv .terminalrc.tmp terminalrc

将其复制到您的某个位置PATH(例如~/bin/xfce-color-switch)。然后:

$ xfce4-color-switch dark-pastels

在此处输入图片描述

$ xfce4-color-switch solarized   
No such colorscheme: solarized
$ xfce4-color-switch solarized-dark

在此处输入图片描述

您必须记住这些配色方案名称,但一旦我弄清楚了,我会添加有关如何添加制表符补全的说明。

答案2

更好的方法是:创建自己的配色方案并从众多配色方案中选择一种。

为此,请创建目录:

mkdir -p ~/.local/share/xfce4/terminal/colorschemes

并将如下文件放入其中:

[Scheme]
Name=my good profile
ColorForeground=#4ccd4ccd4ccd
ColorBackground=#f851f465ebe7
TabActivityColor=#d8d8a9a97f7f
ColorCursor=#f0f0b6b66666
ColorSelection=#16163b3b5959
ColorPalette=rgb(0,0,0);rgb(170,0,0);rgb(0,170,0);rgb(170,85,0);rgb(0,0,170);rgb(170,0,170);rgb(0,138,138);rgb(170,170,170);rgb(85,85,85);rgb(255,85,85);rgb(85,255,85);rgb(196,160,0);rgb(85,85,255);rgb(255,85,255);rgb(68,204,204);rgb(255,255,255)

如果您的目录中已经有一个配置好的 terminalrc 文件~/.config/xfce4/terminal,那么您只需复制相应的行即可。

执行此操作后,您的新配色方案将出现在主列表中。例如,您可以同时拥有自定义白色主题和自定义黑色主题。

答案3

梵高为 xfce4-terminal 应用颜色主题(https://github.com/Gogh-Co/Gogh/blob/master/apply-colors.sh#L809):

# XFCE4 terminal has no profiles, instead it uses color presets
SCHEMEDIR="${HOME}/.local/share/xfce4/terminal/colorschemes"
CONFFILE="${HOME}/.config/xfce4/terminal/terminalrc"

if [[ ! (-w "${CONFFILE}") ]]; then
    if [[ -r "${XDG_CONFIG_DIRS%%:*}/Terminal/terminalrc" ]]; then
        cp "${XDG_CONFIG_DIRS%%:*}/Terminal/terminalrc" ${CONFFILE}
    else
        echo "ERROR: config file not present or not writable!"
        exit 1
    fi
fi

[[ -d "${SCHEMEDIR}" ]] || mkdir -p "${SCHEMEDIR}"

F_NAME=${PROFILE_NAME// /-}
F_NAME=$(echo ${F_NAME} | tr -d ":()")
F_NAME=$(echo "${F_NAME}" | awk '{print tolower($0)}')

FF_NAME="${SCHEMEDIR}/${F_NAME}.theme"

touch "${FF_NAME}"

L_COLORCURSOR="ColorCursor=${CURSOR_COLOR}"
L_COLORPALETTE="ColorPalette=${COLOR_01};${COLOR_02};${COLOR_03};${COLOR_04};${COLOR_05};${COLOR_06};${COLOR_07};${COLOR_08};${COLOR_09};${COLOR_10};${COLOR_11};${COLOR_12};${COLOR_13};${COLOR_14};${COLOR_15};${COLOR_16}"

printf '%s\n' \
    "; Generated by Gogh" \
    "; https://Gogh-Co.github.io/Gogh" \
    "[Scheme]" \
    "Name=${PROFILE_NAME}" \
    "ColorForeground=${FOREGROUND_COLOR}" \
    "ColorBackground=${BACKGROUND_COLOR}" \
    "${L_COLORCURSOR}" \
    "${L_COLORPALETTE}" \
    "ColorCursorUseDefault=FALSE" > ${FF_NAME}

# apply last theme in queue
# xfce4-terminal monitors its rc file and doesn't reference
# any of the themes in there. The color settings need to
# be written there directly.
if ((LOOP == OPTLENGTH)); then
    read -r -p "All done - apply new theme? [y/N] " -n 1 XFCE4_APPLY_CURR_THEME
    if [[ ${XFCE4_APPLY_CURR_THEME::1} =~ ^(y|Y)$ ]]; then
        if grep -q "^ColorPalette=" "${CONFFILE}"; then
            sed -i -r -e "s/^ColorPalette=.*/${L_COLORPALETTE}/" "${CONFFILE}"
        else
            echo "${L_COLORPALETTE}" >> "${CONFFILE}"
        fi

        if grep -q "^ColorCursor=" "${CONFFILE}"; then
            sed -i -r -e "s/^ColorCursor=.*/${L_COLORCURSOR}/" "${CONFFILE}"
        else
            echo "${L_COLORCURSOR}" >> "${CONFFILE}"
        fi

        if grep -q "^ColorForeground=" "${CONFFILE}"; then
            sed -i -r -e "s/^ColorForeground=.*/ColorForeground=${FOREGROUND_COLOR}/" "${CONFFILE}"
        else
            echo "ColorForeground=${FOREGROUND_COLOR}" >> "${CONFFILE}"
        fi

        if grep -q "^ColorBackground=" "${CONFFILE}"; then
            sed -i -r -e "s/^ColorBackground=.*/ColorBackground=${BACKGROUND_COLOR}/" "${CONFFILE}"
        else
            echo "ColorBackground=${BACKGROUND_COLOR}" >> "${CONFFILE}"
        fi

        if grep -q "^ColorCursorUseDefault=FALSE" "${CONFFILE}"; then
            true
        else
            echo "ColorCursorUseDefault=FALSE" >> "${CONFFILE}"
        fi
    fi
fi

unset SCHEMEDIR
unset CONFFILE
unset PROFILE_NAME
unset F_NAME
unset FF_NAME
unset L_COLORCURSOR
unset L_COLORPALETTE
exit 0

它不是完整的代码,但可能会有帮助。

相关内容