Skip to content

Commit 5ae01ef

Browse files
author
Manuel Einfalt
committed
Ticket #4981: do not show color warning with --nocolor
Suppress the color capability warning when --nocolor is used. Keep the warning if a skin is explicitly requested. Signed-off-by: Manuel Einfalt <einfalt1@proton.me>
1 parent dc2dbc8 commit 5ae01ef

7 files changed

Lines changed: 218 additions & 17 deletions

File tree

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,7 @@ tests/Makefile
690690
tests/lib/Makefile
691691
tests/lib/mcconfig/Makefile
692692
tests/lib/search/Makefile
693+
tests/lib/skin/Makefile
693694
tests/lib/strutil/Makefile
694695
tests/lib/vfs/Makefile
695696
tests/lib/vfs/mc.charsets

lib/skin/common.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ mc_skin_t mc_skin__default;
4545

4646
/*** file scope variables ************************************************************************/
4747

48-
static gboolean mc_skin_is_init = FALSE;
49-
5048
/* --------------------------------------------------------------------------------------------- */
5149
/*** file scope functions ************************************************************************/
5250
/* --------------------------------------------------------------------------------------------- */
@@ -149,27 +147,28 @@ mc_skin_init (const gchar *skin_override, GError **mcerror)
149147
}
150148
if (is_good_init && mc_skin__default.have_true_colors && !tty_use_truecolors (&error))
151149
{
152-
mc_propagate_error (mcerror, 0,
153-
_ ("Unable to use '%s' skin with true colors support:\n%s\nDefault "
154-
"skin has been loaded"),
155-
mc_skin__default.name, error->message);
150+
if (!mc_global.tty.disable_colors || skin_override != NULL)
151+
mc_propagate_error (mcerror, 0,
152+
_ ("Unable to use '%s' skin with true colors support:\n%s\n"
153+
"Default skin has been loaded"),
154+
mc_skin__default.name, error->message);
156155
g_error_free (error);
157156
mc_skin_try_to_load_default ();
158157
(void) mc_skin_ini_file_parse (&mc_skin__default);
159158
is_good_init = FALSE;
160159
}
161160
if (is_good_init && mc_skin__default.have_256_colors && !tty_use_256colors (&error))
162161
{
163-
mc_propagate_error (mcerror, 0,
164-
_ ("Unable to use '%s' skin with 256 colors support:\n%s\nDefault "
165-
"skin has been loaded"),
166-
mc_skin__default.name, error->message);
162+
if (!mc_global.tty.disable_colors || skin_override != NULL)
163+
mc_propagate_error (mcerror, 0,
164+
_ ("Unable to use '%s' skin with 256 colors support:\n%s\n"
165+
"Default skin has been loaded"),
166+
mc_skin__default.name, error->message);
167167
g_error_free (error);
168168
mc_skin_try_to_load_default ();
169169
(void) mc_skin_ini_file_parse (&mc_skin__default);
170170
is_good_init = FALSE;
171171
}
172-
mc_skin_is_init = TRUE;
173172
return is_good_init;
174173
}
175174

@@ -188,8 +187,6 @@ mc_skin_deinit (void)
188187

189188
mc_config_deinit (mc_skin__default.config);
190189
mc_skin__default.config = NULL;
191-
192-
mc_skin_is_init = FALSE;
193190
}
194191

195192
/* --------------------------------------------------------------------------------------------- */

lib/tty/color.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ gboolean need_convert_256color = FALSE;
6262

6363
/*** file scope variables ************************************************************************/
6464

65-
static GHashTable *mc_tty_color__hashtable = NULL;
65+
#ifndef HAVE_TESTS
66+
extern GHashTable *mc_tty_color__hashtable;
67+
#endif
68+
69+
MC_TESTABLE GHashTable *mc_tty_color__hashtable = NULL;
6670

6771
/* --------------------------------------------------------------------------------------------- */
6872
/*** file scope functions ************************************************************************/

lib/tty/color.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ void tty_color_free_all (void);
6969
void tty_setcolor (int color);
7070
void tty_set_normal_attrs (void);
7171

