Add ncurses library to check terminal's color support

This commit is contained in:
Thomas Jensen 2023-05-08 21:12:00 +02:00
parent 89111c55b6
commit 241459e5b4
No known key found for this signature in database
GPG Key ID: A4ACEE270D0FB7DB
8 changed files with 98 additions and 8 deletions

View File

@ -25,7 +25,7 @@ jobs:
- name: add-packages
run: |
sudo apt-get update -y
sudo apt-get install -y git flex bison libunistring-dev libpcre2-dev diffutils vim-common lcov libcmocka-dev
sudo apt-get install -y git flex bison libunistring-dev libpcre2-dev libncurses-dev diffutils vim-common lcov libcmocka-dev
- name: make cov
run: make cov

View File

@ -7,7 +7,8 @@
"${workspaceFolder}/src/misc",
"${workspaceFolder}/out",
"${workspaceFolder}/vendor/pcre2-10.40/src",
"${workspaceFolder}/vendor/cmocka-1.1.0/include"
"${workspaceFolder}/vendor/cmocka-1.1.0/include",
"C:/MinGW/include/ncurses"
],
"defines": [
"_DEBUG",

View File

@ -27,6 +27,8 @@ PCRE2_VERSION = 10.40
PCRE2_DIR = vendor/pcre2-$(PCRE2_VERSION)
LIBUNISTRING_VERSION = 1.0
LIBUNISTRING_DIR = vendor/libunistring-$(LIBUNISTRING_VERSION)
LIBNCURSES_VERSION = 6.4
LIBNCURSES_DIR = vendor/ncurses-$(LIBNCURSES_VERSION)
WIN_FLEX_BISON_VERSION = 2.5.24
WIN_FLEX_BISON_DIR = vendor/flex_bison_$(WIN_FLEX_BISON_VERSION)
WIN_CMOCKA_VERSION = 1.1.0
@ -111,8 +113,15 @@ $(LIBUNISTRING_DIR)/lib/.libs/libunistring.a: vendor/libunistring-$(LIBUNISTRING
tar -C vendor -xzf vendor/libunistring-$(LIBUNISTRING_VERSION).tar.gz
cd $(LIBUNISTRING_DIR) ; ./configure --enable-static ; $(MAKE)
static: infomsg replaceinfos $(LIBUNISTRING_DIR)/lib/.libs/libunistring.a $(PCRE2_DIR)/.libs/libpcre2-32.a
$(MAKE) -C src BOXES_PLATFORM=static LEX=flex YACC=bison LIBUNISTRING_DIR=$(LIBUNISTRING_DIR) PCRE2_DIR=$(PCRE2_DIR) $@
vendor/libncurses-$(LIBNCURSES_VERSION).tar.gz: | vendor
curl -L http://invisible-mirror.net/archives/ncurses/ncurses-$(LIBNCURSES_VERSION).tar.gz --output $@
$(LIBNCURSES_DIR)/lib/libncurses.a: vendor/libncurses-$(LIBNCURSES_VERSION).tar.gz
tar -C vendor -xzf vendor/libncurses-$(LIBNCURSES_VERSION).tar.gz
cd $(LIBNCURSES_DIR) ; ./configure --enable-static ; $(MAKE)
static: infomsg replaceinfos $(LIBUNISTRING_DIR)/lib/.libs/libunistring.a $(PCRE2_DIR)/.libs/libpcre2-32.a $(LIBNCURSES_DIR)/lib/libncurses.a
$(MAKE) -C src BOXES_PLATFORM=static LEX=flex YACC=bison LIBUNISTRING_DIR=$(LIBUNISTRING_DIR) PCRE2_DIR=$(PCRE2_DIR) LIBNCURSES_DIR=$(LIBNCURSES_DIR) $@
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -89,6 +89,34 @@
"value": "C"
}
]
},
{
"type": "library",
"author": "the ncurses contributors",
"name": "libncurses",
"version": "6.4",
"description": "The ncurses library routines are a terminal-independent method of updating character screens with reasonable optimization.",
"licenses": [
{
"license": {
"name": "MIT with advertising clause",
"url": "http://ncurses.scripts.mit.edu/?p=ncurses.git;a=blob_plain;f=COPYING;hb=79b9071f2be20a24c7be031655a5638f6032f29f"
}
}
],
"copyright": "Copyright 2018-2020,2021 Thomas E. Dickey\nCopyright 1998-2017,2018 Free Software Foundation, Inc.",
"externalReferences": [
{
"type": "website",
"url": "https://invisible-island.net/ncurses/"
}
],
"properties": [
{
"name": "primaryLanguage",
"value": "C"
}
]
}
]
}

View File

