3838
3939#include "lib/global.h"
4040
41+ #include "tty.h"
4142#include "tty-ncurses.h"
4243#include "color.h" // variables
4344#include "color-internal.h"
5354/*** file scope variables ************************************************************************/
5455
5556static GHashTable * mc_tty_color_color_pair_attrs = NULL ;
57+ static int overlay_colors = 0 ;
5658
5759/* --------------------------------------------------------------------------------------------- */
5860/*** file scope functions ************************************************************************/
@@ -127,6 +129,10 @@ tty_color_init_lib (gboolean disable, gboolean force)
127129 use_colors = TRUE;
128130 start_color ();
129131 use_default_colors ();
132+
133+ // Extended color mode detection routines must first be called before loading any skin
134+ tty_use_256colors (NULL );
135+ tty_use_truecolors (NULL );
130136 }
131137
132138 mc_tty_color_color_pair_attrs = g_hash_table_new_full (
@@ -179,8 +185,9 @@ tty_color_try_alloc_lib_pair (tty_color_lib_pair_t *mc_color_pair)
179185 ibg = mc_color_pair -> bg ;
180186 attr = mc_color_pair -> attr ;
181187
182- // In legacy color mode, change bright colors into bold
183- if (!tty_use_256colors (NULL ) && !tty_use_truecolors (NULL ))
188+ // If we have 8 indexed colors only, change foreground bright colors into bold and
189+ // background bright colors to basic colors
190+ if (COLORS <= 8 || (tty_use_truecolors (NULL ) && overlay_colors <= 8 ))
184191 {
185192 if (ifg >= 8 && ifg < 16 )
186193 {
@@ -191,11 +198,31 @@ tty_color_try_alloc_lib_pair (tty_color_lib_pair_t *mc_color_pair)
191198 if (ibg >= 8 && ibg < 16 )
192199 {
193200 ibg &= 0x07 ;
194- // attr | = A_BOLD | A_REVERSE ;
195201 }
196202 }
197203
204+ // Shady trick: if we don't have the exact color, because it is overlaid by backwards
205+ // compatibility indexed values, just borrow one degree of red. The user won't notice :)
206+ if ((ifg & FLAG_TRUECOLOR ) != 0 )
207+ {
208+ ifg &= ~FLAG_TRUECOLOR ;
209+ if (ifg != 0 && ifg <= overlay_colors )
210+ ifg += (1 << 16 );
211+ }
212+
213+ if ((ibg & FLAG_TRUECOLOR ) != 0 )
214+ {
215+ ibg &= ~FLAG_TRUECOLOR ;
216+ if (ibg != 0 && ibg <= overlay_colors )
217+ ibg += (1 << 16 );
218+ }
219+
220+ #if NCURSES_VERSION_PATCH >= 20170401 && defined(NCURSES_EXT_COLORS ) && defined(NCURSES_EXT_FUNCS ) \
221+ && defined(HAVE_NCURSES_WIDECHAR )
222+ init_extended_pair (mc_color_pair -> pair_index , ifg , ibg );
223+ #else
198224 init_pair (mc_color_pair -> pair_index , ifg , ibg );
225+ #endif
199226 mc_tty_color_save_attr (mc_color_pair -> pair_index , attr );
200227 }
201228}
@@ -231,17 +258,56 @@ tty_use_256colors (GError **error)
231258{
232259 (void ) error ;
233260
234- return (COLORS == 256 );
261+ overlay_colors = tty_tigetnum ("CO" , NULL );
262+
263+ if (COLORS == 256 || (COLORS > 256 && overlay_colors == 256 ))
264+ return TRUE;
265+
266+ if (tty_use_truecolors (NULL ))
267+ {
268+ need_convert_256color = TRUE;
269+ return TRUE;
270+ }
271+
272+ g_set_error (error , MC_ERROR , -1 ,
273+ _ ("\nIf your terminal supports 256 colors, you need to set your TERM\n"
274+ "environment variable to match your terminal, perhaps using\n"
275+ "a *-256color or *-direct256 variant. Use the 'toe -a'\n"
276+ "command to list all available variants on your system.\n" ));
277+ return FALSE;
235278}
236279
237280/* --------------------------------------------------------------------------------------------- */
238281
239282gboolean
240283tty_use_truecolors (GError * * error )
241284{
242- // Not yet supported in ncurses
243- g_set_error (error , MC_ERROR , -1 , _ ("True color not supported with ncurses." ));
285+ // Low level true color is supported since ncurses 6.0 patch 20170401 preceding release
286+ // of ncurses 6.1. It needs ABI 6 or higher.
287+ #if !(NCURSES_VERSION_PATCH >= 20170401 && defined(NCURSES_EXT_COLORS ) \
288+ && defined(NCURSES_EXT_FUNCS ) && defined(HAVE_NCURSES_WIDECHAR ))
289+ g_set_error (error , MC_ERROR , -1 ,
290+ _ ("For true color support, you need version 6.1 or later of the ncurses\n"
291+ "library with wide character and ABI 6 or higher support.\n"
292+ "Please upgrade your system.\n" ));
244293 return FALSE;
294+ #else
295+ // We support only bool RGB cap configuration (8:8:8 bits), but the other variants are so rare
296+ // that we don't need to bother.
297+ if (!(tty_tigetflag ("RGB" , NULL ) && COLORS == COLORS_TRUECOLOR ))
298+ {
299+ g_set_error (
300+ error , MC_ERROR , -1 ,
301+ _ ("\nIf your terminal supports true colors, you need to set your TERM\n"
302+ "environment variable to a *-direct256, *-direct16, or *-direct variant.\n"
303+ "Use the 'toe -a' command to list all available variants on your system.\n" ));
304+ return FALSE;
305+ }
306+
307+ overlay_colors = tty_tigetnum ("CO" , NULL );
308+
309+ return TRUE;
310+ #endif
245311}
246312
247313/* --------------------------------------------------------------------------------------------- */
0 commit comments