72-
extern gboolean tty_use_256colors (GError **error);
73-
extern gboolean tty_use_truecolors (GError **error);
72+
MC_MOCKABLE gboolean tty_use_256colors (GError **error);
73+
MC_MOCKABLE gboolean tty_use_truecolors (GError **error);
7474

7575
/*** inline functions ****************************************************************************/
7676

tests/lib/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
PACKAGE_STRING = "/lib"
22

3-
SUBDIRS = . mcconfig search strutil vfs widget
3+
SUBDIRS = . mcconfig search strutil vfs widget skin
44

55
AM_CPPFLAGS = $(GLIB_CFLAGS) -I$(top_srcdir) @CHECK_CFLAGS@
66

tests/lib/skin/Makefile.am

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
PACKAGE_STRING = "/lib/skin"
2+
3+
AM_CPPFLAGS = \
4+
$(GLIB_CFLAGS) \
5+
-I$(top_srcdir) \
6+
@CHECK_CFLAGS@
7+
8+
LIBS = @CHECK_LIBS@ \
9+
$(top_builddir)/lib/skin/libmcskin.la
10+
11+
12+
13+
if ENABLE_MCLIB
14+
LIBS += $(GLIB_LIBS)
15+
endif
16+
17+
TESTS = \
18+
common__mc_skin_init
19+
20+
check_PROGRAMS = $(TESTS)
21+
22+
common__mc_skin_init_SOURCES = \
23+
common__mc_skin_init.c
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
/*
2+
lib/skin - mc_skin_init() function testing
3+
4+
Copyright (C) 2026
5+
Free Software Foundation, Inc.
6+
7+
Written by:
8+
Manuel Einfalt <einfalt1@proton.me>, 2026
9+
10+
This file is part of the Midnight Commander.
11+
12+
The Midnight Commander is free software: you can redistribute it
13+
and/or modify it under the terms of the GNU General Public License as
14+
published by the Free Software Foundation, either version 3 of the License,
15+
or (at your option) any later version.
16+
17+
The Midnight Commander is distributed in the hope that it will be useful,
18+
but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
GNU General Public License for more details.
21+
22+
You should have received a copy of the GNU General Public License
23+
along with this program. If not, see <https://www.gnu.org/licenses/>.
24+
*/
25+
26+
#define TEST_SUITE_NAME "/lib/skin"
27+
28+
#include "tests/mctest.h"
29+
#include "lib/util.h"
30+
#include "lib/skin.h"
31+
32+
#include "lib/strutil.h" // str_init_strings
33+
#include "src/vfs/local/local.h" // vfs_init_localfs
34+
35+
/* --------------------------------------------------------------------------------------------- */
36+
37+
static const struct skin_tests // xterm, xterm-256color, xterm-direct
38+
{
39+
const char *term;
40+
const gboolean nocolor;
41+
const gchar *skin_name;
42+
const gboolean has_error;
43+
const gboolean ret;
44+
} skin_tests[] = { { "xterm", FALSE, "dark", FALSE, TRUE },
45+
{ "xterm", FALSE, "julia256", TRUE, FALSE },
46+
{ "xterm", FALSE, "seasons-summer16M", TRUE, FALSE },
47+
{ "xterm", TRUE, "dark", FALSE, TRUE },
48+
{ "xterm", TRUE, "julia256", TRUE, FALSE },
49+
{ "xterm", TRUE, "seasons-summer16M", TRUE, FALSE },
50+
51+
{ "xterm-256color", FALSE, "dark", FALSE, TRUE },
52+
{ "xterm-256color", FALSE, "julia256", FALSE, TRUE },
53+
{ "xterm-256color", FALSE, "seasons-summer16M", TRUE, FALSE },
54+
{ "xterm-256color", TRUE, "dark", FALSE, TRUE },
55+
{ "xterm-256color", TRUE, "julia256", FALSE, TRUE },
56+
{ "xterm-256color", TRUE, "seasons-summer16M", TRUE, FALSE },
57+
58+
{ "xterm-256direct", FALSE, "dark", FALSE, TRUE },
59+
{ "xterm-256direct", FALSE, "julia256", FALSE, TRUE },
60+
{ "xterm-256direct", FALSE, "seasons-summer16M", FALSE, TRUE },
61+
{ "xterm-256direct", TRUE, "dark", FALSE, TRUE },
62+
{ "xterm-256direct", TRUE, "julia256", FALSE, TRUE },
63+
{ "xterm-256direct", TRUE, "seasons-summer16M", FALSE, TRUE } };
64+
65+
extern GHashTable *mc_tty_color__hashtable;
66+
static char inipath[PATH_MAX];
67+
68+
/* --------------------------------------------------------------------------------------------- */
69+
70+
static void
71+
setup (void)
72+
{
73+
getcwd (inipath, PATH_MAX);
74+
strcpy (inipath + strlen (inipath), "/../../../misc/");
75+
76+
mc_global.share_data_dir = inipath;
77+
mc_global.sysconfig_dir = inipath;
78+
79+
str_init_strings (NULL);
80+
vfs_init ();
81+
vfs_init_localfs ();
82+
tty_color_role_to_pair = g_new (int, COLOR_MAP_SIZE);
83+
mc_tty_color__hashtable = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
84+
}
85+
86+
static void
87+
teardown (void)
88+
{
89+
vfs_shut ();
90+
str_uninit_strings ();
91+
}
92+
93+
/* --------------------------------------------------------------------------------------------- */
94+
95+
// Mock
96+
gboolean
97+
tty_use_truecolors (GError **error)
98+
{
99+
char *termname = getenv ("TERM");
100+
101+
if (termname == NULL)
102+
{
103+
mc_propagate_error (error, 0, _ ("foobar"));
104+
return FALSE;
105+
}
106+
107+
if (strcmp (termname, "xterm-256direct") == 0)
108+
return TRUE;
109+
110+
mc_propagate_error (error, 0, _ ("foobar"));
111+
return FALSE;
112+
}
113+
114+
// Mock
115+
gboolean
116+
tty_use_256colors (GError **error)
117+
{
118+
char *termname = getenv ("TERM");
119+
120+
if (termname == NULL)
121+
{
122+
mc_propagate_error (error, 0, _ ("foobar"));
123+
return FALSE;
124+
}
125+
126+
if (strcmp (termname, "xterm-256color") == 0 || tty_use_truecolors (NULL))
127+
return TRUE;
128+
129+
mc_propagate_error (error, 0, _ ("foobar"));
130+
return FALSE;
131+
}
132+
133+
/* --------------------------------------------------------------------------------------------- */
134+
135+
START_PARAMETRIZED_TEST (skin_test, skin_tests)
136+
{
137+
GError *mcerror = NULL;
138+
139+
mc_global.tty.disable_colors = data->nocolor;
140+
setenv ("TERM", data->term, 1);
141+
142+
if (data->ret)
143+
{
144+
mctest_assert_true (mc_skin_init (data->skin_name, &mcerror));
145+
}
146+
else
147+
mctest_assert_false (mc_skin_init (data->skin_name, &mcerror));
148+
149+
if (data->has_error)
150+
{
151+
mctest_assert_not_null (mcerror);
152+
g_error_free (mcerror);
153+
}
154+
else
155+
mctest_assert_null (mcerror);
156+
}
157+
END_PARAMETRIZED_TEST
158+
159+
/* --------------------------------------------------------------------------------------------- */
160+
161+
int
162+
main (void)
163+
{
164+
TCase *tc_core;
165+
166+
tc_core = tcase_create ("Core");
167+
tcase_add_checked_fixture (tc_core, setup, teardown);
168+
169+
// Add new tests here: ***************
170+
mctest_add_parameterized_test (tc_core, skin_test, skin_tests);
171+
// ***********************************
172+
173+
return mctest_run_all (tc_core);
174+
}
175+
176+
/* --------------------------------------------------------------------------------------------- */

0 commit comments

Comments
 (0)