@ -64,15 +64,15 @@ debug: flags_$(BOXES_PLATFORM) | $(OUT_DIR)
CFLAGS_ADDTL="-g $(CFLAGS_ADDTL)" flags_$(BOXES_PLATFORM) $(BOXES_EXECUTABLE_NAME)
boxes: $(ALL_OBJ) | check_dir
$(CC) $(LDFLAGS) $^ -o $@ -lunistring -lpcre2-32
$(CC) $(LDFLAGS) $^ -o $@ -lunistring -lpcre2-32 -lncurses
if [ "$(STRIP)" = "true" ] ; then strip $@ ; fi
boxes.static: $(ALL_OBJ) | check_dir
$(CC) $(LDFLAGS) $^ -o $(BOXES_EXECUTABLE_NAME) -l:libunistring.a -l:libpcre2-32.a
$(CC) $(LDFLAGS) $^ -o $(BOXES_EXECUTABLE_NAME) -l:libunistring.a -l:libpcre2-32.a -l:libncurses.a
if [ "$(STRIP)" = "true" ] ; then strip $(BOXES_EXECUTABLE_NAME) ; fi
boxes.exe: $(ALL_OBJ) | check_dir
$(CC) $(LDFLAGS) $^ -o $@ -lkernel32 -l:libunistring.a -l:libpcre2-32.a -l:libiconv.a
$(CC) $(LDFLAGS) $^ -o $@ -lkernel32 -l:libunistring.a -l:libpcre2-32.a -l:libiconv.a -l:libncurses.a
if [ "$(STRIP)" = "true" ] ; then strip $@ ; fi
@ -85,7 +85,7 @@ flags_unix:
flags_static:
$(eval CFLAGS := -I. -I$(SRC_DIR) -Wall -W $(CFLAGS_ADDTL))
$(eval LDFLAGS := $(LDFLAGS) -L../$(LIBUNISTRING_DIR)/lib/.libs -L../$(PCRE2_DIR)/.libs $(LDFLAGS_ADDTL))
$(eval LDFLAGS := $(LDFLAGS) -L../$(LIBUNISTRING_DIR)/lib/.libs -L../$(PCRE2_DIR)/.libs -L../$(LIBNCURSES_DIR)/lib $(LDFLAGS_ADDTL))
$(eval BOXES_EXECUTABLE_NAME := boxes)
$(eval ALL_OBJ := $(GEN_SRC:.c=.o) $(ORIG_NORM:.c=.o))
echo $(filter-out boxes.o,$(ALL_OBJ)) > $(OUT_DIR)/modules.txt

View File

@ -15,10 +15,13 @@
#include "config.h"
#include <ncurses.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <uniconv.h>
#include <unistd.h>
#include "boxes.h"
#include "cmdline.h"
@ -47,6 +50,9 @@ opt_t opt; /* command line options */
input_t input; /* input lines */
int color_output_enabled; /* Flag indicating if ANSI color codes should be printed (1) or not (0) */
/* _\|/_
(o o)
@ -388,6 +394,45 @@ static void handle_remove_box()
/* These two functions are actually declared in term.h, but for some reason, that can't be included. */
extern NCURSES_EXPORT(int) setupterm (NCURSES_CONST char *, int, int *);
extern NCURSES_EXPORT(int) tigetnum (NCURSES_CONST char *);
static int terminal_has_colors()
{
int result = 0;
char *termtype = getenv("TERM");
if (termtype != NULL && setupterm(termtype, STDOUT_FILENO, NULL) == OK && tigetnum("colors") >= 8) {
result = 1;
}
#if defined(DEBUG)
int num_colors = result ? tigetnum("colors") : 0;
fprintf(stderr, "Terminal \"%s\" %s colors (number of colors = %d).\n", termtype != NULL ? termtype : "(null)",
result ? "has" : "does NOT have", num_colors);
#endif
return result;
}
static int check_color_support(int opt_color)
{
int result = 0;
if (opt_color == force_ansi_color) {
result = 1;
}
else if (opt_color == color_from_terminal) {
result = terminal_has_colors();
}
#if defined(DEBUG)
fprintf(stderr, "%s: Color support %sabled\n", PROJECT, result ? "\x1b[92mEN\x1b[0m" : "DIS");
#endif
return result;
}
/* _\|/_
(o o)
+----oOO-{_}-OOo------------------------------------------------------------+
@ -416,6 +461,8 @@ int main(int argc, char *argv[])
fprintf (stderr, "Character Encoding = %s\n", encoding);
#endif
color_output_enabled = check_color_support(opt.color);
handle_config_parsing();
/* If "-l" option was given, list designs and exit. */

View File

@ -141,6 +141,9 @@ extern opt_t opt;
#define color_from_terminal 1
#define force_ansi_color 2
/** Flag indicating if ANSI color codes should be printed (1) or not (0) */
extern int color_output_enabled;
typedef struct {
size_t len; /* length of visible text in columns (visible character positions in a text terminal), which is the same as the length of the 'text' field */

View File

@ -40,6 +40,8 @@ opt_t opt;
input_t input;
int color_output_enabled = 1;
char **collect_out = NULL;
int collect_out_size = 0;