netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Matteo Croce <technoboy85@gmail.com>
To: netdev@vger.kernel.org
Cc: Stephen Hemminger <stephen@networkplumber.org>,
	Phil Sutter <phil@nwl.cc>, Matteo Croce <teknoraver@meta.com>
Subject: [PATCH iproute2-next v2] color: default to dark color theme
Date: Mon, 10 Mar 2025 21:36:09 +0100	[thread overview]
Message-ID: <20250310203609.4341-1-technoboy85@gmail.com> (raw)

From: Matteo Croce <teknoraver@meta.com>

The majority of Linux terminals are using a dark background.
iproute2 tries to detect the color theme via the `COLORFGBG` environment
variable, and defaults to light background if not set.

Change the default behaviour to dark background, and while at it change
the current logic which assumes that the color code is a single digit.

Signed-off-by: Matteo Croce <teknoraver@meta.com>
---
 lib/color.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/lib/color.c b/lib/color.c
index cd0f9f75..b883aa1c 100644
--- a/lib/color.c
+++ b/lib/color.c
@@ -72,7 +72,7 @@ static enum color attr_colors_dark[] = {
 	C_CLEAR
 };
 
-static int is_dark_bg;
+static int is_light_bg;
 static int color_is_enabled;
 
 static void enable_color(void)
@@ -128,10 +128,12 @@ static void set_color_palette(void)
 	 * values separated by semicolons; we want the last value in either case.
 	 * If this value is 0-6 or 8, background is dark.
 	 */
-	if (p && (p = strrchr(p, ';')) != NULL
-		&& ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
-		&& p[2] == '\0')
-		is_dark_bg = 1;
+	if (p && (p = strrchr(p, ';')) != NULL) {
+		int bg = atoi(p + 1);
+
+		if (bg == 7 || (bg >= 9 && bg <= 15))
+			is_light_bg = 1;
+	}
 }
 
 __attribute__((format(printf, 3, 4)))
@@ -150,8 +152,8 @@ int color_fprintf(FILE *fp, enum color_attr attr, const char *fmt, ...)
 		goto end;
 	}
 
-	ret += fprintf(fp, "%s", color_codes[is_dark_bg ?
-		attr_colors_dark[attr] : attr_colors_light[attr]]);
+	ret += fprintf(fp, "%s", color_codes[is_light_bg ?
+		attr_colors_light[attr] : attr_colors_dark[attr]]);
 
 	ret += vfprintf(fp, fmt, args);
 	ret += fprintf(fp, "%s", color_codes[C_CLEAR]);
-- 
2.48.1


             reply	other threads:[~2025-03-10 20:36 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-10 20:36 Matteo Croce [this message]
2025-03-10 21:12 ` [PATCH iproute2-next v2] color: default to dark color theme Stephen Hemminger
2025-03-13 11:28   ` Phil Sutter
2025-03-13 11:41     ` Matteo Croce
2025-03-13 12:05       ` Phil Sutter
2025-03-13 16:30         ` Stephen Hemminger
2025-03-14 17:12           ` Phil Sutter
2025-03-14 17:37             ` Phil Sutter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250310203609.4341-1-technoboy85@gmail.com \
    --to=technoboy85@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=phil@nwl.cc \
    --cc=stephen@networkplumber.org \
    --cc=teknoraver@meta.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).