From 25cdd9969fe145061682305f93c1c81b50823090 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 6 Jun 2024 15:01:26 +0800 Subject: [PATCH] Color: support `{#keys}` and `{#title}` in `---format` --- CHANGELOG.md | 1 + presets/examples/16.jsonc | 30 ++++++++++++++---------------- src/common/option.c | 8 ++++++++ src/data/help_color.txt | 24 +++++++++++++++++++----- 4 files changed, 42 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ccf1d6fe..a7e5e5047 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ Features: * Make it possible to shorten the theme and icons output (Theme / Icons) * Support `-l '?'` to show a question mark * Add new module `CPUCache` to display CPU cache sizes (CPUCache) +* In `---format`, `{#keys}` and `{#title}` can be used to reference the color of keys and title Bugfixes: * Remove shebangs from completions diff --git a/presets/examples/16.jsonc b/presets/examples/16.jsonc index 67f4532fd..6809c3e81 100644 --- a/presets/examples/16.jsonc +++ b/presets/examples/16.jsonc @@ -7,8 +7,7 @@ } }, "display": { - "separator": "", - "keyWidth": 15 + "separator": " " }, "modules": [ { @@ -16,56 +15,55 @@ "type": "custom" }, { - // draw borders first to make colors of left and right border consistant - "key": "║ ║\u001b[11D{#31} user", + "key": "║ {#31} user {#keys}║", "type": "title", "format": "{1}" }, { - "key": "║ ║\u001b[11D{#32}󰇅 hname", + "key": "║ {#32}󰇅 hname {#keys}║", "type": "title", "format": "{2}" }, { - "key": "║ ║\u001b[11D{#33}󰅐 uptime", + "key": "║ {#33}󰅐 uptime {#keys}║", "type": "uptime" }, { - "key": "║ ║\u001b[11D{#34}󰟾 distro", + "key": "║ {#34}󰟾 distro {#keys}║", "type": "os" }, { - "key": "║ ║\u001b[11D{#35} kernel", + "key": "║ {#35} kernel {#keys}║", "type": "kernel" }, { - "key": "║ ║\u001b[11D{#36}󰇄 desktop", + "key": "║ {#36}󰇄 desktop {#keys}║", "type": "de" }, { - "key": "║ ║\u001b[11D{#31} term", + "key": "║ {#31} term {#keys}║", "type": "terminal" }, { - "key": "║ ║\u001b[11D{#32} shell", + "key": "║ {#32} shell {#keys}║", "type": "shell" }, { - "key": "║ ║\u001b[11D{#33}󰍛 cpu", + "key": "║ {#33}󰍛 cpu {#keys}║", "type": "cpu", "showPeCoreCount": true }, { - "key": "║ ║\u001b[11D{#34}󰉉 disk", + "key": "║ {#34}󰉉 disk {#keys}║", "type": "disk", "folders": "/" }, { - "key": "║ ║\u001b[11D{#35} memory", + "key": "║ {#35} memory {#keys}║", "type": "memory" }, { - "key": "║ ║\u001b[11D{#36}󰩟 network", + "key": "║ {#36}󰩟 network {#keys}║", "type": "localip", "format": "{1} ({4})" }, @@ -74,7 +72,7 @@ "type": "custom" }, { - "key": "║ ║\u001b[11D{#39} colors", + "key": "║ {#39} colors {#keys}║", "type": "colors", "symbol": "circle" }, diff --git a/src/common/option.c b/src/common/option.c index de2e487b4..e0bf5fc3f 100644 --- a/src/common/option.c +++ b/src/common/option.c @@ -1,3 +1,4 @@ +#include "fastfetch.h" #include "common/option.h" #include "common/color.h" #include "util/stringUtils.h" @@ -153,6 +154,8 @@ void ffOptionParseColorNoClear(const char* value, FFstrbuf* buffer) { #define FF_APPEND_COLOR_CODE_COND(prefix, code) \ if(ffStrStartsWithIgnCase(value, #prefix)) { ffStrbufAppendS(buffer, code); value += strlen(#prefix); continue; } + #define FF_APPEND_COLOR_PROP_COND(prefix, prop) \ + if(ffStrStartsWithIgnCase(value, #prefix)) { if (instance.config.display.prop.length) ffStrbufAppend(buffer, &instance.config.display.prop); else ffStrbufAppendS(buffer, FF_COLOR_FG_DEFAULT); value += strlen(#prefix); continue; } if (ffCharIsEnglishAlphabet(value[0])) { @@ -182,11 +185,16 @@ void ffOptionParseColorNoClear(const char* value, FFstrbuf* buffer) else FF_APPEND_COLOR_CODE_COND(light_magenta, FF_COLOR_FG_LIGHT_MAGENTA) else FF_APPEND_COLOR_CODE_COND(light_cyan, FF_COLOR_FG_LIGHT_CYAN) else FF_APPEND_COLOR_CODE_COND(light_white, FF_COLOR_FG_LIGHT_WHITE) + else FF_APPEND_COLOR_PROP_COND(keys, colorKeys) + else FF_APPEND_COLOR_PROP_COND(title, colorTitle) + else FF_APPEND_COLOR_PROP_COND(output, colorOutput) + else FF_APPEND_COLOR_PROP_COND(separator, colorSeparator) } ffStrbufAppendC(buffer, *value); ++value; #undef FF_APPEND_COLOR_CODE_COND + #undef FF_APPEND_COLOR_PROP_COND } } diff --git a/src/data/help_color.txt b/src/data/help_color.txt index 1804498e9..9adcb1c88 100644 --- a/src/data/help_color.txt +++ b/src/data/help_color.txt @@ -1,10 +1,24 @@ Usage: fastfetch --color +Shortcut of `fastfetch --color-keys --color-title `. +If no color is set, the main color of the logo will be used. + +Following information applies to all settings + must be a color encoding as ANSI escape sequences. It is inserted between "ESC[" and "m". Infos about them can be found here: https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters. Examples: - --color 35: sets the color to magenta. `--color magenta` is also supported - --color 38;5;38: sets the color to 38th color of ANSI 256 colors - --color 4;92: sets the color to bright Green with underline. `--color underline_green` is also supported - --color 5;104: blinking text on a blue background -If no color is set, the main color of the logo will be used. + 35: sets the color to magenta + 38;5;38: sets the color to 38th color of ANSI 256 colors + 4;92: sets the color to bright green with underline + 5;104: blinking text on a blue background + +ANSI named colors are also supported: + magenta: equivalent to `35` + underline_bright_green: equivalent to `4;92` + +After v2.15.0, some special keys can be used: + keys: use the color set by `--color-keys` + title: use the color set by `--color-title` + output: use the color set by `--color-output` + separator: use the color set by `--color-separator`